The Greendao of Android_orm Framework (II.)

Source: Internet
Author: User

I. Overview

In the last Greendao first article, we introduced the use steps and basic usage of Greendao, the article Links: http://www.cnblogs.com/jerehedu/p/4304766.html

We will continue to study Greendao in depth. Data query presentation is one of the most commonly used features in the application, Greendao provides us with powerful query support, and uses a completely object-oriented approach, even if you do not understand the SQL is not a problem.

Second, the function realization

First, the Java entity class and the DAO class are generated according to the procedure described above, and the test data is inserted and the main code is as follows:

  Generator Code:

    public static void Main (string[] args) throws IOException, Exception {        //Create Schema Object        schema scheme = new schema (1, "Com.jredu.entity");        Scheme.setdefaultjavapackagedao ("Com.jredu.dao");        Add the Employee Entity entities        employee= scheme.addentity ("employee");        Employee.addidproperty ();        Employee.addstringproperty ("name");        Employee.addstringproperty ("Sex");        Employee.adddateproperty ("Birthday");        Employee.adddateproperty ("HireDate");        Employee.addlongproperty ("Deptno");        Create Java class        new Daogenerator (). Generateall (Scheme, "e:\\android_space\\jredu_greendao\\src");    }

Insert test data:

list<employee> list = new arraylist<employee> ();    List.add (new Employee (null, "Zhang San", "male", new Date (), new Date (), 1l));    List.add (new Employee (NULL, "John Doe", "male", new Date (), new Date (), 1l));    List.add (new Employee (null, "Harry", "Male", new Date (), new Date (), 1l));    List.add (new Employee (null, "Zhangzhang", "female", new Date (), new Date (), 1l));    List.add (new Employee (null, "Zhao Liu", "female", new Date (), new Date (), 2l));    List.add (new Employee (null, "Zhao Qi", "Male", new Date (), new Date (), 1l));    Employeedao.insertintx (list);

After the preparation is done, we'll look at how to use Greendao for data queries, and DAO classes can make complex queries with QueryBuilder objects in addition to the Load series method.

  The Load series method is as follows:

Public T Load (K key): Load entity objects based on primary key

Public T Loadbyrowid (long rowId): Load entity objects based on RowId

Public list<t> Loadall (): Loads all valid entity objects.

Load Series method is relatively simple, do not repeat here, we mainly study how to query through the QueryBuilder object.

QueryBuilder is a class that Greendao provides specifically for building queries, and using this class, we can use the following code when querying without using SQL, such as querying all employee information:

    Check all employee information querybuilder<employee> employeequerbuilder= employeedao.querybuilder (); Employeequerbuilder.list ( );

With Logcat, you can view the generated SQL.

So how do you make conditional queries? QueryBuilder provides us with a method for constructing the query condition, which is prototyped as follows:

1. Public querybuilder<t> where (wherecondition cond, WhereCondition ... condmore): Use and to connect multiple query criteria.

2, public querybuilder<t> Whereor (wherecondition cond1, WhereCondition cond2, wherecondition ... condmore) : Use or to concatenate multiple query criteria.

3. Public querybuilder<t> Orderdesc (Property ... properties): Sort

4. Public wherecondition or (WhereCondition cond1, WhereCondition cond2, WhereCondition ... condmore): Use or to form a query condition

5. Public wherecondition or (WhereCondition cond1, WhereCondition cond2, WhereCondition ... condmore): Using and to form a query condition

Through the above method, we can see that using the above method needs to construct the WhereCondition object, by viewing the document we can construct by the Property object of the corresponding entity generated in the DAO class.

  Property part of the source code is as follows:

/** creates an "equal (' = ')" Condition for the property.    */Public WhereCondition eq (Object value) {return new propertycondition (this, "=?", value); }/** creates an "not equal (' <> ')" condition for the ".    */Public WhereCondition Noteq (Object value) {return new propertycondition (this, "&LT;&GT;?", value); }/** creates an ' like ' condition for the property.    */public wherecondition like (String value) {return new propertycondition (this, ' like ', value); }/** creates an "between ... And ... "Condition for the property.        */Public WhereCondition between (object Value1, Object value2) {object[] values = {value1, value2}; return new PropertyCondition (this, "between?")    and? ", values); }/** creates a "in (..., ...)" condition for the.        */Public WhereCondition in (Object ... invalues) {StringBuilder condition = new StringBuilder ("in"); Sqlutils.appendplaceholDERs (condition, invalues.length). Append (') ');    return new PropertyCondition (this, condition.tostring (), invalues); }

Through the source code we can see that the property class provides a lot of constructs wherecondition methods, these methods are used to construct the contents of the SQL statement. Below we use some examples to illustrate how to use:

III. implementation of the case
Example 1: Querying all employees in department 1
Employeequerbuilder.where (EmployeeDao.Properties.Deptno.eq (1l)); employees = Employeequerbuilder.list ();

Example 2: Query the department 1 male employees and department 2 female employees
WhereCondition wherecondition1=        Employeequerbuilder.and (Properties.Deptno.eq (1l), Properties.Sex.eq ("male"));
WhereCondition wherecondition2= Employeequerbuilder.and (Properties.Deptno.eq (2l), Properties.Sex.eq ("female"));
Employees =employeequerbuilder.whereor (WhereCondition1, WhereCondition2). List ();

In fact, each time the QueryBuilder object calls the list method query, it creates one of the query objects, and the query object is ultimately used for querying the database. Through the documentation and source code, we know that query is a reusable querying object that is used to query the returned entity. For example 2, we change the condition to query the female employee in department 1 and the male employee in department 2, and reuse the query object in Example 2, which is more efficient than using the QueryBuilder list method every time.

Query<employee> query= Employeequerbuilder.whereor (WhereCondition1, WhereCondition2). Build (); Query.setparameter (0, 1l), Query.setparameter (1, "female"), Query.setparameter (2, 2l), Query.setparameter (3, "male"); Employees = Query.list ();

The Greendao of Android_orm Framework (II.)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.