greenDao学习了解

参考地址:

https://www.jianshu.com/p/853401a7d02b

https://www.cnblogs.com/zhangqie/p/7250459.html

第一步 先在 项目的Project 的 buil.gradle 里 在buildscript- repositories添加配置mavenCentral(),

在dependencies 里添加
classpath'org.greenrobot:greendao-gradle-plugin:3.1.0'

第二步 在自己想要用的
GreenDao Module 里的 dependencies 里添加

compile'org.greenrobot:greendao:3.1.0'

compile'org.greenrobot:greendao-generator:3.1.0'

android 里 添加

 

greendao{

schemaVersion1  

daoPackage'com.afa.tourism.greendao.gen'

targetGenDir'src/main/java'

}

 

schemaVersion: 数据库schema版本,也可以理解为数据库版本号
daoPackage:设置DaoMaster、DaoSession、Dao包名
targetGenDir:设置DaoMaster、DaoSession、Dao目录
targetGenDirTest:设置生成单元测试目录
generateTests:设置自动生成单元测试用例

头部 添加

applyplugin:'org.greenrobot.greendao'


第三步 创建新的实体类

在实体类 上方 写 上 @Entity 即可!然后导包而且不需要写set get 方法。 他自动帮我们生成.. 是不是很牛逼哦?

 

/** * Created by AFa on 2016/8/23. *

/@Entity

public class User {  

 @Id(autoincrement = true)   

private Long id;   

private String name;   

private String age;   

private String sex;   

private String salary;

}


GreenDao 优点:

1.性能高,号称Android最快的关系型数据库
2.内存占用小
3.库文件比较小,小于100K,编译时间低,而且可以避免65K方法限制
4.支持数据库加密 greendao支持SQLCipher进行数据库加密 有关SQLCipher可以参考这篇博客Android数据存储之Sqlite采用SQLCipher数据库加密实战
5.简洁易用的API


-----------------------------

首先先添加相关配置文件,依赖库

 

 compile 'org.greenrobot:greendao:3.+'

并在库文件顶部添加:

apply plugin: 'org.greenrobot.greendao'

库文件下定义路径

greendao {

    schemaVersion 1//指定数据库版本号,更新操作会用到;

    daoPackage 'com.zhangqie.greendao.gen'//自动生成的dao的包名,包名默认是entity所在的包;

    targetGenDir 'src/main/java'//生成数据库文件的目录

}

在build.gradle文件中添加配置

 

buildscript {

    repositories {

        jcenter()

    }

    dependencies {

        classpath 'com.android.tools.build:gradle:2.2.3'

        classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1'

        // NOTE: Do not place your application dependencies here; they belong

        // in the individual module build.gradle files

    }

}

 

 通过以上操作就完成我们的基本配置了;

创建一个类User

 

@Entity

public class User {

    @Id(autoincrement = true)

    private Long id;//主键  自增长

    @NotNull   //不许为空

    private String name;

    private String age;

    private String content;

}

 

 package fateat.xinkong.com.greendaotest;

import android.app.Application;
import android.database.sqlite.SQLiteDatabase;

import fateat.xinkong.com.greendaotest.greendao.gen.DaoMaster;
import fateat.xinkong.com.greendaotest.greendao.gen.DaoSession;
//import fateat.xinkong.com.greendaotest.util.dao.Helper;

public class MyApplication extends Application {

    private DaoMaster.DevOpenHelper mHelper;
    private SQLiteDatabase db;
    private DaoMaster mDaoMaster;
    private DaoSession mDaoSession;


    public static MyApplication instance;

    public static MyApplication getInstances(){
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        setDatabase();
    }

    private void setDatabase(){
        mHelper =new DaoMaster.DevOpenHelper(this, "notes-db", null);//new Helper(this);//
        db = mHelper.getWritableDatabase();
        // 注意:该数据库连接属于 DaoMaster,所以多个 Session 指的是相同的数据库连接。
        mDaoMaster = new DaoMaster(db);
        mDaoSession = mDaoMaster.newSession();
    }

    public DaoSession getDaoSession() {
        return mDaoSession;
    }
    public SQLiteDatabase getDb() {
        return db;
    }
}
 

 

 

版本升级更新

比如需要在实体类加一个字段 或者 改变字段属性等 就需要版本更新来保存以前的数据了;

我的案例中是加了一个  times的字段;

1:需要一个MigrationHelper.java一位大神写的 直接拿来用即可

地址:https://stackoverflow.com/questions/13373170/greendao-schema-update-and-data-migration/30334668#30334668

 

 

2:接下来就是我们 GreenApplication 类里的 Helper类了

 

public class Helper extends DaoMaster.OpenHelper{

 

    private static DaoMaster daoMaster;

    private static DaoSession daoSession;

 

    public static final String DBNAME = "greendao.db";

 

    public Helper(Context context){

        super(context,DBNAME,null);

    }

 

 

    @Override

    public void onUpgrade(Database db, int oldVersion, int newVersion) {

        super.onUpgrade(db, oldVersion, newVersion);

        Log.i("version", oldVersion + "---先前和更新之后的版本---" + newVersion);

        if (oldVersion < newVersion) {

            Log.i("version", oldVersion + "---先前和更新之后的版本---" + newVersion);

            MigrationHelper.getInstance().migrate(db, UserDao.class);

            //更改过的实体类(新增的不用加)   更新UserDao文件 可以添加多个  XXDao.class 文件

//             MigrationHelper.getInstance().migrate(db, UserDao.class,XXDao.class);

        }

    }

 

    /**

     * 取得DaoMaster

     *

     * @param context

     * @return

     */

    public static DaoMaster getDaoMaster(Context context) {

        if (daoMaster == null) {

            DaoMaster.OpenHelper helper = new DaoMaster.DevOpenHelper(context,

                    DBNAME, null);

            daoMaster = new DaoMaster(helper.getWritableDatabase());

        }

        return daoMaster;

    }

 

    /**

     * 取得DaoSession

     *

     * @param context

     * @return

     */

    public static DaoSession getDaoSession(Context context) {

        if (daoSession == null) {

            if (daoMaster == null) {

                daoMaster = getDaoMaster(context);

            }

            daoSession = daoMaster.newSession();

        }

        return daoSession;

    }

}

 

完成相关操作之后;还要改数据库版本

greendao {

    schemaVersion 2//改版本号为2

    daoPackage 'com.zhangqie.greendao.gen';

    targetGenDir 'src/main/java'

}

编译运行,可以去实体类和UserDao查看  增加的字段相关信息自动生成完成;

当然先前的版本1 中的数据也是存在的,只不过 新添加的字段 的值全部为空而已;


 

 

转载于:https://www.cnblogs.com/liangxy/p/9099742.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值