ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Android ORM 框架 GreenDao 使用详解,Android攒了一个月的面试题及解答

2022-01-27 13:03:01  阅读:155  来源: 互联网

标签:面试题 String dest GreenDao private id goodsId Android public


======================================================================

案例具体效果如下所示:

这个案例就是利用 greenDAO 对商品进行增删改查。

点击插入数据,就会把所有的数据保存到数据库中,点击查询所有数据,会将数据库中的所有数据查询出来,并显示在界面上,同时也可以根据条件进行查询,当点击查询零食类时,只会将零食查询出来显示在桌面上,点击单个商品,会跳转到商品详情页,在商品详情页可以对商品的描述进行修改,同时也可以删除商品。

商品的列表显示是用 RecyclerView 进行实现的。

五、greenDAO 使用步骤

=============================================================================

5.1、准备工作


5.1.1、引入 greenDAO

greenDAO 的引入非常简单,我们只要按照 github 文档 上去做就可以了,具体如下所示:

5.1.2、创建实体类

@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;

其中 @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);

先要获取 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);

标签:面试题,String,dest,GreenDao,private,id,goodsId,Android,public
来源: https://blog.csdn.net/m0_66145060/article/details/122715897

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有