码迷,mamicode.com
首页 > 移动开发 > 详细

android_orm框架之greenDAO(二)

时间:2017-04-23 20:14:45      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:tle   date()   strong   itblog   ati   logs   调用   一点   roi   

一、概述

  在上次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();
技术分享

技术分享

android_orm框架之greenDAO(二)

标签:tle   date()   strong   itblog   ati   logs   调用   一点   roi   

原文地址:http://www.cnblogs.com/tangshiguang/p/6753630.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!