Android Combat--greendao3.2 Use, admiringly

Source: Internet
Author: User
Tags one table sqlite database build gradle

This article covers the following topics:

  • Objective
  • GreenDao3.2 's introduction
  • Configuration of the GreenDao3.2
  • Use of GreenDao3.2
  • Conclusion

Greendao is an artifact of the operational database, which has been used by a wide range of developers after a 2.0 version of the upgrade. Really is very useful, easy to get started, can be left to the database of the table operation and database SQL write, Bo Master used once, and the previous database operation a lot of code will be shrunk into a sentence, comfortable

Before knowing Greendao, you must know that an ORM (object Relation Mapping-relational mapping) is represented by the association of a database and a Bean object through Greendao, which is represented in the form

Greendao is very popular, and its advantages are closely related, from the official website can see such a picture, it represents in the mainstream ORM third-party library, its database operation speed is the fastest

Moreover, the advantages include the following points

    • Fast access speed
    • Support for database encryption
    • Lightweight
    • Activating entities
    • Cache support
    • Code Auto-generation

Greendao configuration is very simple, but it is important to note that some people follow the correct configuration but frequently error, the individual has experienced, the last reason is the network has a problem. Because the campus network of the DNS service is poor, so the resolution of Greendao dependent Web site

One, you need to add dependencies in the Build.gradle of the Project (project)

Second, add dependencies in the Build.gradle of the project (Module)

The configuration is successful here.

After the configuration is complete, the most important thing is the use of Greendao, perhaps used bmob third-party back-end Cloud alumni know that their API is somewhat similar, all through the API to assemble SQL statements

The following is a shopping cart in the actual combat to use the Greendao, here the shopping cart display chart is as follows

We know the database operation needs: database name, table name, field name, is indispensable, the following is the creation of these three items

Greendao the Bean object needs to be created, the Bean object is the name of the table, and its property value is the field name, and its implementation is implemented by means of annotations, the following is the bean object of the shopping cart (one table per bean object)

It is important to note that after the creation is complete, build gradle is required to complete our code auto-generation. The automatically generated code has

    1. How to construct bean entities and get and set methods
    2. Daomaster, Daosession, Daos class

This explains the comment for the Bean object.

    1. @Entity: Tells Greendao that the object is an entity and only the Bean class that is annotated by @Entity can be manipulated by the DAO class
    2. @Id: The Id of the object, using the long type as EntityId, or an error will be added. (Autoincrement=true) indicates that the primary key is self-increasing, and if false, the old value is used
    3. @Property: You can customize the field name and note that the foreign key cannot use the property
    4. @NotNull: property cannot be empty
    5. @Transient: Properties that use this comment are not stored in a field in the database
    6. @Unique: The property value must be a unique value in the database
    7. @Generated: Comments on constructors, methods, etc. that are automatically generated after compilation, prompting that constructors, methods, etc. cannot be modified

database table names and fields have been built, below the creation of a database, below through the traditional and Greendao comparison to experience its advantages

① Traditional Database creation

②greendao Database Creation

As you can see, Greendao has shrunk our database creation to a few words, and the code automatically creates the Bean object as a table, no longer a traditional handwritten SQL statement. The database creation here only needs to be performed once in application, where several classes are explained

    • Devopenhelper: Specific implementation of Sqliteopenhelper to create SQLite database
    • Daomaster:greendao Top-level objects, as database objects, for creating tables, and for deleting tables
    • Daosession: Managing all of the DAO objects, there is an add-and-Remove Search API in DAO object

Since we have created the Bean object for daosession and shop, we will automatically generate our Shopdao object after compiling, which can be obtained by daosession

The DAO (data Access Object) here refers to an API that provides database operations, which can be used by DAO for additional pruning and checking operations.

The table names, fields, and databases of the database are all set up, and the following is a comparison of the advantages of the database by traditional and greendao operations.

① traditional additions and deletions and changes

②greendao additions and deletions to search

The effect is obvious, Greendao packaging more concise, semantic clarity, QS under the face of Greendao DAO object other API Introduction

  • Add a single data
    • Getshopdao (). Insert (shop);
    • Getshopdao (). Insertorreplace (Shop);
  • Add multiple Data
    • Getshopdao (). Insertintx (Shoplist);
    • Getshopdao (). Insertorreplaceintx (Shoplist);
  • Search All
    • list< shop> List=getshopdao (). Loadall ();
    • list< shop> List=getshopdao (). QueryBuilder (). List ();
  • Query Attach single condition
    • . where ()
    • . Whereor ()
  • Query attaching multiple conditions
    • . where (,,,)
    • . Whereor (,,,)
  • Query additional sort
    • . Orderdesc ()
    • . ORDERASC ()
  • Query limits when page count
    • . Limit ()
  • Total number of queries
    • . Count ()
  • modifying individual data
    • Getshopdao (). Update (shop);
  • Modify multiple data
    • Getshopdao (). Updateintx (Shoplist);
  • Delete individual data
    • Gettabuserdao (). Delete (user);
  • Delete multiple data
    • Getuserdao (). Deleteintx (userlist);
  • Delete Data Bykey
    • Gettabuserdao (). Deletebykey ();

About the basic concept of Greendao and basic operation here, more for Greendao database operation also need to explore from the actual combat, here is just a quick start guide. Greendao advanced operation also includes: multi-table query, multi-table association, session cache and other uses, can go to Greendao's official website to study

Android Combat--greendao3.2 Use, admiringly

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.