0
点赞
收藏
分享

微信扫一扫

Android集成GreenDao数据库


数据持久化就是指将那些内存中的瞬时数据保存到存储设备中,保证即使在手机或电脑关机的情况下,这些数据仍然不会丢失。保存在内存中的数据是处于瞬时状态的,而保存在存储设备中的数据是处于持久状态的,持久化技术则提供了一种机制可以让数据在瞬时状态和持久状态之间进行转换。
目前,Android系统中提供了3种方式的数据持久化技术,即文件存储、SharedPreferences存储以及数据库存储。当然,除了这3种方式之外,你还可以将数据保存在手机的SD卡中,不过使用文件、Shared Preferences或数据库来保存数据会相对更简单一些,而且比起将数据保存在SD卡中会更加地安全。Shared Preferences通常用在轻量级的数据存储场景中,比如账号/密码的存储,而数据库则用在数据量比较大的场景中,比如聊天数据的存储。

现在,使用数据库存储时候,一般都会使用一些第三方ORM框架,比如GreenDao。在Android开发中,集成Greendao通常需要如下几步:

  1. 首先,在项目的build.gradle文件中添加依赖:

 classpath 'org.greenrobot:greendao-gradle-plugin:3.3.0' // add plugin

  1. 然后,在app/build.gradle文件中添加如下依赖:

apply plugin: 'org.greenrobot.greendao' // apply plugin
implementation 'org.greenrobot:greendao:3.3.0'

为了方便操作GreenDao数据库,我们还需要对其进行封装。首先,我们创建一个实体对象:

package com.yufulife.xj.model;

import com.yufulife.xj.bean.CameraTakeBean;
import com.yufulife.xj.bean.JoinTakeBean;
import com.yufulife.xj.bean.ListSubCachedBeanConverter;

import org.greenrobot.greendao.annotation.Convert;
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Generated;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Unique;

import java.util.HashMap;
import java.util.List;


@Entity
public class People {
//不能用int
@Id(autoincrement = true)
private Long id;


//巡检编号
@Unique
private String inspection_id="";


private String content="";

@Convert(converter = ListSubCachedBeanConverter.class, columnType = String.class)
private List<JoinTakeBean> mImgTT;

@Generated(hash = 574353758)
public People(Long id, String inspection_id, String content, List<JoinTakeBean> mImgTT) {
this.id = id;
this.inspection_id = inspection_id;
this.content = content;
this.mImgTT = mImgTT;
}

@Generated(hash = 1406030881)
public People() {
}

public Long getId() {
return this.id;
}

public void setId(Long id) {
this.id = id;
}

public String getInspection_id() {
return this.inspection_id;
}

public void setInspection_id(String inspection_id) {
this.inspection_id = inspection_id;
}

public String getContent() {
return this.content;
}

public void setContent(String content) {
this.content = content;
}

public List<JoinTakeBean> getMImgTT() {
return this.mImgTT;
}

public void setMImgTT(List<JoinTakeBean> mImgTT) {
this.mImgTT = mImgTT;
}
}

然后再封装一个统一的管理类:

package com.yufulife.xj.model;

import android.util.Log;

import com.google.gson.Gson;
import com.yufulife.xj.App;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.ArrayList;
import java.util.List;


public class FaceMsgDao {

private static final String TAG = "ClockInfoDao";

/**
* 添加数据,如果有重复则覆盖
*
* @param people
*/
public static void insertData(People people) {
long l = App.getmDaoSession().getPeopleDao().insertOrReplace(people);
System.out.println("liu:::::: "+l);
}

public static long getId(People people) {

return App.getmDaoSession().getPeopleDao().getKey(people);
}

public static People getPeopleWhere(String Inspection_id){
List<People> list = App.getmDaoSession().getPeopleDao().queryBuilder().where(PeopleDao.Properties.Inspection_id.eq(Inspection_id)).list();
if (list!=null){
if (list.size()>=1){
return list.get(0);
}
}
return null;
}
public static void deletePeopleWhere(String Inspection_id){
App.getmDaoSession().getPeopleDao().queryBuilder().where(PeopleDao.Properties.Inspection_id.eq(Inspection_id)) .buildDelete().executeDeleteWithoutDetachingEntities();

}
/**
* 更新数据
*
* @param people
*/
public static void updateData(People people) {
App.getmDaoSession().getPeopleDao().update(people);
}
/**
* 查询全部数据
*/
public static List<People> queryAll() {
return App.getmDaoSession().getPeopleDao().loadAll();
}

public static void systemOutPeopleAll(){
List<People> people = queryAll();
for (People people1:people){
System.out.println("liu::all "+new Gson().toJson(people1));
}
}


public static People getPeople(String war) {
List<People> clockInfos = queryAll();
if (clockInfos==null){
System.out.println("liu::: "+"沒有找到数据");
return null;
}

for (People info : clockInfos) {
if (info.getInspection_id() .equals(war) ) {
return info;
}
}
System.out.println("liu::: "+"沒有找到数据");
return null;
}

}

需要注意的是,在使用GreenDao数据库之前,需要先在项目中初始化,比如。

 private static DaoSession mDaoSession;
public static DaoSession getmDaoSession() {
return mDaoSession;
}
/**
* 初始化数据库
*/
private void setDataBaseData() {
//创建数据库shop.db"
DaoMaster.DevOpenHelper mDaoMaster = new DaoMaster.DevOpenHelper(this, "inspection.db", null);
//获取可写的数据库
SQLiteDatabase writableDatabase = mDaoMaster.getWritableDatabase();
//获取数据库对象
DaoMaster daoMaster = new DaoMaster(writableDatabase);
//获取Dao对象管理者
mDaoSession = daoMaster.newSession();
}

最后,只需要在业务中使用FaceMsgDao操作数据即可。


举报

相关推荐

0 条评论