继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

轻量级的数据库GreenDao的整合

繁华开满天机
关注TA
已关注
手记 134
粉丝 15
获赞 68

数据库的选择

之前在项目中使用的是Xutils里面封装的数据库模块,操作起来也是简便好用,但是由于xutils的注解采用的是反射机制,在性能上不是很友好,于是拿来GreenDao试着玩玩。
GreenDao git首页可以看到一句介绍

greenDAO is a light & fast ORM solution for Android that maps objects to SQLite databases.

妥,本篇不讲GreenDao的实现原理,说明一下配置过程以及遇到的坑。

gradle配置

外层需要全局引入GreenDao的插件

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2'
    }
}

然后在内部的gradle apply以及依赖相关的包

apply plugin: 'com.android.application'//applygreendao插件apply plugin: 'org.greenrobot.greendao'

 compile 'org.greenrobot:greendao:3.2.2'
 compile 'net.zetetic:android-database-sqlcipher:3.5.1@aar'

上面 compile 'net.zetetic:android-database-sqlcipher:3.5.1@aar'的导入也是在配置过程中遇到的坑后面会解释一下为什么加这一句。

greenDao配置及使用

首先在Application中初始化

public class MyApplication extends Application {    public static MyApplication INSTANCE;    public static final boolean ENCRYPTED = true;    private DaoSession mDaoSession;    @Override
    public void onCreate() {        super.onCreate();
        INSTANCE = this;
        DaoMaster.DevOpenHelper help = new DaoMaster.DevOpenHelper(this, ENCRYPTED ? "bear-db-encrypted" : "bear-db");        //此处有坑
        Database db = ENCRYPTED ? help.getEncryptedWritableDb("admin") : help.getWritableDb();
        mDaoSession = new DaoMaster(db).newSession();
    }    public DaoSession getDaoSession() {        return mDaoSession;
    }
}

下面建立一个数据表来试试,那先来个User表来试玩一下。新建UserBean class

@Entity(indexes = {        //index 值是拿表中的account作为索引,且索引是唯一不可重复的
        @Index(value = "account",unique = true)
})public class UserBean {    //声明一个主键值
    @Id
    public long id;    public String account;    public String passWord;    public String mobile;    public String email;
}

建立完之后点一下Android Studio运行左边的小锤子make project后就会预编译出一个比较长的类了。

@Entity(indexes = {        @Index(value = "account",unique = true)
})public class UserBean {    @Id
    public long id;    public String account;    public String passWord;    public String mobile;    public String email;@Generated(hash = 1328729131)public UserBean(long id, String account, String passWord, String mobile,
        String email) {    this.id = id;    this.account = account;    this.passWord = passWord;    this.mobile = mobile;    this.email = email;
}@Generated(hash = 1203313951)public UserBean() {
}public long getId() {    return this.id;
}public void setId(long id) {    this.id = id;
}public String getAccount() {    return this.account;
}public void setAccount(String account) {    this.account = account;
}public String getPassWord() {    return this.passWord;
}public void setPassWord(String passWord) {    this.passWord = passWord;
}public String getMobile() {    return this.mobile;
}public void setMobile(String mobile) {    this.mobile = mobile;
}public String getEmail() {    return this.email;
}public void setEmail(String email) {    this.email = email;
}

}

Ok,表已经建立完毕,现在试着插条数据进去玩玩

 UserBean userBean = new UserBean();
        userBean.setId((long)1);
        userBean.setAccount("18262282215");
        userBean.setPassWord("111111qq");
        userBean.setEmail("bear@berdatata.com");
        userBean.setMobile("18262282215");
        UserBeanDao dao = MyApplication.INSTANCE.getDaoSession().getUserBeanDao();

开始运行后,Crash 没错就是crash,wocao,明明是按照官网的配置一个个来的嘛。折腾几次终于发现少依赖了一个包,就是前面讲的使用加密的数据库需要导入 compile 'net.zetetic:android-database-sqlcipher:3.5.1@aar'。总算是走通了。

原文链接:http://www.apkbus.com/blog-302849-63543.html

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP