导包
project的gradle:
buildscript { repositories { jcenter() //1.greenDAO的远程仓库 mavenCentral() }
dependencies { classpath 'com.android.tools.build:gradle:2.3.3' //2.greenDAO的添加的插件 classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' }
}
allprojects { repositories { jcenter() }
}
task clean(type: Delete) { delete rootProject.buildDir }
module的gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion "26.0.0"
defaultConfig {
applicationId "com.junx.greendao"
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
apply plugin: 'com.android.application'
apply plugin: 'org.greenrobot.greendao'
greendao {
schemaVersion 1
daoPackage 'com.koma.greendao.gen'
targetGenDir 'src/main/java'
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
compile 'org.greenrobot:greendao:3.1.0'
}
初始化实体类
@Entity
public class User {
@Id(autoincrement = true)
private Long id;
@Property(nameInDb = "USERNAME")
private String username;
@Property(nameInDb = "NICKNAME")
private String nickname;
@Transient
private String useless;
}
然后点击Build>>make project/module:自动生成代码
@Entity
public class User {
@Id(autoincrement = true)
private Long id;
@Property(nameInDb = "USERNAME")
private String username;
@Property(nameInDb = "NICKNAME")
private String nickname;
@Transient
private String useless;
@Generated(hash = 523935516)
public User(Long id, String username, String nickname) {
this.id = id;
this.username = username;
this.nickname = nickname;
}
@Generated(hash = 586692638)
public User() {
}
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getNickname() {
return this.nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
}
在代码中初始化
public class MyApplcation extends Application {
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
DevOpenHelper helper = new DevOpenHelper(this, "test.db", null);
Database db = helper.getWritableDb();
DaoMaster master = new DaoMaster(db);
daoSession = master.newSession();
}
static MyApplcation mInstance;
public static MyApplcation getInstance() {
return mInstance;
}
private DaoSession daoSession;
public DaoSession getDaoSession() {
return daoSession;
}
}
增删改查
获取Dao对象
//通过DaoSession.getXXXDao()获取对应的数据库Dao对象
userDao = MyApplcation.getInstance().getDaoSession().getUserDao()
增加数据
userDao.insert(new User());
userDao.insertInTx(Iterator);
userDao.insertOrReplace();
userDao.insertOrReplaceInTx();
查询数据
//查询全部user
List users = userDao.loadAll()
//查询特定id的user
User user = userDao.load(Id)
//查询特定条件的数据
//多个条件相与
Query query = userDao.queryBuilder()
.where(UserDao.Properties.Nickname.eq("小花"),UserDao.Properties.Username.eq("小胖"))
.build()
List users2 = query.list()
//多个条件相或
List users3 = userDao.queryBuilder()
.whereOr(UserDao.Properties.Nickname.eq("小花")
, UserDao.Properties.Username.eq("小胖"))
.build().list()
//查询结果进行升序或降序排序
List users5 = userDao.queryBuilder()
.orderAsc(UserDao.Properties.Id)
.build().list()
userDao.queryBuilder().orderAsc(UserDao.Properties.Id)
.build().list()
//限制查询数量
List user4 = userDao.queryBuilder().limit(5).build().list()
//查询数据库数据的总数
long count = userDao.count()
删除数据
userDao.deleteByKey(user.getId());
userDao.deleteByKeyInTx();
userDao.delete(new User());
userDao.updateInTx();
userDao.deleteAll();
升级数据库
当数据的结构变化时,就不得不升级数据库了
module的gradle中:
greendao {
schemaVersion 2
daoPackage 'com.koma.greendao.gen'
targetGenDir 'src/main/java'
}