添加依赖
根工程:build.gradle
//数据库
classpath 'org.greenrobot:greendao-gradle-plugin:3.3.0' // add plugin
项目工程:build.gradle
apply plugin: 'org.greenrobot.greendao' // apply plugin
implementation 'org.greenrobot:greendao:3.3.0'
make一下项目后
Application初始化数据库
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();
}
新建一个实体类,然后make一下
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;
/**
* Created by wf on 2019/10/7.
*/
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;
}
}