您的位置:首页 > 移动开发 > Android开发

GreenDao官方文档翻译(三)之模型化实体-Schema & Annotations

2017-10-22 12:40 441 查看

模型化实体-Schema & Annotations

为了在项目中使用greenDao,你应该在应用中创建表示存储数据的实体模型。然后基于这个模型greenDao产生相应的Dao类。这个模型的定义是基于Annotation的。为了产生代码,你应该通过Schema来使用Generator产生相应的代码。

Schema

你可以通过greenDAO Gradle plugin来配置,其中你至少要考虑设置Schema版本:

// In the build.gradle file of your app project:
android {
...
}

greendao {
schemaVersion 2
}


其次,greendao支持的配置元素选项有:

schemaVersion:当前数据库版本。这个元素用于*OpenHelpers 类来整合不同的Schema版本。如果你想改变你的实体/数据库的Schema,这个值必须增加。其默认值为1。

daoPackage:这个包名用于产生的DAOs、DaoMaster及DaoSession。默认的是你的实体源的包名。

**targetGenDir:**daoPackage所在的位置。默认的文件夹是build目录(build/generated/source/greendao)。

generateTests:设置自动产生unit测试为true。

targetGenDirTests:产生的unit测试被存储的目录。默认位置是src/androidTest/java

Entities & Annotations

greenDAO 3 使用Annotations来定语Schema和Entities:

@Entity
public class User {
@Id
private Long id;

private String name;

@Transient
private int tempUsageCount; // not persisted

// getters and setters for id and user ...
}


其中,@Entity annotation转换java类为数据库存储类型实体,并引导greenDAO产生需要的代码(如DAOs)。

Note:仅支持java代码。如果你想要在Kotlin等语言下使用,你的实体类必须是java语言。

@Entity Annotation

由上面的例子可以看出,@Entity Annotation标记一个java类为greenDAO存储型实体。

虽然通常不配置任何多余的参数可以正常运行,但是你仍然可以对@Entity配置:

@Entity(
// If you have more than one schema, you can tell greenDAO
// to which schema an entity belongs (pick any string as a name).
schema = "myschema",

// Flag to make an entity "active": Active entities have update,
// delete, and refresh methods.
active = true,

// Specifies the name of the table in the database.
// By default, the name is based on the entities class name.
nameInDb = "AWESOME_USERS",

// Define indexes spanning multiple columns here.
indexes = {
@Index(value = "name DESC", unique = true)
},

// Flag if the DAO should create the database table (default is true).
// Set this to false, if you have multiple entities mapping to one table,
// or the table creation is done outside of greenDAO.
createInDb = false,

// Whether an all properties constructor should be generated.
// A no-args constructor is always required.
generateConstructors = true,

// Whether getters and setters for properties should be generated if missing.
generateGettersSetters = true
)
public class User {
...
}


Note:多Schema在使用Gradle Plugin时当前版本还不支持。

基本属性

@Entity
public class User {
@Id(autoincrement = true)
private Long id;

@Property(nameInDb = "USERNAME")
private String name;

@NotNull
private int repos;

@Transient
private int tempUsageCount;

...
}


@Id注解可以选择long/ Long类型作为实体ID。在数据库中,它是主键。autoincrement参数是用于使ID值自增的标记(不会使用老的值)。

@Property通过映射用于定义非默认列名。如果确实,greenDao会使用SQL风格的字段名称(将小写转为大写,大写前面加下划线)。

@NotNull使在数据库中该列不能为空。通常用于标记基本数据类型(long, int, short, byte),也可以用于基本数据类型的包装类(Long, Integer, Short, Byte)。

@Transient用于排除不需要存储入数据库的属性。通常该属性用于存储临时状态。当然你可以属于java中的transient关键字。

主键约束

当前实体必须有long或Long类型的属性作为主键。这是Android和SQLite所推荐的方式。

为了解决这个问题,你可以定义一个附加的key属性,但需要创建一个唯一的索引:

@Id
private Long id;

@Index(unique = true)
private String key;


属性索引

在属性上使用@Index可以为相应的数据库列添加数据库索引。使用以下参数来自定义:

name:如果你不喜欢greenDAO默认所生成的索引,你可以通过这个参数来设置。

unique:对索引添加UNIQUE约束,来强制使所有值为唯一值

@Entity

public class User {

@Id private Long id;

@Index(unique = true)

private String name;

}

@Unique 用于对数据库列添加了UNIQUE约束。注意,SQLite内部创建了索引。

@Entity
public class User {
@Id private Long id;
@Unique private String name;
}


默认值

greenDAO试图使用合理的默认值来工作,所以开发人员不必要配置所有的属性。

例如,数据库中表名和列名源于实体和属性的名称。默认的数据库中使用名称为下划线分割的大写名称而不是java风格的驼峰命名法。

关联

你可以看来学习怎样添加to-one和to-many关联方法。

生成代码

一旦配置好Schema,你可以通过“Make Project”或执行greendao Gradle任务来产生相关代码。

当你因改变实体类后发生错误,试着重新构建你的项目以确保旧的生成类被清理。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据库 greenDAO Android