Android开源:数据库ORM框架GreenDao学习(三)封装工具类使用

博客转载地址:http://www.it165.net/pro/html/201401/9026.html
        上一篇中讲解了基本的增删改查,本篇继续讲解 QureyBuilder 使用,及工具类封装使用

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

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


  1.查询

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

 


public boolean isSaved(int ID){

  QueryBuilder qb = saveListDao.queryBuilder();
   qb.where(Properties.Id.eq(ID));
   qb.buildCount().count();
   return qb.buildCount().count() > 0 ? true : false;
}



范例2:获取整个表的数据集合,一句代码就搞定!

 

public List getPhotoGallery()
{
    return photoGalleryDao.loadAll();// 获取图片相册
}

 

范例3:通过一个字段值查找对应的另一个字段值(为简便直接使用下面方法,也许有更简单的方法,尚未尝试)

 
/** 通过图片id查找其目录id */
public int getTypeId(int picId)
{
   QueryBuilder qb = photoGalleryDao.queryBuilder();
   qb.where(Properties.Id.eq(picId));
   if (qb.list().size() > 0)
   {
      return qb.list().get(0).getTypeId();
   }else{
      return -1;
   }
}


 

范例4:查找所有第一姓名是“Joe”并且以lastname排序。

 

 
  List joes = userDao.queryBuilder()
  where(Properties.FirstName.eq("Joe"))
  orderAsc(Properties.LastName)
  list();



范例5:多重条件查询

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


public List getSupportingList(int cityId){
   QueryBuilder qb = cityInfoDao.queryBuilder();
   qb.where(qb.and(Properties.CityId.eq(cityId),Properties.InfoType.eq(HBContant.CITYINFO_SL)));
   qb.orderAsc(Properties.Id);// 排序依据
   return qb.list();
}


 

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

 

  
  QueryBuilder qb = userDao.queryBuilder();
  qb.where(Properties.FirstName.eq("Joe"),
  qb.or(Properties.YearOfBirth.gt(1970),
  qb.and(Properties.YearOfBirth.eq(1970), Properties.MonthOfBirth.ge(10))));
  List youngJoes = qb.list();



 

范例6:获取某列对象

 
 picJsonDao.loadByRowId(picId);



 

 

2.增添/插入、修改

插入数据更加简单,也是只要一句代码便能搞定!

 

 
public void addToPhotoTable(Photo p){
 photoDao.insert(p);
}



 

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

 

 
  DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "notes-db", null);
  db = helper.getWritableDatabase();
  daoMaster = new DaoMaster(db);
  daoSession = daoMaster.newSession();
  noteDao = daoSession.getNoteDao();
  Note note = new Note(null, noteText, comment, new Date());
  noteDao.insert(note);



 

修改更新:


photoDao.insertOrReplace(photo);

photoDao.insertInTx(photo);



 

3.删除:

(1)清空表格数据

 


/** 清空相册图片列表的数据 */
public void clearPhoto(){

 photoDao.deleteAll();

}



 

(2)删除某个对象

public void deleteCityInfo(int cityId){

  QueryBuilder qb = cityInfoDao.queryBuilder();
  DeleteQuery bd = qb.where(Properties.CityId.eq(cityId)).buildDelete();
  bd.executeDeleteWithoutDetachingEntities();
}


 

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

 

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

 

(三)常用方法笔记

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

 

private static DaoMaster daoMaster;
private static DaoSession daoSession;

/**
* 取得DaoMaster
*
* @param context
* @return
*/
public static DaoMaster getDaoMaster(Context context)
{
   if (daoMaster == null){
     OpenHelper helper = new DaoMaster.DevOpenHelper(context, ontant.DATABASE_NAME, null);
     daoMaster = new DaoMaster(helper.getWritableDatabase());
   }
   return daoMaster;
}

 /**
* 取得DaoSession
*
* @param context
* @return
*/
public static DaoSession getDaoSession(Context context){
   if (daoSession == null){
     if (daoMaster == null){
        daoMaster = getDaoMaster(context);
      }
     daoSession = daoMaster.newSession();
   }
     return daoSession;
}

 


 

2.增删改查工具类:

public class DBHelper{

private static Context mContext;
private static DBHelper instance;
 
private CityInfoDBDao cityInfoDao;
 
private DBHelper(){

}
 
public static DBHelper getInstance(Context context){
    if (instance == null){
        instance = new DBHelper();
    if (mContext == null){
        mContext = context;
    }
 
       // 数据库对象
       DaoSession daoSession = HBApplication.getDaoSession(mContext);
       instance.cityInfoDao = daoSession.getCityInfoDBDao();
   }
    return instance;
}
 
/** 添加数据 */
public void addToCityInfoTable(CityInfo item){
   cityInfoDao.insert(item);
}
 

/** 查询 */
public List getCityInfoList(){
 
   QueryBuilder qb = cityInfoDao.queryBuilder();

   return qb.list();
}
 
/** 查询 */
public List getCityInfo(){

   return cityInfoDao.loadAll();// 查找图片相册
}
 
/** 查询 */
public boolean isSaved(int Id){

   QueryBuilder qb = cityInfoDao.queryBuilder();
   qb.where(Properties.Id.eq(Id));
   qb.buildCount().count();
   return qb.buildCount().count() > 0 ? true : false;// 查找收藏表
}
 
/** 删除 */
public void deleteCityInfoList(int Id){
  QueryBuilder qb = cityInfoDao.queryBuilder();
   DeleteQuery bd = where(Properties.Id.eq(Id)).buildDelete();
   bd.executeDeleteWithoutDetachingEntities();
}
 
/** 删除 */
public void clearCityInfo(){
  cityInfoDao.deleteAll();
}
 
/** 通过城市id查找其类型id */
public int getTypeId(int cityId){
  QueryBuilder qb = cityInfoDao.queryBuilder();
   qb.where(Properties.Id.eq(cityId));
   if (qb.list().size() > 0){
     return qb.list().get(0).getTypeId();
   }else{
     return 0;
   }
}
 
/** 多重查询 */
public List getIphRegionList(int cityId){

  QueryBuilder qb = cityInfoDao.queryBuilder();
   qb.where(qb.and(Properties.CityId.eq(cityId), perties.InfoType.eq(HBContant.CITYINFO_IR)));
   qb.orderAsc(Properties.Id);// 排序依据
   return qb.list();
   }
}



另外,还有多表关联、惰性加载等功能,待后续研究。

参考资料:

1.https://github.com/greenrobot/greenDAO

2.http://greendao-orm.com/documentation/how-to-get-started/

3.http://blog.csdn.net/krislight/article/details/9391455



你可能感兴趣的:(Android,GreenDao)