Android数据库GreenDao的使用

最近公司的项目中需要使用到数据库,使用里GreenDao的数据库后,觉得非常好用,速度快,轻量级,代码简单,省略里写增删改查的代码,今天分享给大家。

认识GreenDao之前必须知道ORM(Object Relation Mapping对象关系映射),其表现形式就是通过GreenDao将数据库和Bean对象关联起来,其表现形式如下图

具体的实现过程如下:

1.添加依赖 

(1)需要在项目(Project)的build.gradle中加入依赖:

buildscript {

repositories {

jcenter()

}

dependencies {

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

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

// in the individual module build.gradle files

//GreenDao3.2依赖

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

}

}

(2)在Moudle的build.gradle文件中添加依赖:

//使用greendao

applyplugin:'org.greenrobot.greendao'

添加依赖(dependencies)中:

//greendao3.2的依赖

compile'org.greenrobot:greendao:3.2.0'

(3)添加greendao的数据库配置:

greendao {

schemaVersion 1  //数据库版本号 每次升级数据库都需要改变版本,只能增加

daoPackage  'com.pacgename.greendao.gen'   //设置DaoMaster、DaoSession、Dao包名

targetGenDir  'src/main/java'    //设置DaoMaster、DaoSession、Dao目录

//targetGenDirTest:设置生成单元测试目录

//generateTests:设置自动生成单元测试用例

}

2.创建Entity类(相当于开发中的Bean类)

greendao采用注解来创建Entity类,创建完成之后build自己的Moudlle,会自动生成:

(1)Bean实体的构造方法和get、set方法

(2)DaoMaster、DaoSession、DAOS类  这里的类生成我们在Moudle的build.gradle中设置的

daoPackage路径

这里对Bean对象的注释进行解释

@Entity:告诉GreenDao该对象为实体,只有被@Entity注释的Bean类才能被dao类操作

@Id:对象的Id,使用Long类型作为EntityId,否则会报错。(autoincrement = true)表示主键会自增,如果false就会使用旧值

@Property:可以自定义字段名,注意外键不能使用该属性

@NotNull:属性不能为空

@Transient:使用该注释的属性不会被存入数据库的字段中

@Unique:该属性值必须在数据库中是唯一值

@Generated:编译后自动生成的构造函数、方法等的注释,提示构造函数、方法等不能被修改

3.创建数据库

在项目的Application的oncreate方法中,创建数据库。

DaoManager.getInstance().setupDatabase(this)

其中DaoManager在后面创建。

4.配置数据库

public class DaoManager {

    public static DaoManager instance;
    private DaoSession daoSession = null;

    /**
     * 配置数据库
     */
    public void setupDatabase(Context context) {
        //创建数据库shop.db"
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context, "goods.db", null);
        //获取可写数据库
        SQLiteDatabase db = helper.getWritableDatabase();
        //获取数据库对象
        DaoMaster daoMaster = new DaoMaster(db);
        //获取Dao对象管理者
        daoSession = daoMaster.newSession();
    }

    public DaoSession getDaoInstant() {
        return daoSession;
    }


    public static DaoManager getInstance() {

        if (instance == null) {
            instance = new DaoManager();
        }
        return instance;
    }

}

5.对数据库进行增删改查的操作

public class GreendaoHelper {
    /**
     * 添加数据,如果有重复则覆盖
     */
    public static void insert(GoodsDetailModel goodsDetailModel) {


       DaoManager.getInstance().getDaoInstant().getGoodsDetailModelDao().insertOrReplace(goodsDetailModel);


    }

    /**
     * 删除数据
     */
    public static void delete(GoodsDetailModel model) {
        DaoManager.getInstance().getDaoInstant().getGoodsDetailModelDao().delete(model);
    }

    /**
     * 删除s所有数据
     */
    public static void deleteAll() {
        DaoManager.getInstance().getDaoInstant().getGoodsDetailModelDao().deleteAll();
    }

    /**
     * 更新数据
     */
    public static void update(GoodsDetailModel goodsDetailModel) {
        DaoManager.getInstance().getDaoInstant().getGoodsDetailModelDao().update(goodsDetailModel);
    }

    /**
     * 查询条件为Type=TYPE_LOVE的数据
     *
     * @return
     */
    public static List<GoodsDetailModel> query(String id) {
        return DaoManager.getInstance().getDaoInstant().getGoodsDetailModelDao().queryBuilder().where(GoodsDetailModelDao.Properties.ProductId.eq(id)).list();
    }

    /**
     * 查询全部数据
     */
    public static List<GoodsDetailModel> queryAll() {
        return DaoManager.getInstance().getDaoInstant().getGoodsDetailModelDao().loadAll();
    }
}

其中的Model,大家可以根据自己的需求进行设置。

GreenDao的设置完成,大家可以在项目中进行使用了。

猜你喜欢

转载自blog.csdn.net/mlsnatalie/article/details/81186586