1.配置環(huán)境
app ? ? ? gradle下:
apply ? plugin:'org.greenrobot.greendao'//GreenDao
greendao {//配置數(shù)據(jù)庫(kù)
schemaVersion 1
daoPackage'com.xxx.xxx.greendao'
targetGenDir'src/main/java'
}
添加依賴(lài):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:創(chuàng)建實(shí)體類(lèi)
@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.操作數(shù)據(jù)庫(kù)工具類(lèi)
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;
}
/**
*獲取可讀數(shù)據(jù)庫(kù)
*/
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);
}
/*
查詢(xún)
*/
public List<DbBrowsingHistory>? select() {
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;
}
}
運(yùn)行后自動(dòng)生成: