Android ORM 框架 GreenDao 使用详解

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;

其中 @EntitygreenDAO 的实体注解(用于标识当前实体需要 GreenDao 生成代码)。

@Id 是主键 idLong 类型,可以通过 @Id(autoincrement = true) 设置自动增长(自动增长主键不能用基本类型 long,只能用包装类型 Long)。

@Index(unique = true) 是向数据库添加了唯一约束。

5.1.3、自动生成实体类代码

写完上面实体类代码之后,接下来实体类代码的生成就交给 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 目录下生成三个文件 DaoMasterDaoSessionXXXDao。利用这三个文件我们就可以操作数据库了,如下所示:

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、具体使用(增删改查)


准备工作做完之后,使用起来就非常简单了,只需要调用 greenDAOAPI 就可以了。要想操作数据库,我们首先要获取 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));

}

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、删除数据

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助

因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
oid工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助**。

因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

[外链图片转存中…(img-FYPAWLEZ-1715328946605)]

[外链图片转存中…(img-SWvlpCEi-1715328946606)]

[外链图片转存中…(img-wzBm2XiE-1715328946607)]

[外链图片转存中…(img-Bk5slNDe-1715328946607)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值