GreenDao是一个轻量级且性能优良的ORM框架,本文主要简述如何使用,并作一些简单的封装。
使用步骤:
step1.引入框架以及插件
在项目的build.gradle中
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
在模块的build.gradle中
apply plugin: 'org.greenrobot.greendao'
dependencies {
android {
compileSdkVersion 26
buildToolsVersion "26.0.2"
greendao {
schemaVersion 3 //表示数据库版本号
daoPackage 'com.example.administrator.greendao'//自动生成代码所在包名,默认在build/generated/source/greendao
targetGenDir 'src/main/java' //生成的DAOMaster和DaoSession的位置
}
}
compile 'org.greenrobot:greendao:3.2.0'//引入greenDAO的类库
}
step2.创建用于映射的对象实体VoiceMsg
@Entity
public class VoiceMsg {
@Id
private Long id;
private long time;//时间长度
private String filePath;//文件路径
private float voiceTime;//
private int deriction;//0 send 1 receive
private String name;
}
在安卓studio的工具栏中点击Build—>Make Project或者Make Moudle 插件将会自动帮我们生成文件
image.png
step3.初始化数据库
最好在application中做数据库的初始化
public class BaseApplication extends Application {
private static BaseApplication instances;
private DaoMaster.DevOpenHelper mHelper;
private SQLiteDatabase db;
private DaoMaster mDaoMaster;
private DaoSession mDaoSession;
public static BaseApplication getInstances() {
return instances;
}
@Override
public void onCreate() {
super.onCreate();
instances = this;
initGreenDao();
}
/**
* 设置greenDao
*/
private void initGreenDao() {
// 通过 DaoMaster 的内部类 DevOpenHelper,你可以得到一个便利的 SQLiteOpenHelper 对象。
// 可能你已经注意到了,你并不需要去编写「CREATE TABLE」这样的 SQL 语句,因为 greenDAO 已经帮你做了。
// 注意:默认的 DaoMaster.DevOpenHelper 会在数据库升级时,删除所有的表,意味着这将导致数据的丢失。
// 所以,在正式的项目中,你还应该做一层封装,来实现数据库的安全升级。
mHelper = new DaoMaster.DevOpenHelper(this, "pingantong.db", null);
db = mHelper.getWritableDatabase();
mDaoMaster = new DaoMaster(db);
mDaoSession = mDaoMaster.newSession();
}
public DaoSession getDaoSession() {
return mDaoSession;
}
public SQLiteDatabase getDb() {
return db;
}
}
step4.封装工具类 操作增,删,改,查
消息数据库操作最好封装成一个工具类,方便调用
public class VoiceDbUtil {
private static class VoiceDbUtilHolder {
private static VoiceDbUtil instance = new VoiceDbUtil();
}
public static VoiceDbUtil getInstance() {
return VoiceDbUtilHolder.instance;
}
private VoiceDbUtil() {
}
private VoiceMsgDao getVoiceMsgDao() {
return BaseApplication.getInstances().getDaoSession().getVoiceMsgDao();
}
private SQLiteDatabase getSqlDb() {
return BaseApplication.getInstances().getDb();
}
//增
public boolean insert(VoiceMsg msg) {
boolean flag = false;
try {
getVoiceMsgDao().insert(msg);
flag = true;
} catch (Exception e) {
Log.e("kk", "insert: -----------" + e.getMessage());
}
return flag;
}
/*
* 插入多条数据对象
* 可能会存在耗时 操作 所以new 一个线程
* */
public Boolean insertMultUser(final List<VoiceMsg> msgs) {
boolean flag = false;
try {
BaseApplication.getInstances().getDaoSession().runInTx(new Runnable() {
@Override
public void run() {
for (VoiceMsg msg : msgs) {
insert(msg);
}
}
});
flag = true;
} catch (Exception e) {
e.getStackTrace();
}
return flag;
}
//删
public boolean delete(VoiceMsg msg) {
boolean flag = false;
try {
getVoiceMsgDao().delete(msg);
flag = true;
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
//改
public boolean update(VoiceMsg msg) {
boolean flag = false;
try {
getVoiceMsgDao().update(msg);
flag = true;
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
//查
public List<VoiceMsg> getAll() {
return getVoiceMsgDao().loadAll();
}
/**
* 分页加载 20条
*
* @param offset 页
* @return
*/
public List<VoiceMsg> getTwentyMsg(int offset) {
if (offset < 1) {
throw new IllegalArgumentException("offset need input the number over zero");
}
List<VoiceMsg> listMsg = getVoiceMsgDao().queryBuilder()
.offset((offset - 1) * 20).limit(20).list();
return listMsg;
}
/**
* 类似于微信的从后面加载数据
*
* @param offset
* @return
*/
public List<VoiceMsg> getWXTwentyMsg(int offset) {
List<VoiceMsg> msgs = new ArrayList<>();
List<VoiceMsg> voiceMsgs = getVoiceMsgDao().loadAll();
int size = voiceMsgs.size();
if (size > offset * 20) {
msgs = getVoiceMsgDao().queryBuilder().offset(size - offset * 20).limit(20).list();
} else if (size > (offset - 1) * 20) {
msgs = getVoiceMsgDao().queryBuilder().offset(0).limit(size - (offset - 1) * 20).list();
}
return msgs;
}
}
GreenDao的基本使用就讲到这里了,如果要深入了解最好去看GreenDao官方文档。
网友评论