Basic use of Andrews ORM framework GreenDao

Basic use of Andrews ORM framework GreenDao

1. Project introduction
2. to establish a database

1. Project introduced
in build.gradle (Project) was added to the dependencies

classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' // add plugin

Added (APP) above build.gradle

apply plugin: 'org.greenrobot.greendao' // apply plugin

Adding dependencies in

 implementation 'org.greenrobot:greendao:3.2.2' // add library
    implementation 'net.zetetic:android-database-sqlcipher:3.5.9@aar'

2. The establishment of a database
2.1 build entity class, we Goods (Goods) category as an example
we just need to create a new class, and @Entity comment to add at the top of the class, and then re-make_project, GreenDao is generated below for our Code

@Entity
public class GoodsModel {
    @Id(autoincrement = true)//设置id为主键,并且自增
    private long id;
    private Integer goodsId;
    private String name;
    private String icon;
    private String info;
    private String type;
    @Generated(hash = 1830037561)
    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 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;
    }
}

2.2 In the myApplication

/*
    连接数据库并创建会话
     */
  public class MyApplication extends Application
{
    public static final String DB_NAME = "goods.dp";
    public static final String DB_PASS = "123456";
    public static DaoSession mDaoSession;
    public static DaoMaster daoMaster;
    public static DaoMaster.DevOpenHelper devOpenHelper;
    @Override
    public void onCreate() {
        super.onCreate();
        initDb();
    }

    /*
    连接数据库并创建会话
     */
    public void initDb()
    {
        //1.获取需要连接的数据库
        devOpenHelper = new DaoMaster.DevOpenHelper(this,DB_NAME);
//        SQLiteDatabase db = devOpenHelper.getWritableDatabase();//非加密数据库
        Database database = devOpenHelper.getEncryptedReadableDb(DB_PASS);//加密数据库
        //2.创建数据库连接
        daoMaster = new DaoMaster(database);
        //3.根据连接创建数据库会话
        mDaoSession = daoMaster.newSession();
    }
    /*
    关闭数据库连接
     */
    public static void closeConnection()
    {
        if (devOpenHelper != null)
        {
            devOpenHelper.close();
            devOpenHelper=null;
        }
        if (mDaoSession != null)
        {
            mDaoSession.clear();
            mDaoSession=null;
        }
    }
}

3. The additions and deletions to the database change search operation
after the above operation we carried out makeproject, GreenDao will help us generate a class named GoodsModelDao, we can manipulate the database through the object of this class.
GreenDaoManager we create a new class:

public class GreenDaoManager
{
    private Context context;
    private GoodsModelDao mGoodModelDao;
    private static GreenDaoManager greenDaoManager;
    private DaoMaster daoMaster;
    private DaoSession daoSession;

    public GoodsModel goodsModel = new GoodsModel();

    private GreenDaoManager(Context context) {
        this.context = context;
        mGoodModelDao = MyApplication.mDaoSession.getGoodsModelDao();
    }
}

After initializing the mGoodModelDao, we can use mGoodModelDao CRUD operations
3.1 insert data

/**
     * 添加数据到数据库,常用的有三种方式
     */
    public void insertGoods()
    {
        //case1:插入一条数据,当指定主键已经存在时会发生错误
        mGoodModelDao.insert(goodsModel);

        //case2:插入一条数据,当指定主键存在时则进行替换,没有则进行插入
        mGoodModelDao.insertOrReplace(goodsModel);
        //case 3: 插入一条数据,判断数据的主键是否为空,若为空,则进行insert操作,否则进行update操作。
        // 但若数据库中无此条主键所在的数据,则会执行失败
        mGoodModelDao.save(goodsModel);
    }

Wherein the source save () method is:

public void save(T entity) {
        if (hasKey(entity)) {
            update(entity);
        } else {
            insert(entity);
        }
    }

3.2 query data, there are a lot of data query method, are listed below:

    /**
     * 从数据库查询数据
     */

    public List<GoodsModel> quaryGoods( )
    {
        //查询全部数据
        mGoodModelDao.loadAll();
        //查询指定主键
        mGoodModelDao.load(Long key);
        //条件查询,名称等于奥利奥
        mGoodModelDao.queryBuilder().where(GoodsModelDao.Properties.Name.eq("奥利奥")).list();
        //条件查询,名称不等于奥利奥
        mGoodModelDao.queryBuilder().where(GoodsModelDao.Properties.Name.notEq("奥利奥")).list();
        //条件查询,id大于5的
        mGoodModelDao.queryBuilder().where(GoodsModelDao.Properties.GoodsId.gt(5)).list();
        //条件查询,id 大于等于5
        mGoodModelDao.queryBuilder().where(GoodsModelDao.Properties.GoodsId.ge(5)).list();
        //条件查询,id小于5
        mGoodModelDao.queryBuilder().where(GoodsModelDao.Properties.GoodsId.lt(5)).list();
        //条件查询,id小于等于5
        mGoodModelDao.queryBuilder().where(GoodsModelDao.Properties.GoodsId.le(5)).list();
        //模糊查询
        mGoodModelDao.queryBuilder().where(GoodsModelDao.Properties.Name.like("奥利%")).list();
        //查询区间内的数据
        mGoodModelDao.queryBuilder().where(GoodsModelDao.Properties.GoodsId.between(5,10)).list();
        //查询结果升序
        mGoodModelDao.queryBuilder().where(GoodsModelDao.Properties.Name.like("奥利")).orderAsc(GoodsModelDao.Properties.GoodsId).list();
        //查询结果降序
        mGoodModelDao.queryBuilder().where(GoodsModelDao.Properties.Name.like("奥利%")).orderDesc(GoodsModelDao.Properties.GoodsId).list();

    }

3.3 update data

   public void updateGoods(GoodsModel goodsModel)
    {
        mGoodModelDao.update(goodsModel);
    }

3.4 to delete data

  public void deleteGoods(GoodsModel goodsModel)
    {
        //删除全部
        mGoodModelDao.deleteAll();
        //删除指定主键数据
        mGoodModelDao.deleteByKey(Long key);
        //删除某个实体对应的数据
        mGoodModelDao.delete(goodsModel);
    }

Basics of GreenDao is that these specific things still have to be combined with flexible project

Published 47 original articles · won praise 15 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_41525021/article/details/104201101