引入GreenDao
repositories {
google()
jcenter()
mavenCentral() // add repository
}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.0-rc02'
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' // add plugin
apply plugin: 'org.greenrobot.greendao' // apply plugin
implementation 'androidx.recyclerview:recyclerview:1.1.0'// add library
建立实体类
@Entity
public class GoodsModel {
@Id(autoincrement = true)
private Long id;
private Integer goodsId;
private String name;
private String icon;
private String info;
private String type;
然后点击makeproject,编辑器会自己生成一下代码
@Generated(hash = 1834473137)
public GoodsModel(Long id, Integer goodsId, String name, String icon,
String info, String type) {
this.id = id;
this.goodsId = goodsId;
this.name = name;
this.icon = icon;
this.info = info;
this.type = type;
}
@Generated(hash = 971639536)
public GoodsModel() {
}
protected GoodsModel(Parcel in) {
if (in.readByte() == 0) {
id = null;
} else {
id = in.readLong();
}
if (in.readByte() == 0) {
goodsId = null;
} else {
goodsId = in.readInt();
}
name = in.readString();
icon = in.readString();
info = in.readString();
type = in.readString();
}
建立某dao 创建会话
public void initDb () {
// 获取SQLiteOpenHelper对象devOpenHelper
DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(this, "imooc.db");
// 获取SQLiteDatabase
SQLiteDatabase db = devOpenHelper.getWritableDatabase();
// 加密数据库
// Database db = devOpenHelper.getEncryptedWritableDb("123");
// 创建DaoMaster实例
// DaoMaster保存数据库对象(SQLiteDatabase)并管理特定模式的Dao类(而不是对象)。
// 它具有静态方法来创建表或将它们删除。
// 其内部类OpenHelper和DevOpenHelper是在SQLite数据库中创建模式的SQLiteOpenHelper实现。
DaoMaster daoMaster = new DaoMaster(db);
// 管理特定模式的所有可用Dao对象
mSession = daoMaster.newSession();
}
系统会自动生成这些类
建立操作类GreenDaoManager
public class GreenDaoManager {
private Context mContext;
private GoodsModelDao mGoodsModelDao;//表的操作类
public GreenDaoManager(Context context) {
this.mContext = context;
// 获取DAO实例
mGoodsModelDao = Myapplication.mSession.getGoodsModelDao();//获取表的操作类实例 有这个后就可以进行操作了
}
/**
* 添加所有的数据到数据库
*/
public void insertGoods () {
String json = DataUtils.getJson("goods.json", mContext);
// 如果不想因为重复添加数据而导致崩溃,可以使用insertOrReplaceInTx API
// mGoodsModelDao.insertInTx(DataUtils.getGoodsModels(json));
mGoodsModelDao.insertOrReplaceInTx(DataUtils.getGoodsModels(json));
}
/**
* 查询所有的数据
* @return
*/
public List<GoodsModel> queryGoods () {
QueryBuilder<GoodsModel> result = mGoodsModelDao.queryBuilder();
result = result.orderAsc(GoodsModelDao.Properties.GoodsId);
return result.list();
}
/**
* 查询水果的数据
* @return
*/
public List<GoodsModel> queryFruits () {
QueryBuilder<GoodsModel> result = mGoodsModelDao.queryBuilder();
/**
* 借助Property属性类提供的筛选方法
*/
result = result.where(GoodsModelDao.Properties.Type.eq("0")).orderAsc(GoodsModelDao.Properties.GoodsId);
return result.list();
}
/**
* 查询零食的数据
* @return
*/
public List<GoodsModel> querySnacks () {
QueryBuilder<GoodsModel> result = mGoodsModelDao.queryBuilder();
/**
* 借助Property属性类提供的筛选方法
*/
result = result.where(GoodsModelDao.Properties.Type.eq("1")).orderAsc(GoodsModelDao.Properties.GoodsId);
return result.list();
}
/**
* 修改指定商品的商品信息
* @param model
*/
public void updateGoodsInfo (GoodsModel model) {
mGoodsModelDao.update(model);
mGoodsModelDao.updateInTx();
}
/**
* 删除指定商品的商品信息
* @param model
*/
public void deleteGoodsInfo (GoodsModel model) {
mGoodsModelDao.deleteByKey(model.getId());
}
然后就可以根据需要去办事了