写完上面实体类代码之后,接下来实体类代码的生成就交给 Android Studio 编译器就可以了,首先我们点击菜单栏中 Build 然后点击 Make Project,等待编译器编译完就可以了,编译完后实体类代码如下所示:(这里实现了 Parcelable 接口是为了在 Activity 之间传递实体类,实现接口的方法一直 Alt + Enter 就可以了)
@Entity
public class GoodsModel implements Parcelable {
@Id(autoincrement = true)
private Long id;
@Index(unique = true)
private Integer goodsId;
private String name;
private String icon;
private String info;
private String type;
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();
}
@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() {
}
public static final Creator CREATOR = new Creator() {
@Override
public GoodsModel createFromParcel(Parcel in) {
return new GoodsModel(in);
}
@Override
public GoodsModel[] newArray(int size) {
return new GoodsModel[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
if (id == null) {
dest.writeByte((byte) 0);
} else {
dest.writeByte((byte) 1);
dest.writeLong(id);
}
if (goodsId == null) {
dest.writeByte((byte) 0);
} else {
dest.writeByte((byte) 1);
dest.writeInt(goodsId);
}
dest.writeString(name);
dest.writeString(icon);
dest.writeString(info);
dest.writeString(type);
}
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public Integer getGoodsId() {
return this.goodsId;
}
public void setGoodsId(Integer goodsId) {
this.goodsId = goodsId;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getIcon() {
return this.icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public String getInfo() {
return this.info;
}
public void setInfo(String info) {
this.info = info;
}
public String getType() {
return this.type;
}
public void setType(String type) {
this.type = type;
}
}
点击编译后,编译器不仅会为我们自动完成实体类代码的生成,还会在 build 目录下生成三个文件 DaoMaster,DaoSession,XXXDao。利用这三个文件我们就可以操作数据库了,如下所示:
5.1.4、初始化 greenDAO
我这里是在 Application 里面初始化(注意要在清单文件里面引用,否则 Application 不生效),并提供一个getDaoSession() 的方法供外部使用,具体代码如下:
public class MyApplication extends Application {
public static DaoSession mSession;
@Override
public void onCreate() {
super.onCreate();
initDb();
}
/**
*/
public void initDb() {
// 1、获取需要连接的数据库
DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(this, “test.db”);
SQLiteDatabase db = devOpenHelper.getWritableDatabase();
// 2、创建数据库连接
DaoMaster daoMaster = new DaoMaster(db);
// 3、创建数据库会话
mSession = daoMaster.newSession();
}
// 供外接使用
public DaoSession getDaoSession() {
return mSession;
}
}
5.2、具体使用(增删改查)
准备工作做完之后,使用起来就非常简单了,只需要调用 greenDAO 的 API 就可以了。要想操作数据库,我们首先要获取 DAO 实例,我们创建一个 GreenDaoManager 类来专门管理数据库的操作,具体代码如下所示:
public class GreenDaoManager {
private Context mContext;
private GoodsModelDao mGoodsModelDao;
public GreenDaoManager (Context context) {
this.mContext = context;
// 获取DAO实例
mGoodsModelDao = MyApplication.getDaoSession().getGoodsModelDao();
}
}
5.2.1、新增数据
// 添加一个实体
DAO.insert(T entity);
// 添加多个实体
DAO.insertInTx(T… entities);
…
// 插入数据
public void insertGoods () {
String json = DataUtils.getJson(“goods.json”, mContext);
mGoodsModelDao.insertOrReplaceInTx(DataUtils.getGoodsModels(json));
}
[](
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
https://blog.csdn.net/u012165769/article/details/109267137)5.2.2、查询数据
1)、查询所有
DAO.loadAll();
// 查询所有数据
public List queryGoods () {
return mGoodsModelDao.loadAll();
}
2)、根据主键查询
DAO.load(Long key);
3)、利用 QueryBuilder 与 properties 设置查询条件
// 查询水果的数据
public List queryFruits () {
QueryBuilder result = mGoodsModelDao.queryBuilder();
//借助Property属性类提供的筛选方法
result = result.where(GoodsModelDao.Properties.Type.eq(“0”)).orderAsc(GoodsModelDao.Properties.GoodsId);
return result.list();
}
5.2.3、更新数据
DAO.update(T entity);
DAO.updateInTx(T… entities);
…
// 修改指定商品的商品信息
public void updateGoodsInfo (GoodsModel model) {
mGoodsModelDao.update(model);
}
5.2.3、删除数据
DAO.delete(T entity);
DAO.deleteAll();
DAO.deleteByKey(K key);
…
// 删除指定商品
public void deleteGoodsInfo (GoodsModel model) {
mGoodsModelDao.deleteByKey(model.getId());
}
5.2.4、GreenDaoManager 完整代码
public class GreenDaoManager {
private Context mContext;
private GoodsModelDao mGoodsModelDao;
public GreenDaoManager (Context context) {
this.mContext = context;
|