Android GreenDAO(一)
阿新 • • 發佈:2018-12-15
GreenDao是一個高效的資料庫訪問ORM框架,節省了自己編寫SQL的時間,快速的增刪查改等操作。
配置GreenDao
新增在工程的build.gradle
// In your root build.gradle file:
buildscript {
dependencies {
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' // add plugin
}
}
新增到module的build.gradle檔案中
// In your app projects build.gradle file: apply plugin: 'com.android.application' apply plugin: 'org.greenrobot.greendao' // apply plugin android { greendao { schemaVersion 1 //版本 // 一般為app包名+生成檔案的資料夾名 daoPackage 'com.example.administrator.mvp.greendao.gen' targetGenDir 'src/main/java' //生成檔案路徑 } } dependencies { compile 'org.greenrobot:greendao:3.2.2' // add library }
建立實體類
@Entity
public class FavorDb {
@Id
private Long id;
@NotNull
private String title;
private long update_time;
private String chapter_name;
private long chapter_id;
private boolean isUnRead;
}
建立完成實體類之後點選Build–>MarkProject,會自動在配置的路徑下生成DaoMaster、DaoSession、以及UserInfoDao檔案(如果擁有多個實體類,則會生成多個實體類Dao檔案),並且,剛才編寫的實體類檔案UserInfo會自動生成get、set方法跟無參構造以及完整引數的構造方法
package com.example.administrator.mvp.model.db; import org.greenrobot.greendao.annotation.Entity; import org.greenrobot.greendao.annotation.Id; import org.greenrobot.greendao.annotation.NotNull; import org.greenrobot.greendao.annotation.Generated; @Entity public class FavorDb { @Id private Long id; @NotNull private String title; private long update_time; private String chapter_name; private long chapter_id; private boolean isUnRead; @Generated(hash = 172177374) public FavorDb(Long id, @NotNull String title, long update_time, String chapter_name, long chapter_id, boolean isUnRead) { this.id = id; this.title = title; this.update_time = update_time; this.chapter_name = chapter_name; this.chapter_id = chapter_id; this.isUnRead = isUnRead; } @Generated(hash = 1955454265) public FavorDb() { } public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } public String getTitle() { return this.title; } public void setTitle(String title) { this.title = title; } public long getUpdate_time() { return this.update_time; } public void setUpdate_time(long update_time) { this.update_time = update_time; } public String getChapter_name() { return this.chapter_name; } public void setChapter_name(String chapter_name) { this.chapter_name = chapter_name; } public long getChapter_id() { return this.chapter_id; } public void setChapter_id(long chapter_id) { this.chapter_id = chapter_id; } public boolean getIsUnRead() { return this.isUnRead; } public void setIsUnRead(boolean isUnRead) { this.isUnRead = isUnRead; } }