GreenDao 3.X之注解
来源:互联网 发布:软件修改器 编辑:程序博客网 时间:2024/05/21 11:36
在注解横行的时代,GreenDao 需要通过新建GreenDaoGenerator工程生成Java数据对象(实体)和DAO对象,已经追赶不上时代前行的步伐了。GreenDao 3.X的注解开发模式,终于解开它的面纱,GreenDao 3.X最大的变化就是采 用注解的方式通过编译方式生成Java数据对象和DAO对象。
Gradle配置
buildscript { repositories { mavenCentral() } dependencies { classpath 'org.greenrobot:greendao-gradle-plugin:3.1.0' } } apply plugin: 'org.greenrobot.greendao' dependencies { compile 'org.greenrobot:greendao:3.1.0' } // 数据库的版本及Dao相关设置 greendao { // 数据库schema版本,也可以理解为数据库版本号 schemaVersion 1000 // 设置DaoMaster 、DaoSession、Dao包名 daoPackage '***' // 设置DaoMaster 、DaoSession、Dao目录 targetGenDir 'src/main/java' // 设置生成单元测试目录 targetGenDirTest // 设置自动生成单元测试用例 generateTests }
常用注解
@Entity -- 实体注解
public @interface Entity { /** * 在数据库中表的名称,默认为实体的类名 */ String nameInDb() default ""; /** * 定义索引,可以跨越多个列(默认为实体类成员变量的个数) */ Index[] indexes() default {}; /** * 标记创建数据库表 * 若一个表映射多个实体类或者创建表外应的GreenDao,设置为false */ boolean createInDb() default true; /** * 告知GreenDao当前实体属于哪个schema */ String schema() default "default"; /** * 实体活动状体标志位(默认为false) * 若设置为true,实体有更新、删除和刷新方法 */ boolean active() default false; }
@NotNull
设置表中当前列的值不可为空
@Convert
指定自定义类型(@link PropertyConverter)
public @interface Convert { /** 转换类*/ Class<? extends PropertyConverter> converter(); /** * 在数据库中持久化的列 * 此受限于GreenDao所支持的类 */ Class columnType(); }
@Generated
GreenDao运行所产生的构造函数或者方法,被此标注的代码可以变更或者下次运行时清除
public @interface Generated { int hash() default -1; }
@Id
主键 Long型,可以通过@Id(autoincrement = true)设置自增长。通过这个注解标记的字段必须是Long,数据库中表示它就是主键,并且默认是自增的。
public @interface Id { /** * 设置是否为自增长,默认为false */ boolean autoincrement() default false; }
@Index
使用@Index作为一个属性来创建一个索引;定义多列索引(@link Entity#indexes())
public @interface Index { /** * 通过逗号间隔创建表的属性索引,例如 “propertyA,propertyB,propertyC” * 若要指定排序, 需在列明以后添加 ASC(升序) 或者DESC(降序) , 例如 "propertyA DESC, propertyB ASC" * 只有实体类中使用 {@link Entity#indexes()} 才可设置 */ String value() default ""; /** * 表的可选索引 * 默认为实体类中的成员变量 */ String name() default ""; /** * 是否为属性设置唯一属性,默认为false */ boolean unique() default false; }
@JoinEntity
定义表连接关系
public @interface JoinEntity { /** 添加的实体类 */ Class<?> entity(); /** 源表的列索引 */ String sourceProperty(); /** 连接表内拥有源实体的属性*/ String targetProperty(); }
@JoinProperty
定义名称和引用名称属性关系
public @interface JoinProperty { /** 实体中的名称,对应于引用的名称 */ String name(); /** 引用的名称 */ String referencedName(); }
@Keep
注解的代码段在GreenDao下次运行时保持不变
1.注解实体类:默认禁止修改此类2.注解其他代码段,默认禁止修改注解的代码段
@OrderBy
指定排序
public @interface OrderBy { /** * 通过逗号间隔创建表的属性索引,例如 “propertyA,propertyB,propertyC” * 若要指定排序, 需在列明以后添加 ASC(升序) 或者DESC(降序) , 例如 "propertyA DESC, propertyB ASC" * 默认按升序排序 * 若不设置默认根据主键排序 */ String value() default ""; }
@Property
设置一个非默认关系映射所对应的列名,默认是的使用字段名 举例:@Property (nameInDb="name")
public @interface Property { /** * 默认是的使用字段名 */ String nameInDb() default ""; }
@ToMany
定义与多个实体对象的关系
public @interface ToMany { /** * 目标实体持有源实体的名称 * 此必须设置,否则有 {@link JoinProperty} or {@link JoinEntity} 指定 */ String referencedJoinProperty() default ""; /** * 将源表列索引->目标列 * 此必须设置,否则有 {@link JoinProperty} or {@link JoinEntity} 指定 */ JoinProperty[] joinProperties() default {}; }
@ToOne
定义与另一个实体(一个实体对象)的关系
public @interface ToOne { /** * 表中相关实体的属性名称 * 如果该参数不存在,则附加列会自动创建保存密钥 */ String joinProperty() default ""; }
@Transient
添加次标记之后不会生成数据库表的列
@Unique
向数据库列添加了一个唯一的约束
现对GreenDao 3.X的改动已经有了初步了解,尤其是各个注解的含义,后续深入了解GreenDao 3.0如何使用。
参考文档
1、官方文档
2.、GreenDAO 3.0 初次使用
- GreenDao 3.X之注解
- GreenDao 3.X之RxJava
- GreenDao 3.X之基本使用
- GreenDao 3.X
- GreenDao 3.x 注解中ToOne和ToMany的个人理解
- greenDao 3.X 的使用简析
- Android ORM框架介绍之greenDao注解及加密(三)
- GreenDao 3.0 使用详解之注解实体类
- greendao中的注解
- Android GreenDao常用注解
- 力所能及之Hibernate4.3.x注解常见错误
- GreenDAO 3.x官方文档 — Android ORM框架(二)
- GreenDAO 3.x官方文档 — Android ORM框架(二)
- Android开源框架greenDAO 3.X的使用
- GreenDAO之「01.初始GreenDAO」
- android 数据库之greendao
- Android ORM之greenDao
- Andorid之GreenDao
- 科技论文写作
- Hive学习笔记 --- Hive的数据类型
- 2567. 【NOIP2011模拟9.17】电话时间 (StandardIO)
- 设计模式-适配器模式
- CodeForces 510B Fox And Two Dots(判断环的存在性,DFS一类题)
- GreenDao 3.X之注解
- Arithmetic problem | 二进制表示
- 嵌入式开发28天(线程)
- HTML <pre>标签
- js调用百度地图接口绘制任意多边形并获取每个点的经纬度
- 一点小感悟
- CListCtrl控件的标题栏添加checkbox
- 2016CCPC网络赛赛后总结——回顾与反思
- Android官方开发文档Training系列课程中文版:后台服务之响应IntentService的处理结果