GreenDAO (2) of android_orm framework, which is used by androidgreendao

Source: Internet
Author: User

GreenDAO (2) of android_orm framework, which is used by androidgreendao

I. Overview

In the first article of greenDao last time, we introduced the steps and basic usage of greenDao.

We will continue to learn about greenDAO. Data Query display is one of the most commonly used functions in applications. greenDAO provides us with powerful query support and adopts a fully object-oriented approach. Even if you do not understand SQL at all, there is no problem.

Ii. function implementation

First, generate the java entity class and dao class according to the process described above, and insert the test data. 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. setdefajavjavapackagedao ("com. jredu. dao "); // Add the Employee Entity employee = scheme. addEntity ("Employee"); employee. addIdProperty (); employee. addStringProperty ("name"); employee. addStringProperty ("sex"); employee. addDateProperty ("birthday"); employee. addDateProperty ("hireDate"); employee. addLongProperty ("deptno"); // creates a java class new DaoGenerator (). generateAll (scheme, "E: \ android_space \ j1__greendao \ src ");}

Insert test data:

List <Employee> list = new ArrayList <Employee> (); list. add (new Employee (null, "Michael", "male", new Date (), new Date (), 1l); list. add (new Employee (null, "Li Si", "male", new Date (), new Date (), 1l); list. add (new Employee (null, "Wang Wu", "male", new Date (), new Date (), 1l); list. add (new Employee (null, "chapter", "female", new Date (), new Date (), 1l); list. add (new Employee (null, "Zhao ", "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 complete, let's take a look at how to use greenDAO to query data. In addition to the load series methods, DAO can also perform complex queries through QueryBuilder objects.

  The Load methods are as follows:

Public T load (K key): loads object based on the primary key

Public T loadByRowId (long rowId): loads object based on rowId

Public List <T> loadAll (): loads all valid object objects.

The Load methods are relatively simple, so we will not repeat them here. Next we will mainly study how to query through QueryBuilder objects.

QueryBuilder is a class provided by greenDAO that is used to build a query. When using this class, you do not need to use SQL when querying. For example, you can use the following code to query information of all employees:

// Query all Employee information: QueryBuilder <Employee> employeeQuerBuilder = employeeDao. queryBuilder (); employeeQuerBuilder. list ();

You can use logcat to view the generated SQL.

So how do I perform conditional queries? QueryBuilder provides us with methods to construct query conditions,The method is prototype as follows:

1. public QueryBuilder <T> where (WhereCondition cond, WhereCondition... condMore): use and to connect multiple query conditions.

2. public QueryBuilder <T> whereOr (WhereCondition cond1, WhereCondition cond2, WhereCondition... condMore): use or to connect multiple query conditions.

3. public QueryBuilder <T> orderDesc (Property... properties): Sort

4. public WhereCondition or (WhereCondition cond1, WhereCondition cond2, WhereCondition... condMore): use or to create a query Condition

5. public WhereCondition or (WhereCondition cond1, WhereCondition cond2, WhereCondition... condMore): use and to create query Conditions

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

  The property source code is as follows:

/** 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);    }

From the source code, we can see that the property class provides a lot of methods to construct WhereCondition. These methods are used to construct the content of SQL statements. Here are some examples to illustrate how to use them:

Iii. Case implementation
Example 1: Query all employees in department 1
employeeQuerBuilder.where(EmployeeDao.Properties.Deptno.eq(1l));employees = employeeQuerBuilder.list();

Example 2: Search for male employees in department 1 and female employees in Department 2
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 for Query, a Query object is created first, and the Query object is used to Query the database. Through documents and source code, we know that Query is a reusable Query object used to Query returned objects. For example, in Example 2, we change the condition to Query female employees in department 1 and male employees in Department 2, and reuse the Query objects in example 2, this 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 ();

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.