GreenDAO 3.0 基本使用
来源:互联网 发布:什么软件需要win7sp1 编辑:程序博客网 时间:2024/04/25 14:06
之前项目中用到了GreenDAO,使用的是2.X的版本,总结下来就是非常难用。2.X中是新建一个项目,然后再项目中配置各个实体的字段等相关属性,然后生成实体以及DAO操作相关的一些类。但是,当你修改了实体中的一些属性,并且这些属性又要在数据库中保存的时候,你会发现每次重新运行generator之前的改变都得重新再来一次。
GreenDAO中比较大的变化就是使用了注解和Gradle,而且生成方式也发生了逆向变化,现在是先自己新建实体,然后从实体生成DAO操作相关的一些类。
有两种方式使用最新的GreenDAO3,一种是保留用于生成的项目,另一种则是使用基于注解的实体生成方式。我们会注意到GreenDAO3之后,包名由之前的de.greenrobot 改为了 org.greenrobot。还要注意,de.greenrobot.daogenerator 变为了 org.greenrobot.greendao.generator。de.greenrobot.dao变为了org.greenrobot.greendao。
配置Gradle:
在你的build.gradle中加入以下内容:
buildscript { repositories { mavenCentral() } dependencies { classpath 'org.greenrobot:greendao-gradle-plugin:3.0.0' }}// In current version 3.0.0, this must precede the android plugin!apply plugin: 'org.greenrobot.greendao'dependencies { compile 'org.greenrobot:greendao:3.0.1'}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
然后再Make Project 你会在build/generated/source/greendao中发现新生成的内容,build/generated/source/greendao是默认路径,可以去配置的,配置方法在接下来将会讲到。
Gradle Plugin的配置
// In the build.gradle file of your app project:android {...}greendao { schemaVersion 2}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
schemaVersion 当前数据库结构的版本。结构版本变化时在OpenHelpers中被使用到。当你改变实体或者数据的结构时,这个值应该增加。
daoPackage 生成的DAO,DaoMaster和DaoSession的包名。默认是实体的包名。
targetGenDir 生成源文件的路径。默认源文件目录是在build目录中的(build/generated/source/greendao)。
generateTests 设置是否自动生成单元测试。
targetGenDirTest 生成的单元测试的根目录。
实体注解:
@Entity( // 如果你有超过一个的数据库结构,可以通过这个字段来区分 // 该实体属于哪个结构 schema = "myschema", // 实体是否激活的标志,激活的实体有更新,删除和刷新的方法 active = true, // 确定数据库中表的名称 // 表名称默认是实体类的名称 nameInDb = "AWESOME_USERS", // Define indexes spanning multiple columns here. indexes = { @Index(value = "name DESC", unique = true) }, // DAO是否应该创建数据库表的标志(默认为true) // 如果你有多对一的表,将这个字段设置为false // 或者你已经在GreenDAO之外创建了表,也将其置为false createInDb = false)public class User { ...}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
注意,使用Gradle Plugin时暂不支持多个数据库结构。
基础的注解:
@Entitypublic class User { @Id(autoincrement = true) private Long id; @Property(nameInDb = "USERNAME") private String name; @NotNull private int repos; @Transient private int tempUsageCount; ...}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
@Id 选定一个long/Long类型的字段作为实体的ID,即数据库中的主键。 @Property 让你自定义字段在数据库中的名称,如果为空,GreenDAO将根据驼峰法将其用”_”分割,并全部转为大写,如userName 变为 USER_NAME。@NonNull 使字段在数据库中成为非空字段,通常都会将基本类型加上NonNull标志。 @Transient 使得字段不再持久化。
索引注解:
@Index 给对应的字段创建索引,可以使用以下参数进行自定义:
name: 自定义的名称。
unique: 添加唯一性标志,使得其值必须唯一。
@Entitypublic class User { @Id private Long id; @Index(unique = true) private String name;}
- 1
- 2
- 3
- 4
- 5
- 6
- 1
- 2
- 3
- 4
- 5
- 6
@Unique 对数据库列添加唯一性限制。
@Entitypublic class User { @Id private Long id; @Unique private String name;}
- 1
- 2
- 3
- 4
- 5
- 1
- 2
- 3
- 4
- 5
数据库关系注解
@ToOne 定义一对一的关系。此注解适用于其他实体对象的字段。GreenDAO需要一个字段来和外联实体中的ID相对应,所以在joinProperty中制定这个字段。如果没有自定,则会自动生成一个字段。
@Entitypublic class Order { @Id private Long id; private long customerId; @ToOne(joinProperty = "customerId") private Customer customer;}@Entitypublic class Customer { @Id private Long id;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
@ToMany 定义和多个实体之间的关系。此注解适用于其他实体对象集合的字段。有三种方式可以用来实现多映射。
referencedJoinProperty:指定目标实体中与源实体相对应的外键。
@Entitypublic class User { @Id private Long id; @ToMany(referencedJoinProperty = "ownerId") private List<Site> ownedSites;}@Entitypublic class Site { @Id private Long id; private long ownerId;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
joinProperty: 对于复杂一点的关系可以定义一组@JoinProperty注解。每个@JoinProperty注解都需要有源实体中的源属性和对应实体中的引用属性。
@Entitypublic class User { @Id private Long id; @Unique private String authorTag; @ToMany(joinProperties = { @JoinProperty(name = "authorTag", referencedName = "ownerTag") }) private List<Site> ownedSites;}@Entitypublic class Site { @Id private Long id; @NotNull private String ownerTag;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
@JoinEntity 在做NM多对多映射的时候使用
@Entitypublic class Site { @Id private Long id; @ToMany @JoinEntity( entity = JoinSiteToUser.class, sourceProperty = "siteId", targetProperty = "userId" ) private List<User> authors;}@Entitypublic class JoinSiteToUser { @Id private Long id; private Long siteId; private Long userId;}@Entitypublic class User { @Id private Long id;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
使用体会:
升级到3.0后每次生成的DAO相关文件中总会报错,一个是DevOpenHelper中的方法名称发生了改变,还有就是DAOs中的bindValue(SQLiteStatement, T)没有了继承关系。
- GreenDAO 3.0 基本使用
- greenDao 3.0基本使用
- GreenDao 的基本使用
- GreenDao 基本使用
- greenDao--3.2基本使用
- GreenDao-基本使用
- greenDao的基本使用
- greenDAO基本使用详解
- GreenDao基本使用
- Android GreenDao基本使用
- Android GreenDao基本使用
- GreenDao基本使用_
- greenDao的基本使用
- GreenDao的基本使用
- greenDao的基本使用
- GreenDAO的使用基本篇
- 安卓 GreenDao 基本使用
- greenDao Orm 基本使用<一>
- 硬币找零
- PATl两个有序链表序列的合并
- 统一设置状态栏高亮
- ibatis 奇怪问题
- 2012蓝桥杯试题第二题
- GreenDAO 3.0 基本使用
- handlebars.js模版引擎中EACH循环遍历的使用
- 字节流与字符流、字节与字符 --个人理解
- windows通过StretchBlt实现动态背景移动
- iOS中block的详解weakSelf、strongSelf <转自唐巧>
- 15 个有趣的 JS 和 CSS 库
- C语言::printf()函数功能、原型、用法及实例
- 互联网支付:商户T+2的結算方式
- Postman的安装与使用