您的位置:首页 > 其它

ORM对象关系映射之GreenDAO高级用法

2015-09-15 19:48 555 查看

CRUD

增加:

dao.insert(Student entity);//添加一个
dao.insertInTx(Student... entity);//批量添加


删除:

dao.deleteByKey(Long key);//根据主键删除
dao.deleteByKeyInTx(Long... keys);//批量删除
dao.delete(Student entity);//根据实体删除
dao.deleteInTx(Student... entities);//批量删除
dao.deleteAll();//全部删除


修改:

dao.update(Student entity);//根据实体更新
dao.updateInTx(Student... entities);//批量更新


查找:

Query<Student> query = dao.queryBuilder().where(StudentDao.Properties.Name.eq(content)).build();
List<Student> list = query.list();
//或者利用sql语言查询
Query query = dao.queryBuilder().where( new StringCondition("_ID IN " + "(SELECT _ID FROM STUDENT WHERE AGE = 20)").build()


创建属性约束

我们可以在为Schema添加实体的时候,在实体添加属性时候进行属性的一些约束,如:

Schema schema = new Schema(1,"com.sunzxyong.greendao");
Entity entity = schema.addEntity("Student");
entity.addIdProperty().autoincrement().primaryKey();
entity.addStringProperty("name").notNull();
entity.addIntProperty("age");
entity.addBooleanProperty("is_man").notNull();
new DaoGenerator().generateAll(schema,"../GreenDAODemo/app/src/main/java-gen");


实体类的继承、接口、序列化

entity.implementsSerializable();//实现序列化
entity.implementsInterface("com.sunzxyong.A");//实现A接口
entity.setSuperclass("com.sunzxyong.B");//继承B类
entity.addImport("com.sunzxyong.C");//导包


保持实体中自定义的代码不会被覆盖

假如你需要在实体类中自定义一些字段变量和方法,而再次generator的实体类通常会覆盖以前的,如果你不想覆盖掉你自定义的代码,可以这样设置:

schema.enableKeepSectionsByDefault();//通过次Schema对象添加的所有实体都不会覆盖自定义的代码
//或者
entity.setHasKeepSections(true);//单独让某个实体不覆盖自定义的代码


如果设置了上述代码,则生成的实体中会多出以下代码:

// KEEP INCLUDES - put your custom includes here
在这里添加引入
// KEEP INCLUDES END

// KEEP FIELDS - put your custom fields here
在这里添加字段变量
// KEEP FIELDS END

// KEEP METHODS - put your custom methods here
在这里添加方法
// KEEP METHODS END


【注意】:添加的自定义代码只能在我用文字标注的地方添加,否则超过次区域的GreenDAO生成实体时将会覆盖

多线程环境下使用GreenDAO

在多线程的环境下使用查询,你必须调用query的 forCurrentThread()为当前的线程获得一个query实例,如:

Query<Student> query = dao.queryBuilder().where(StudentDao.Properties.Name.eq(content)).build().forCurrentThread();
List<Student> list = query.list();


LazyList和List

在通过queryBuilder建立查询时候:

1、如果你想得到一个唯一的结果,可以调用Query的unique()方法,如:

Query<Student> query = dao.queryBuilder().where(StudentDao.Properties.Name.eq(content)).build().forCurrentThread();
Student student = query.unique();


2、如果你想得到多个结果,可以调用Query的list()或者listLazy()方法,这两个方法有什么区别呢?

List - 当所有的实体查询完会立即一次性加载到内存中,即在List集合中

LazyList - 懒加载的方式,即实体按照需求加载进入内存(查询完不会立即加载到内存中,只有在需要数据的时候也就是遍历这个LazyList的时候,才会加载到内存中),列表中的元素在第一次访问时候就会同时缓存在一个ArrayList集合中,它再次访问这些元素则直接从集合中拿。如果你不想缓存这些数据又想懒加载,可以通过Query的listLazyUncached()方法。这个集合在使用完毕必须close,即调用:listLazy.close();

GreenDao的代码混淆配置

-keepclassmembers class * extends de.greenrobot.dao.AbstractDao {
public static java.lang.String TABLENAME;
}
-keep class **$Properties


借助两个Flag打印出SQL命令和传入的值

通过设置这两个Flag,可以在控制台中打印出我们执行的sql语句以及传入的值,方便用于检查

//下面两个Flag的设置可以在控制台中打印出此次查询的sql语句和value值
QueryBuilder.LOG_SQL = true;
QueryBuilder.LOG_VALUES = true;
Query<Student> query = dao.queryBuilder().where(StudentDao.Properties.Name.eq(content)).build().forCurrentThread();
LazyList<Student> list = query.listLazyUncached();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: