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;
}
}