版权声明:本文为博主原创文章,未经博主允许不得转载。讨论QQ群:372702757 https://blog.csdn.net/wenwen091100304/article/details/61453136
引言
前面两篇文章,介绍了greenDao的集成方法与基本注解,具备了基本的基础知识,就可以开始进行实践了,本篇博客介绍,我是如何在项目中使用greenDao进行数据存储的,高手勿喷。
这个项目是自己在学习过程中的一个小项目,主要是想实践一下Android系统的中闹钟的开发,在这个过程中用到了对创建的闹钟做数据库存储的问题,所以这里就以这个为例,分享一下我的实践过程。
集成
集成的方式和之前写的关于集成的文章是一致的,所以这里不再赘述,不知道的朋友点击这里
实践步骤
第一步:初始化DaoSession
这里我写了一个工具类GreenDaoUtils
,在里面写了初始化的方法,并在Application的onCreate方法中调用,示例代码如下:
GreenDaoUtils:
public class GreenDaoUtils {
private static DaoSession mDaoSession;
private static boolean isInit = false;
private GreenDaoUtils(){
}
/**
* 初始化数据库
* @param context
*/
public static void init(Context context){
if(mDaoSession == null){
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context,"clock_db");
Database database = helper.getWritableDb();
mDaoSession = new DaoMaster(database).newSession();
isInit = true;
}
}
/**
* 获取数据库会话实例
* @return
*/
public static DaoSession getmSession(){
if(!isInit){
System.out.println("The GreenDaoUtils has not init");
}
return mDaoSession;
}
}
在MyApplication
中调用,如下:
@Override
public void onCreate() {
super.onCreate();
instance = this;
GreenDaoUtils.init(this);
}
创建对应数据表的实体
在闹钟项目中,我们需要创建一个闹钟的实体,用于存储闹钟对象,示例代码如下:
Clock:
@Entity(
active = true
)
public class Clock {
@Id
private Long id;
@Transient
public static final long KeepTime = 1000*60*2; //默认持续2分钟
private String title;
private String hour;
private String minute;
private boolean repeate;
private boolean close;
private boolean mon;
private boolean tues;
private boolean wed;
private boolean thurs;
private boolean fri;
private boolean sat;
private boolean sun;
public void setId(Long id) {
this.id = id;
}
public Long getId() {
return this.id;
}
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
public String getHour() {
return this.hour;
}
public void setHour(String hour) {
this.hour = hour;
}
public String getMinute() {
return this.minute;
}
public void setMinute(String minute) {
this.minute = minute;
}
public boolean getRepeate() {
return this.repeate;
}
public void setRepeate(boolean repeate) {
this.repeate = repeate;
}
public boolean getMon() {
return this.mon;
}
public void setMon(boolean mon) {
this.mon = mon;
}
public boolean getTues() {
return this.tues;
}
public void setTues(boolean tues) {
this.tues = tues;
}
public boolean getWed() {
return this.wed;
}
public void setWed(boolean wed) {
this.wed = wed;
}
public boolean getThurs() {
return this.thurs;
}
public void setThurs(boolean thurs) {
this.thurs = thurs;
}
public boolean getFri() {
return this.fri;
}
public void setFri(boolean fri) {
this.fri = fri;
}
public boolean getSat() {
return this.sat;
}
public void setSat(boolean sat) {
this.sat = sat;
}
public boolean getSun() {
return this.sun;
}
public void setSun(boolean sun) {
this.sun = sun;
}
public boolean getClose() {
return this.close;
}
public void setClose(boolean close) {
this.close = close;
}
}
完成这一步之后,需要make project
,然后由greenDao自动生成我们的ClockDao。
在应用中执行Clock的增删改查
greenDao对于增删改查提供了多种方法,每一种方法的应用要结合具体的业务场景,这里不再一一罗列。罗列了几个常用的:
/**新增**/
GreenDaoUtils.getmSession().getClockDao().save(clock);
/**删除**/
GreenDaoUtils.getmSession().getClockDao().delete(clock);
GreenDaoUtils.getmSession().getClockDao().deleteByKey(1231L);
/**查找**/
GreenDaoUtils.getmSession().getClockDao().load(1231L);
/**更新**/
GreenDaoUtils.getmSession().getClockDao().update(clock);
结尾
GreenDao的知识不止于此,需要在实践中不断地学习,也会遇见很多其他问题:
1.实体类变更了,重新build,表结构怎么没变更?
答:需要修改数据库的版本号,再次build才可以。
整体感觉GreenDao的使用有点类似web中的hibernate
,省去了很多我们写SQL语句的环节,大大提高了开发效率 ,用于做项目中的数据库存储框架时很不错的!