greendao的优点就不说了,直接说怎么用,方便以后查阅,这是我在csdn上的第一篇博客,哈哈
-
第一步,在build.gradle(Project:) 里面添加依赖
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2'
注意:依赖包的版本号要和下面导入的greendao的版本号一致 -
第二步,在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'
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'
}
}
}
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'
})
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'
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 第三步:创建实体类
如图:
这时候运行软件,就会在设置的位置生成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;
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
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<Entity> 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 {
daoSession.delete(entity);
flag = true;
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
/**
*删除所有的实体记录
*/
public boolean deleteAllEntity(Class cls){
boolean flag = false;
try {
daoSession.deleteAll(cls);
flag = true;
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
/**
* 返回多行记录
*
* @return
*/
public List<Entity> listAll() {
return (List<Entity>) daoSession.loadAll(getClass());
}
/**
* 按照主键返回单行记录
*
* @param key
* @return
*/
public Entity listOneStudent(long key) {
return (Entity) daoSession.load(getClass(), key);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
# # ######## 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.修改实体类
重现编译项目运行即可。一般的数据库升级这样就可以了,特殊情况可能需要自己编写数据库迁移脚本。
结语:第一次写博客,写的不好,见谅,如果看不懂可以给我留言