greendao的优点就不说了,直接说怎么用,方便以后查阅,这是我在csdn上的第一篇博客,哈哈
第一步,在build.gradle(Project:) 里面添加依赖
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2'
第二步,在build.gradle(Module:app) 里面设置
1.apply plugin: ‘org.greenrobot.greendao’
如图:
2.
//属性介绍:
//schemaVersion–> 指定数据库schema版本号,迁移等操作会用到;
//daoPackage –> dao的包名,包名默认是entity所在的包;
//targetGenDir –> 生成数据库文件的目录;
greendao {
schemaVersion 1
daoPackage ‘com.daydaynote’
targetGenDir ‘src/main/java/com/daydaynote/greendao’
}
如图:
3.导入greendao的包
compile 'org.greenrobot:greendao:3.2.2'
compile 'org.greenrobot:greendao-generator:3.2.2'
如图:
build.gradle(Module:app)实例代码
apply plugin: 'com.android.application'
//使用greendao
apply plugin: 'org.greenrobot.greendao'
android {
compileSdkVersion 24
buildToolsVersion '25.0.0'
defaultConfig {
applicationId "com.example.duanzishou.com.daydaynote"
minSdkVersion 19
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
//属性介绍:
//schemaVersion--> 指定数据库schema版本号,迁移等操作会用到;
//daoPackage --> dao的包名,包名默认是entity所在的包;
//targetGenDir --> 生成数据库文件的目录;
greendao {
schemaVersion 1
daoPackage 'com.daydaynote'
targetGenDir 'src/main/java/com/daydaynote/greendao'
}
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'
})
//Design support库
//轻松实现图片园兴化
//
//声明卡片式布局
//超级强大的图片加载库,用于加载本地,网络,gif。甚至本地视频
//轮播图
//greendao包
compile 'org.greenrobot:greendao:3.2.2'
compile 'org.greenrobot:greendao-generator:3.2.2'
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support:design:24.2.1'
compile 'de.hdodenhof:circleimageview:2.1.0'
compile 'com.android.support:recyclerview-v7:24.2.1'
compile 'com.youth.banner:banner:1.4.9'
compile 'com.android.support:cardview-v7:24.2.1'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
}
这时候运行软件,就会在设置的位置生成DaoMaster DaoSession 还有对应的dao文件
如图:
调用:greenDaoUtil = new DaoUtils(this);
获取到DaoUtils对象后调用里面的方法就行了
Daomanager里面的代码`
package com.bluetoothmicrecord.utils;
import android.content.Context;
import com.bluetoothmicrecord.DaoMaster;
import com.bluetoothmicrecord.DaoSession;
import org.greenrobot.greendao.query.QueryBuilder;
/**
* Created by Koterwong on 2016/7/31.
*/
public class DaoManager {
public static final String TAG = DaoManager.class.getSimpleName();
/**
* 数据库名
*/
private static final String DB_NAME = "my_db.sqlite";
private static DaoManager manager;
private static DaoMaster daoMaster;
private static DaoMaster.DevOpenHelper helper;
private static DaoSession daoSession;
private Context context;
private DaoManager(Context context) {
this.context = context;
}
public static DaoManager getInstance(Context context) {
if (manager == null) {
synchronized (DaoManager.class) {
if (manager == null) {
manager = new DaoManager(context);
}
}
}
return manager;
}
/**
* 判断数据库是否存在,如果没有就创建。
*
* @return 一个DaoMaster就代表着一个数据库的连接
*/
public DaoMaster getDaoMaster() {
if (daoMaster == null) {
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context, DB_NAME, null);
daoMaster = new DaoMaster(helper.getWritableDatabase());
}
return daoMaster;
}
/**
* 完成数据的添加、删除、修改、查询的操作
*
* @return DaoSession完成对Entity的基本操作和Dao操作类
*/
public DaoSession getDaoSession() {
if (daoSession == null) {
if (daoMaster == null) {
daoMaster = getDaoMaster();
}
daoSession = daoMaster.newSession();
}
return daoSession;
}
/**
* 设置Debug开启,默认关闭
*/
public void setDebug() {
QueryBuilder.LOG_SQL = true;
QueryBuilder.LOG_VALUES = true;
}
/**
* 关闭数据库连接,数据库开启的时候,使用完毕了必须要关闭
*/
public void closeConnection() {
closeHelper();
closeDaoSession();
}
public void closeHelper() {
if (helper != null) {
helper.close();
helper = null;
}
}
public void closeDaoSession() {
if (daoSession != null) {
daoSession.clear();
daoSession = null;
}
}
}
DaoUtils里面的代码
package com.daydaynote.uitls.greendao;
import android.content.Context;
import android.util.Log;
import com.daydaynote.entity.DaoSession;
import java.util.List;
/**
* Created by Koterwong on 2016/7/31.
*/
public class DaoUtils<Entity> {
private static final String TAG = "DaoUtils";
private final DaoSession daoSession;
public DaoUtils(Context context) {
DaoManager manager = DaoManager.getInstance(context);
daoSession = manager.getDaoSession();
}
/**
* entity 表的插入操作
*
* @param entity 实体类
* @return
*/
public boolean insertEntity(Entity entity) {
boolean flag = false;
flag = daoSession.insert(entity) != -1 ? true : false;
Log.i("CommonUtils", "----insertStudent--result is -->>" + flag);
return flag;
}
/**
* 插入多条记录,需要开辟新的线程
*
* @param entitys
* @return
*/
public boolean insertMultEntity(final List entitys) {
boolean flag = false;
try {
daoSession.runInTx(new Runnable() {
@Override
public void run() {
for (Entity entity : entitys) {
daoSession.insertOrReplace(entity);
}
}
});
flag = true;
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
/**
* 完成对entity的某一条记录的修改
*
* @param entity
* @return
*/
public boolean updateStudent(Entity entity) {
boolean flag = false;
try {
daoSession.update(entity);
flag = true;
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
/**完成对entity的某一条记录的删除
* @param entity
* @return
*/
public boolean delete_Entity (Entity entity) {
boolean flag = false;
try {
//按照指定的id进行删除 delete from student where _id = ?
daoSession.delete(entity);
//manager.getDaoSession().deleteAll();//删除所有的记录
flag = true;
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
/**
*删除所有的实体记录
*/
public boolean deleteAllEntity(Class cls){
boolean flag = false;
try {
//按照指定的id进行删除 delete from student where _id = ?
daoSession.deleteAll(cls);
//manager.getDaoSession().deleteAll();//删除所有的记录
flag = true;
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
/**
* 返回多行记录
*
* @return
*/
public List listAll() {
return (List) daoSession.loadAll(getClass());
}
/**
* 按照主键返回单行记录
*
* @param key
* @return
*/
public Entity listOneStudent(long key) {
return (Entity) daoSession.load(getClass(), key);
}
// /**
// * 按条件查询
// */
// public void query3() {
//
// //逻辑与 和 逻辑或 是双目运算符
// QueryBuilder builder =daoSession.queryBuilder(Student.class);
// //select * from student where (address='北京' or age > 50) and name like '%张%'
// builder.whereOr(StudentDao.Properties.Address.eq("北京"), StudentDao.Properties.Age.ge(50));
// builder.whereOr(StudentDao.Properties.Id.ge(2), StudentDao.Properties.Age.ge(10)).limit(3);//区前三条数据
//
// //builder.where(StudentDao.Properties.Name.like("张"));
// List list = builder.list();
// Log.i("--->>", "" + list);
// }
}
# # ######## greenDao混淆 ##########
#greendao3.2.0,此是针对3.2.0,如果是之前的,可能需要更换下包名
# # -------------------------------------------
-keep class freemarker.** { *; }
-dontwarn freemarker.**
-keep class org.greenrobot.greendao.**{*;}
-dontwarn org.greenrobot.greendao.**
-keepclassmembers class * extends org.greenrobot.greendao.AbstractDao {
public static java.lang.String TABLENAME;
}
-keep class **$Properties
首先在module的gradle文件中修改版本号:
//这里改为最新的版本号
schemaVersion 2
2.修改实体类
重现编译项目运行即可。一般的数据库升级这样就可以了,特殊情况可能需要自己编写数据库迁移脚本。
结语:第一次写博客,写的不好,见谅,如果看不懂可以给我留言