1.配置环境
app gradle下:
apply plugin:'org.greenrobot.greendao'//GreenDao
greendao {//配置数据库
schemaVersion 1
daoPackage'com.xxx.xxx.greendao'
targetGenDir'src/main/java'
}
添加依赖:compile'org.greenrobot:greendao:3.2.0'
as gradle下:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath'com.android.tools.build:gradle:2.2.2'
classpath'org.greenrobot:greendao-gradle-plugin:3.2.0'//GreenDao
}
}
2:创建实体类
@Entity
public class DbBrowsingHistory {
@Id
private longid;
private String Title;
public DbBrowsingHistory() {
}
@Generated(hash=1933645216)
publicDbBrowsingHistory(longid,String classTitle) {
this.id= id;
this.classTitle= classTitle;
}
public long getId() {
return this.id;
}
public void setId(longid) {
this.id= id;
}
public String getTitle() {
return this.title;
}
public voidsetTitle(String title) {
this.title= title;
}
}
}
3.操作数据库工具类
public class DbUtils {
private static final StringDB_NAME="db_browsinghistory";
private Context context;
private static DbUtils mInstance;
private DaoMaster.DevOpenHelper openHelper;
private DbUtils(Context context) {
this.context= context;
this.openHelper=new DaoMaster.DevOpenHelper(context,DB_NAME, null);
}
public static DbUtilsgetInstance(Context context) {
if(mInstance==null) {
synchronized(DbUtils.class) {
if(mInstance==null) {
mInstance=new DbUtils(context);
}
}
}
return mInstance;
}
/**
*获取可读数据库
*/
private SQLiteDatabasegetReadableDatabase() {
if(openHelper==null) {
openHelper=new DaoMaster.DevOpenHelper(context,DB_NAME, null);
}
SQLiteDatabase db =openHelper.getReadableDatabase();
return db;
}
private SQLiteDatabasegetWritableDataBase() {
if(openHelper==null) {
openHelper=newDaoMaster.DevOpenHelper(context,DB_NAME, null);
}
SQLiteDatabase db =openHelper.getWritableDatabase();
return db;
}
/*
插入
*/
public void insert(DbBrowsingHistory dbBrowsingHistory) {
DaoMaster daoMaster =new DaoMaster(getWritableDataBase());
DaoSession daoSession = daoMaster.newSession();
DbBrowsingHistoryDao dbBrowsingHistoryDao =daoSession.getDbBrowsingHistoryDao();
dbBrowsingHistoryDao.insert(dbBrowsingHistory);
}
/*
删除
*/
public void delete(DbBrowsingHistory dbBrowsingHistory) {
DaoMaster daoMaster =new DaoMaster(getWritableDataBase());
DaoSession daoSession = daoMaster.newSession();
DbBrowsingHistoryDao dbBrowsingHistoryDao =daoSession.getDbBrowsingHistoryDao();
dbBrowsingHistoryDao.delete(dbBrowsingHistory);
}
/*
更新
*/
public void update(DbBrowsingHistory dbBrowsingHistory) {
DaoMaster daoMaster =new DaoMaster(getWritableDataBase());
DaoSession daoSession = daoMaster.newSession();
DbBrowsingHistoryDao dbBrowsingHistoryDao =daoSession.getDbBrowsingHistoryDao();
dbBrowsingHistoryDao.update(dbBrowsingHistory);
}
/*
查询
*/
public List
DaoMaster daoMaster =new DaoMaster(getWritableDataBase());
DaoSession daoSession = daoMaster.newSession();
DbBrowsingHistoryDao dbBrowsingHistoryDao =daoSession.getDbBrowsingHistoryDao();
List browsingHistoryList = dbBrowsingHistoryDao.loadAll();
return browsingHistoryList;
}
/*
检查是否已存在
*/
public boolean isHave(DbBrowsingHistory dbBrowsingHistory) {
DaoMaster daoMaster =new DaoMaster(getWritableDataBase());
DaoSession daoSession = daoMaster.newSession();
DbBrowsingHistoryDao dbBrowsingHistoryDao =daoSession.getDbBrowsingHistoryDao();
for(DbBrowsingHistory history:dbBrowsingHistoryDao.loadAll()){
if(history.getId()==dbBrowsingHistory.getId()){
return true;
}
}
return false;
}
}
运行后自动生成: