• android_orm框架之greenDAO(二)


    一、概述

      在上次greenDao第一篇文章中,我们对greenDao的使用步骤和基本用法给大家做了介绍,文章链接:http://www.cnblogs.com/jerehedu/p/4304766.html 

      现在将继续深入学习greenDAO。数据查询展示是应用中最常用的功能之一,greenDAO为我们提供了强大的查询支持,并且采用完全面向对象的方式,即使一点都不懂SQL也没有问题。

    二、功能实现

      首先按照上文所述过程生成java实体类和dao类,并插入测试数据,主要代码如下:

      生成器代码:

    复制代码
        public static void main(String[] args) throws IOException, Exception {
            //创建schema对象
            Schema scheme = new Schema(1, "com.jredu.entity");
            scheme.setDefaultJavaPackageDao("com.jredu.dao");
            //添加Employee实体
            Entity employee= scheme.addEntity("Employee");
            employee.addIdProperty();
            employee.addStringProperty("name");
            employee.addStringProperty("sex");
            employee.addDateProperty("birthday");
            employee.addDateProperty("hireDate");
            employee.addLongProperty("deptno");
            //创建java类
            new DaoGenerator().generateAll(scheme, "E:\android_space\JREDU_GREENDAO\src");
        }
    复制代码

      插入测试数据:

    复制代码
    List<Employee> list = new ArrayList<Employee>();
        list.add(new Employee(null, "张三", "男", new Date(), new Date(), 1l));
        list.add(new Employee(null, "李四", "男", new Date(), new Date(), 1l));
        list.add(new Employee(null, "王五", "男", new Date(), new Date(), 1l));
        list.add(new Employee(null, "章章", "女", new Date(), new Date(), 1l));
        list.add(new Employee(null, "赵六", "女", new Date(), new Date(), 2l));
        list.add(new Employee(null, "赵七", "男", new Date(), new Date(), 1l));
        employeeDao.insertInTx(list);
    复制代码

      准备工作完成后,我们看看如何使用greenDAO进行数据查询,DAO类除了提供load系列方法外,还通过QueryBuilder对象可以进行复杂查询。

      Load系列方法如下:

    public T load(K key):根据主键加载实体对象

    public T loadByRowId(long rowId):根据rowId加载实体对象

    public List<T> loadAll():加载所有有效实体对象。

      Load系列方法比较简单,在此不做赘述,下面我们主要研究如何通过QueryBuilder对象进行查询。

      QueryBuilder是greenDAO提供的专门用于构建查询的类,使用此类,在查询时我们可以不用使用SQL,比如查询所有员工信息,我们可以使用如下代码:

        //查询所有员工信息
    QueryBuilder<Employee> employeeQuerBuilder= employeeDao.queryBuilder();
    employeeQuerBuilder.list();

      通过logcat,可查看生成的SQL。

      那么如何进行条件查询?QueryBuilder为我们提供了用于构造查询条件的方法,方法原型如下:

    1、public QueryBuilder<T> where(WhereCondition cond, WhereCondition... condMore):使用and连接多个查询条件。

    2、public QueryBuilder<T> whereOr(WhereCondition cond1, WhereCondition cond2, WhereCondition... condMore):使用or连接多个查询条件。

    3、public QueryBuilder<T> orderDesc(Property... properties):排序

    4、public WhereCondition or(WhereCondition cond1, WhereCondition cond2, WhereCondition... condMore):使用or构成查询条件

    5、public WhereCondition or(WhereCondition cond1, WhereCondition cond2, WhereCondition... condMore):使用and构成查询条件

      通过以上方法,我们可以看出使用以上方法需要构建WhereCondition对象,通过查看文档我们可以通过Dao类中生成的对应实体的Property对象进行构造,

      property部分源码如下:

    复制代码
    /** 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);
        }
    复制代码

      通过源码我们可以看出,property类中提供了大量构造WhereCondition的方法,这些方法都是用于构造sql语句内容的。下面我们使用一些例子用来说明如何使用:

    三、案例实现

    例1:查询部门1中所有员工

    employeeQuerBuilder.where(EmployeeDao.Properties.Deptno.eq(1l));
    employees = employeeQuerBuilder.list();

    例2:查询部门1中男性员工和部门2中女性员工

    复制代码
    WhereCondition whereCondition1= 
           employeeQuerBuilder.and(Properties.Deptno.eq(1l),Properties.Sex.eq("男"));
    WhereCondition whereCondition2= employeeQuerBuilder.and(Properties.Deptno.eq(2l),Properties.Sex.eq("女"));
    employees =employeeQuerBuilder.whereOr(whereCondition1, whereCondition2).list();
    复制代码

      实际上QueryBuilder对象每次调用list方法查询时都是先创建了一个Query对象,Query对象最终用于查询数据库。通过文档和源码,我们知道Query是一个可重复使用的用于查询返回实体的查询对象。比如例子2中,我们将条件改为查询部门1中的女性员工和部门2中的男性员工,重用例子2中的Query对象,这样要比每次都使用QueryBuilder的list方法更加高效

    复制代码
    Query<Employee> query= employeeQuerBuilder.whereOr(whereCondition1, whereCondition2).build();
    query.setParameter(0, 1l);
    query.setParameter(1, "女");
    query.setParameter(2, 2l);
    query.setParameter(3, "男");
    employees = query.list();
    复制代码

  • 相关阅读:
    网络编程基础
    Unicode 和 UTF-8 有什么区别?
    IDEA中全局搜索只展示100条结果的问题
    MySql实现无则插入有则更新的解决方案
    总是报Expected indentation of 0 spaces but found 2如何解决
    vue Cannot read property 'get' of undefined
    mac 下修改 jenkins 端口
    mac 查找被占用的8080端口并关闭
    sql server 常用高级查询sql
    java & spring 注解 备忘
  • 原文地址:https://www.cnblogs.com/tangshiguang/p/6753630.html
Copyright © 2020-2023  润新知