微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

案例具体效果如下所示:

这个案例就是利用 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();

}

/**

  • 连接数据库并创建会话
  • @H_12_404@

    */

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

    版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐