greendao数据库初次使用的配置及多表关联的初始化
1.在工程外层(Project)的build.gradle中添加依赖
1 buildscript { 2 repositories { 3 jcenter() 4 } 5 dependencies { 6 classpath 'com.android.tools.build:gradle:2.3.2' 7 //GreenDao3依赖 8 classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1' 9 } 10 }
2.在项目(Module,也就是app内层)的build.gradle中添加依赖
1 apply plugin: 'com.android.application' 2 //使用greendao 3 apply plugin: 'org.greenrobot.greendao' 4 5 android { 6 compileSdkVersion 23 7 buildToolsVersion "23.0.2" 8 9 defaultConfig { 10 applicationId "com.handsome.didi" 11 minSdkVersion 14 12 targetSdkVersion 23 13 versionCode 1 14 versionName "1.0" 15 } 16 //greendao配置 17 greendao { 18 //版本号,升级时可配置 19 schemaVersion 1 20 } 21 buildTypes { 22 release { 23 minifyEnabled false 24 proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 25 } 26 } 27 } 28 29 30 dependencies { 31 compile fileTree(include: ['*.jar'], dir: 'libs') 32 testCompile 'junit:junit:4.12' 33 compile 'com.android.support:appcompat-v7:23.1.1' 34 //greendao依赖 35 compile 'org.greenrobot:greendao:3.2.0' 36 }
到这里就配置成功了,同步编译(Sync Project)之后没毛病的话就可以往下创建bean类了。
1 @Entity 2 public class Shop{ 3 4 //不能用int 5 @Id(autoincrement = true) 6 private Long id; 7 8 //商品名称 9 @Unique 10 private String name; 11 12 //商品价格 13 @Property(nameInDb = "price") 14 private String price; 15 16 }
这里创建了一个“商品”的实体类,(PS:主键是Long型,不是long哟)然后选择build——>make project,此时会自动为shop类生成构造方法和set、get方法,还有就是greendao操作的重要部分: DaoMaster、DaoSession、DAOS类,但是初次生成并不在src目录下,而是在这里
如果你想把它放入其他目录方便操作或查看的话,则在build.gradle刚刚配置的地方再加上几句
1 greendao { 2 //版本号,升级时可配置 3 schemaVersion 1 4 //文件目录 5 targetGenDir 'src/main/java' 6 //包名路径 7 daoPackage 'com.sharley.greendaodemo.greendao' 8 }
这样它就会自动移植过来了,复制到指定位置
下面示例说明一下多表关联中的一对多的情况
比如我再新建一个实体类user,用户与商品之间成立关联关系,一个用户可购买多个商品,那么在user类中加入ToMany设置。
1 @Entity 2 public class User { 3 4 @Id(autoincrement = true) 5 private Long id; 6 7 private String name; 8 9 @ToMany(referencedJoinProperty = "sid") 10 private List<Shop> shops; 11 }
在shop类中再额外添加一个字段,即一对多时的外主键,注意一定要与user类的id相同
private Long sid;
make project后,user类除了之前说的方法之外还会额外生成一个getShops的方法
1 /** Used to resolve relations */ 2 @Generated(hash = 2040040024) 3 private transient DaoSession daoSession; 4 /** Used for active entity operations. */ 5 @Generated(hash = 1507654846) 6 private transient UserDao myDao; 7 8 /** 9 * To-many relationship, resolved on first access (and after reset). 10 * Changes to to-many relations are not persisted, make changes to the target entity. 11 */ 12 @Generated(hash = 355127918) 13 public List<Shop> getShops() { 14 if (shops == null) { 15 final DaoSession daoSession = this.daoSession; 16 if (daoSession == null) { 17 throw new DaoException("Entity is detached from DAO context"); 18 } 19 ShopDao targetDao = daoSession.getShopDao(); 20 List<Shop> shopsNew = targetDao._queryUser_Shops(id); 21 synchronized (this) { 22 if (shops == null) { 23 shops = shopsNew; 24 } 25 } 26 } 27 return shops; 28 }
然后,差不多就酱。
参考:http://blog.csdn.net/qq_30379689/article/details/54410838#comments