欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android开源:数据库ORM框架GreenDao封装工具类使用

程序员文章站 2024-01-08 12:01:40
Android开源:数据库ORM框架GreenDao封装工具类使用。 一、使用QureyBuilder实现表的增删改查 增删改查相当方便,完全的面向对象,不需要涉及到任何的sq...

Android开源:数据库ORM框架GreenDao封装工具类使用。

一、使用QureyBuilder实现表的增删改查

增删改查相当方便,完全的面向对象,不需要涉及到任何的sql语言。

1.查询

范例1:查询某个表是否包含某个id:

[java]view plaincopy

publicbooleanisSaved(intID){

QueryBuilderqb=saveListDao.queryBuilder();

qb.where(Properties.Id.eq(ID));

qb.buildCount().count();

returnqb.buildCount().count()>0?true:false;

}

[html]view plaincopy

/**通过图片id查找其目录id*/

publicintgetTypeId(intpicId)

{

QueryBuilderqb=photoGalleryDao.queryBuilder();

qb.where(Properties.Id.eq(picId));

if(qb.list().size()>0)

{

returnqb.list().get(0).getTypeId();

}else{

return-1;

}

}

Listjoes=userDao.queryBuilder()

where(Properties.FirstName.eq("Joe"))

orderAsc(Properties.LastName)

list();

范例5:多重条件查询

(1)获取id为cityId并且infotype为HBContant.CITYINFO_SL的数据集合:

[java]view plaincopy

publicListgetSupportingList(intcityId){

QueryBuilderqb=cityInfoDao.queryBuilder();

qb.where(qb.and(Properties.CityId.eq(cityId),Properties.InfoType.eq(HBContant.CITYINFO_SL)));

qb.orderAsc(Properties.Id);//排序依据

returnqb.list();

}

(2)获取firstname为“Joe”并且出生于1970年10月以后的所有user集合:

[java]view plaincopy

QueryBuilderqb=userDao.queryBuilder();

qb.where(Properties.FirstName.eq("Joe"),

qb.or(Properties.YearOfBirth.gt(1970),

qb.and(Properties.YearOfBirth.eq(1970),Properties.MonthOfBirth.ge(10))));

ListyoungJoes=qb.list();

picJsonDao.loadByRowId(picId);

publicvoidaddToPhotoTable(Photop){

photoDao.insert(p);

}

插入时需要new一个新的对象,范例如下:

[java]view plaincopy

DevOpenHelperhelper=newDaoMaster.DevOpenHelper(this,"notes-db",null);

db=helper.getWritableDatabase();

daoMaster=newDaoMaster(db);

daoSession=daoMaster.newSession();

noteDao=daoSession.getNoteDao();

Notenote=newNote(null,noteText,comment,newDate());

noteDao.insert(note);

photoDao.insertOrReplace(photo);

photoDao.insertInTx(photo);

/**清空相册图片列表的数据*/

publicvoidclearPhoto(){

photoDao.deleteAll();

}

(2)删除某个对象

[java]view plaincopy

publicvoiddeleteCityInfo(intcityId){

QueryBuilderqb=cityInfoDao.queryBuilder();

DeleteQuerybd=qb.where(Properties.CityId.eq(cityId)).buildDelete();

bd.executeDeleteWithoutDetachingEntities();

}

参考:https://github.com/greenrobot/greenDAO/issues/34

由上可见,使用greenDAO进行数据库的增删改查时及其方便,而且性能极佳。

(三)常用方法笔记

1.在Application实现得到DaoMaster和DaoSession的方法:

[java]view plaincopy

privatestaticDaoMasterdaoMaster;

privatestaticDaoSessiondaoSession;

/**

*取得DaoMaster

*

*@paramcontext

*@return

*/

publicstaticDaoMastergetDaoMaster(Contextcontext)

{

if(daoMaster==null){

OpenHelperhelper=newDaoMaster.DevOpenHelper(context,ontant.DATABASE_NAME,null);

daoMaster=newDaoMaster(helper.getWritableDatabase());

}

returndaoMaster;

}

/**

*取得DaoSession

*

*@paramcontext

*@return

*/

publicstaticDaoSessiongetDaoSession(Contextcontext){

if(daoSession==null){

if(daoMaster==null){

daoMaster=getDaoMaster(context);

}

daoSession=daoMaster.newSession();

}

returndaoSession;

}

[java]view plaincopy

publicclassDBHelper{

privatestaticContextmContext;

privatestaticDBHelperinstance;

privateCityInfoDBDaocityInfoDao;

privateDBHelper(){

}

publicstaticDBHelpergetInstance(Contextcontext){

if(instance==null){

instance=newDBHelper();

if(mContext==null){

mContext=context;

}

//数据库对象

DaoSessiondaoSession=HBApplication.getDaoSession(mContext);

instance.cityInfoDao=daoSession.getCityInfoDBDao();

}

returninstance;

}

/**添加数据*/

publicvoidaddToCityInfoTable(CityInfoitem){

cityInfoDao.insert(item);

}

/**查询*/

publicListgetCityInfoList(){

QueryBuilderqb=cityInfoDao.queryBuilder();

returnqb.list();

}

/**查询*/

publicListgetCityInfo(){

returncityInfoDao.loadAll();//查找图片相册

}

/**查询*/

publicbooleanisSaved(intId){

QueryBuilderqb=cityInfoDao.queryBuilder();

qb.where(Properties.Id.eq(Id));

qb.buildCount().count();

returnqb.buildCount().count()>0?true:false;//查找收藏表

}

/**删除*/

publicvoiddeleteCityInfoList(intId){

QueryBuilderqb=cityInfoDao.queryBuilder();

DeleteQuerybd=where(Properties.Id.eq(Id)).buildDelete();

bd.executeDeleteWithoutDetachingEntities();

}

/**删除*/

publicvoidclearCityInfo(){

cityInfoDao.deleteAll();

}

/**通过城市id查找其类型id*/

publicintgetTypeId(intcityId){

QueryBuilderqb=cityInfoDao.queryBuilder();

qb.where(Properties.Id.eq(cityId));

if(qb.list().size()>0){

returnqb.list().get(0).getTypeId();

}else{

return0;

}

}

/**多重查询*/

publicListgetIphRegionList(intcityId){

QueryBuilderqb=cityInfoDao.queryBuilder();

qb.where(qb.and(Properties.CityId.eq(cityId),perties.InfoType.eq(HBContant.CITYINFO_IR)));

qb.orderAsc(Properties.Id);//排序依据

returnqb.list();

}

}

 

上一篇:

下一篇: