相信很多人都用过greenDao 今天 我抽空总结下使用的时候一些小东西吧 废话不多说 下边就GreenDao 的使用遇到的问题以及解决方案记录一下吧。
1.greendao 指定不同的生成目录:
Schema 可以制定路径
schema.setDefaultJavaPackageDao(defaultJavaPackageDao)
2.greendao 生成model 之后项目引用需要序列化:
addStringProperty :个人理解是生成表字段个根据 如果没有设置columnName 则就默认使用该方法的参数下划线的命名
columnName: 则表示指定表字段的名称
![](http://upload-images.jianshu.io/upload_images/5318415-6a5292b6415fd21d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
3.greenDao我们有时候需要在查询结果中多一列 但是不需要在表中生成字段
例如:adress表中 他有多个办公地点 我们需要一个addrCount字段来计算布局中的item 但是这个字段是从其他表关联查询道的 不许要在adress表中生成字段
注释:KEEP INCLUDES KEEP FIELDS KEEP METHODS 这三个注释之间的内容是在重新运行generator 的时候不会被清空
![](http://upload-images.jianshu.io/upload_images/5318415-710517c67b99081f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](http://upload-images.jianshu.io/upload_images/5318415-6941d3ec7aff2c1a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](http://upload-images.jianshu.io/upload_images/5318415-3b755acfd4af951c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
4.greendao where 条件查询 WhereCondition 有多个条件的时候
源码给出的 多个whereCondition
![](http://upload-images.jianshu.io/upload_images/5318415-ab22856ee5c7386e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
另外一种思路吧 毕竟一千个人 一千种写法 只是 抛砖引玉:
![](http://upload-images.jianshu.io/upload_images/5318415-130b983a7ed2d94d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](http://upload-images.jianshu.io/upload_images/5318415-b7580a2ea148bfb9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
5.greendao链接多个db库 以及切换库连接
其中的getDaoSession方法
![](http://upload-images.jianshu.io/upload_images/5318415-f1c1de2459a1fdff.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
对指定的表进行升级 删除等操作就需要新建openHelper来操作了
![](http://upload-images.jianshu.io/upload_images/5318415-336f5c06b3a165a0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
其他的greengao 信息相信网上的资料也很多 大家可以自行百度关于greendao 连接多库的demo如下 :
https://github.com/firfoxsky/greenDaoOutsideDB.git