源代码地址 https://github.com/greenrobot/greenDAO
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1'
}
}
apply plugin: 'com.android.application'
apply plugin: 'org.greenrobot.greendao'
greendao{
schemaVersion 1
daoPackage 'com.xxx.video.db' //生成DaoMaster、DaoSession、Dao路径
targetGenDir 'src/main/java/' //生成DaoMaster、DaoSession、Dao目录
}
dependencies { compile 'org.greenrobot:greendao:3.2.0' }
@Entity
public class User {
@Id
private Long id;
private String name;
@Transient
private int tempUsageCount;
}
点击Build-->Make Project
在com.xxx.video.db下会生成 DaoMaster DaoSession 和上面新建User类的DAO UserDao类
在Application初始化
private DaoMaster mDaoMaster;
private DaoSession mDaoSession;
DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(AppApplication.getContext(), "video-db", null);//此处为自己需要处理的表
mDaoMaster = new DaoMaster(devOpenHelper.getWritableDatabase());
mDaoSession = mDaoMaster.newSession();
public static DaoSession getDaoSession() {
return mDaoSession;
}
调用方法
AppApplication.getDaoSession().getUserDao();
根据条件查询
User mUser=AppApplication.getDaoSession().getUserDao().queryBuilder().where(UserDao.Properties.name.eq("yu")).unique(); 查询name等于yu的用户
List<User> mList=AppApplication.getDaoSession().getUserDao().queryBuilder().where(UserDao.Properties.name.eq("yu")).list(); 查询name等于yu的用户列表