GreenDao使用记录

  1. 配置
greendao {
    schemaVersion 1
    //配置gen目录生成地址
    daoPackage 'com.huahua.testing.greendao.gen'
    targetGenDir 'src/main/java'
}
apply plugin: 'com.android.application'
apply plugin: 'org.greenrobot.greendao'

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1'
    }
}
 
dependencies {
    compile 'org.greenrobot:greendao:3.2.0'
}
  1. 代码初始化
// 在Application 类中配置一次
DaoMaster.DevOpenHelper helper = 
new DaoMaster.DevOpenHelper(this, "notes-db", null);
Database db = helper.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster(db);
DaoSession daoSession = daoMaster.newSession();
  1. 在activities或fragments 使用DAO
NoteDao noteDao = daoSession.getNoteDao();
  1. 新建实体
@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 {
    // Id 用 Long 类型存储
    @Id(autoincrement = true)
    private Long id;
 
    @Index(unique = true)
    private String key;

    @Property(nameInDb = "USERNAME")
    private String name;
 
    @NotNull
    private int repos;
 
    @Transient //临时数据,不存入数据库
    private int tempUsageCount;
 
    @Unique //将会隐式的创建一个index
     private String school;
    ...
    @keep //keep标签会保证这段代码不受生成器的影响
    School school = new School(); 
}

  1. 更新数据库

同步数据库(有就更新 ,没有就插入)

insertOrReplace (不要用save,并没有用)
  1. 我明明标了autoincrement , 按理不用传id的, 也确实没传, 还爆not unique的话, 就是踩坑了

GreenDao的主键必须设置成包装类 Long , 大写L

原因就在这里 : 如果主键设置为包装类Long类型, 那么在生成的Dao类中会有一个判断非null才插入
private Long _id;

@Override
protected final void bindValues(SQLiteStatement stmt, IdCache entity) {
stmt.clearBindings();

Long _id = entity.get_id();
if (_id != null) {
    stmt.bindLong(1, _id);
}
stmt.bindString(2, entity.getUser());

}

但是如果写的是 long, 就无脑写进去了, 也就是说即使不传值,也会insert _id = 0, 于是报异常;
@Id(autoincrement = true)
private long _id;

@Override
protected final void bindValues(DatabaseStatement stmt, IdCache entity) {
stmt.clearBindings();
stmt.bindLong(1, entity.get_id());
stmt.bindString(2, entity.getUser());
}

你可能感兴趣的:(GreenDao使用记录)