GreenDao简单使用

上篇给大家介绍了GreenDao的配置,今天给大家讲解GreenDao的简单使用~

  • GreenDao的简单使用:
    • 插入
    • 查询
        • 查询所有:
        • 精度查询
        • 模糊查询
    • 删除
    • 修改
    • 清除缓存
        • 补充:如何获取数据库文件
    • 在这里插入图片描述
  • Git链接: [lanyangyang](https://github.com/langyangyangzzZ/GreenDao/).
  • 欢迎大佬监督,指点迷津!

先给大家看看我写的简单demo:
GreenDao简单使用_第1张图片

GreenDao的简单使用:

//先过去GreenDao的对象  mGreenBeanDao  
private DaoSession mDaoSession;
private GreenBeanDao mGreenBeanDao;
mDaoSession = MyApplication.getInstance().getDaoSession();
mGreenBeanDao  = mDaoSession.getGreenBeanDao();

插入

  • mGreenBeanDao .insert(); //插入一条: 相同ID会报错
  • mGreenBeanDao .insertInTx(); //插入多条:
  • mGreenBeanDao .insertOrReplace(); //不存在则插入,存在则替换 单条
  • mGreenBeanDao .insertOrReplaceInTx(); //不存在则插入,存在则替换 多条

查询

查询所有:

mGreenBeanDao.loadAll();		//查询所有

精度查询

精确查找,若有多个或没有直接崩溃   查询的是当前ID  查询单个
	GreenBean unique = mGreenBeanDao.queryBuilder().where(GreenBeanDao.Properties.Id.eq("1").unique();
精确查找,查询多个
	List<GreenBean> list = 	mGreenBeanDao.queryBuilder().where(GreenBeanDao.Properties.Id.eq("1").list();
精确查找 Id > 输入的值       // where():相当于 &&
	 List<GreenBean> list = mGreenBeanDao.queryBuilder().where(GreenBeanDao.Properties.Id.gt(mEt_query.getText().toString())).list();
/精确查找 Id < 输入的值
        List<GreenBean> list = mGreenBeanDao.queryBuilder().where(GreenBeanDao.Properties.Id.lt(mEt_query.getText().toString())).list();
      
多条件查询 查询neme是否为输入的值 id十位为3
        mGreenBeanDao .queryBuilder().whereOr(GreenBeanDao.Properties.Name.eq(mEt_query.getText().toString()),
                GreenBeanDao.Properties.Id.eq(3)).list();

unique和list的区别:

  • unique() 返回一个对象 若有多个会导致崩溃
  • list() 返回多个对象
    where()和whereOr()的区别:
  • where() 相当于 &&
  • whereOr() 相当于 ||
  1. eq(): ==
  2. noteq():
  3. != - gt():
  4. >lt():
  5. < ge():
  6. >= le():
  7. <= like(%张%):
  8. between 模糊查询
  9. 两者之间- in:
  10. 在某个值内 - notIn: 不在某个值内

详细细节请观看源码:

package org.greenrobot.greendao;

import java.util.Collection;

import org.greenrobot.greendao.internal.SqlUtils;
import org.greenrobot.greendao.query.WhereCondition;
import org.greenrobot.greendao.query.WhereCondition.PropertyCondition;

/**
 * Meta data describing a property mapped to a database column; used to create WhereCondition object used by the query builder.
 * 
 * @author Markus
 */
public class Property {
    public final int ordinal;
    public final Class<?> type;
    public final String name;
    public final boolean primaryKey;
    public final String columnName;

    public Property(int ordinal, Class<?> type, String name, boolean primaryKey, String columnName) {
        this.ordinal = ordinal;
        this.type = type;
        this.name = name;
        this.primaryKey = primaryKey;
        this.columnName = columnName;
    }

    /** Creates an "equal ('=')" condition  for this property. */
    public WhereCondition eq(Object value) {
        return new PropertyCondition(this, "=?", value);
    }

    /** Creates an "not equal ('<>')" condition  for this property. */
    public WhereCondition notEq(Object value) {
        return new PropertyCondition(this, "<>?", value);
    }

    /** Creates an "LIKE" condition  for this property. */
    public WhereCondition like(String value) {
        return new PropertyCondition(this, " LIKE ?", value);
    }

    /** Creates an "BETWEEN ... AND ..." condition  for this property. */
    public WhereCondition between(Object value1, Object value2) {
        Object[] values = { value1, value2 };
        return new PropertyCondition(this, " BETWEEN ? AND ?", values);
    }

    /** Creates an "IN (..., ..., ...)" condition  for this property. */
    public WhereCondition in(Object... inValues) {
        StringBuilder condition = new StringBuilder(" IN (");
        SqlUtils.appendPlaceholders(condition, inValues.length).append(')');
        return new PropertyCondition(this, condition.toString(), inValues);
    }

    /** Creates an "IN (..., ..., ...)" condition  for this property. */
    public WhereCondition in(Collection<?> inValues) {
        return in(inValues.toArray());
    }

    /** Creates an "NOT IN (..., ..., ...)" condition  for this property. */
    public WhereCondition notIn(Object... notInValues) {
        StringBuilder condition = new StringBuilder(" NOT IN (");
        SqlUtils.appendPlaceholders(condition, notInValues.length).append(')');
        return new PropertyCondition(this, condition.toString(), notInValues);
    }

    /** Creates an "NOT IN (..., ..., ...)" condition  for this property. */
    public WhereCondition notIn(Collection<?> notInValues) {
        return notIn(notInValues.toArray());
    }

    /** Creates an "greater than ('>')" condition  for this property. */
    public WhereCondition gt(Object value) {
        return new PropertyCondition(this, ">?", value);
    }

    /** Creates an "less than ('<')" condition  for this property. */
    public WhereCondition lt(Object value) {
        return new PropertyCondition(this, ", value);
    }

    /** Creates an "greater or equal ('>=')" condition  for this property. */
    public WhereCondition ge(Object value) {
        return new PropertyCondition(this, ">=?", value);
    }

    /** Creates an "less or equal ('<=')" condition  for this property. */
    public WhereCondition le(Object value) {
        return new PropertyCondition(this, "<=?", value);
    }

    /** Creates an "IS NULL" condition  for this property. */
    public WhereCondition isNull() {
        return new PropertyCondition(this, " IS NULL");
    }

    /** Creates an "IS NOT NULL" condition  for this property. */
    public WhereCondition isNotNull() {
        return new PropertyCondition(this, " IS NOT NULL");
    }

}

模糊查询

       //模糊查询
        //注意:得先插入小明
//        List list = mGreenBeanDao.queryBuilder().where(GreenBeanDao.Properties.Name.like("小明%")).list();
//        List list = mGreenBeanDao.queryBuilder().where(GreenBeanDao.Properties.Name.like("%小明%")).list();
        List<GreenBean> list = mGreenBeanDao.queryBuilder().where(GreenBeanDao.Properties.Name.like("%小明")).list();

删除

     //按照Key(键) id删除
 mGreenBeanDao.deleteByKey(Long.valueOf(mEtDelete.getText().toString().trim()));
//        mGreenBeanDao .delete();     //删除一条//        mGreenBeanDao .deleteInTx();    //删除多个对象
//        Long[] keys = {5L,6L};
//        mGreenBeanDao .deleteByKeyInTx(keys);       //以Key(id)删除多个对象
//	  mGreenBeanDao .deleteAll();		//删除所有数据

修改

        //注意:name不能重复,因为引用了 @Unique 注解
        mGreenBeanDao.update(new GreenBean(Long.valueOf(mEtUpDate.getText().toString().trim()), "已修改", "已修改" + index++, index++));
//        mGreenBeanDao.updateInTx(List); //修改多个 ,通过Key修改

清除缓存

mDaoSession.clear();//   清理缓存:      MyApplication.getInstance().getDaoSession();
mGreenBeanDao.detachAll();//       清理所有缓存:

补充:如何获取数据库文件

如何在手机中查看数据库生成文件:

GreenDao简单使用_第2张图片

  • 黄色选中:内部存储
  • 蓝色选中:的是外部存储,外部存储有一层默认的保护,所以需要选择adcard0,我的手机是HUAWIEandroid10的手机,可能有部分同学和我的不一样,但基本位置是差不多的不一样的话就麻烦大家举一反三啦~

内部存储:
GreenDao简单使用_第3张图片

  • 红色(cache):app缓存目录
  • 黄色(databases) :数据库 //咋们的数据库角MyDb是应为我在Application中设置的,可以随意改名字
  • 蓝色(shared_prefs):SharedPreferences 保存

外部存储:

  • 通过Context.getExternalFilesDir()方法可以获取到 SDCard/Android/data/你的应用的包名/files/ 目录,一般放一些长时间保存的数据

  • 通过Context.getExternalCacheDir()方法可以获取到 SDCard/Android/data/你的应用包名/cache/目录,一般存放临时缓存数据

GreenDao简单使用_第4张图片

Git链接: lanyangyang.

欢迎大佬监督,指点迷津!

你可能感兴趣的:(GreenDao简单使用)