GreenDao 3.3.0 基本使用与入门 (一)

GreenDao 引入

GreenDao官网
GreenDaoAPI地址

GreenDao 3.3.0 基本使用与入门(一)
GreenDao 3.3.0 多表关联使用(二)
GreenDao 3.3.0 增删改查的使用(三)

Porject 目录下 build.gradle 下添加配置

    dependencies {
        classpath 'com.android.tools.build:gradle:4.0.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'//dagger2 apt 配置

        classpath 'org.greenrobot:greendao-gradle-plugin:3.3.0' //add plugin greendao配置
     
    }

接下来在app的 build.gradle 中添加

apply plugin: 'org.greenrobot.greendao' // apply plugin

android{
    ...
       greendao {

        schemaVersion 5  //数据库版本号,升级数据库需要修改版本号

        daoPackage 'com.greendao.dao'  //一般为app包名+生成文件的文件夹名

        targetGenDir 'src/main/java'  //自动生成的greendao代码存放路径

    }
    
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

    implementation 'org.greenrobot:greendao:3.3.0' // add library
    ....
}

这样就完成GreenDao的引入了

Entity 注解详解

@Entity
表明这个实体类会在数据库中生成一个与之相对应的表
属性:

  • schema:告知GreenDao当前实体属于哪个 schema
    
  • schema active:标记一个实体处于活跃状态,活动实体有更新、删除和刷新方法
    
  • nameInDb:在数据库中使用的别名,默认使用的是实体的类名,
    
  • indexes:定义索引,可以跨越多个列
    
  • createInDb:标记创建数据库表(默认:true)
    
  • generateConstructors 自动创建全参构造方法(同时会生成一个无参构造方法)(默认:true)
    
  • generateGettersSetters 自动生成 getters and setters 方法(默认:true)
    
@Entity(
        schema = "myschema",
        active = true,
        nameInDb = "AWESOME_USERS",
        indexes = {
                @Index(value = "name DESC", unique = true)
        },
        createInDb = true,
        generateConstructors = false,
        generateGettersSetters = true
)
public class User {
  ...
}

@Id
对应数据表中的 Id 字段

@Entity
public class UserList {
    @Id(autoincrement = true)//主键自动增长
    private Long id;

@Index
使用@Index作为一个属性来创建一个索引,默认是使用字段名

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

    @Index(unique = true)
    private String name;
}

@OrderBy
更加某一字段排序 ,例如:@OrderBy(“date ASC”)

@OrderBy("date ASC")
private List<Order> orders;

@Property
设置一个非默认关系映射所对应的列名,默认是使用字段名,例如:

@Entity (nameInDb = “User”)
public class User {
@Property(nameInDb = “userName”)
private String userName;

}

@NotNull
设置数据库表当前列不能为空

@Transient
添加此标记后不会生成数据库表的列

@Unique
表名该属性在数据库中只能有唯一值

@Entity 
public class User {
@Id 
private Long id;
@Unique 
private String name;
}
注意事项

由于greenDAO 3.0 生成的字段添加了非空约束。字段的类型设置为基本类型(如:int)默认会添加非空约束,字段类型设置为对象类型(如:Integer)默认不会添加非空约束,而且最终生成的sql会使用对象类型

定义int类型

@Entity
public class CommentList {
    @Id(autoincrement = true)
    private Long id;
    private int se
  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值