Android Greendao Database Framework Learning (1)

Source: Internet
Author: User

I. Greendao Overview:

Greendao is an ORM solution that can help Android developers quickly map Java objects to SQLite database forms. Greendao characteristics of the most efficient operation, the least memory consumption, official website: http://greendao-orm.com/... GitHub: Https://github.com/greenrobot/greenDAO.

1, Daocore:greendao Core Library.
2, Daoexample:greendao use the sample project.
3. Daoexamplegenerator:greendao uses the example entity class and the DAO Component Builder project.
4. Daogenerator:greendao DAO Component Builder Project.
5, Daotest:greendao test project.
6. Performancetestormlite:ormlite Frame Performance test Project

Two. Generate the Greendao framework:

To download Greendao-generator.jar and Freemarker.jar two packages before use, freemarker need this to generate Java files, to see how to use:

(1). First build a Java project (not an Android project), and then import the above Greendao-generator.jar and Freemarker.jar into the Java project.


Generate a class, the name is random: I have to name Daogeneratortest:

<span style= "FONT-SIZE:18PX;" >public class Daogeneratortest {public static void main (string[] args) throws Exception {  ///The first parameter is a database version number, The second is the path of the automatically generated package schema        schema = new schema (1, "de.greenrobot.daoexample");                  Addnote (schema);         Build the path, that is, in what directory to generate this file, my path is in a new Android project Src-gen directory under the new        Daogenerator (). Generateall (Schema, ". /androidtestgreendao/src-gen ");      }      private static void Addnote (schema schema) {    //entity is an entity, the entity class is named note entity        Note = schema.addentity ("note");          Primary key, set self-growth can be at the back of the home. AutoIncrement ()        note.addidproperty ();          An attribute in an entity class (that is, a field in a table) cannot be empty, and other settings look at the method name to know what it means        note.addstringproperty ("text"). Notnull ();          Note.addstringproperty ("comment");          Note.adddateproperty ("date");      }  } </span>

And note: Src-gen must be created first to succeed.
Build succeeded:



Schema class: Can be seen as a container to hold entities.

Entity class: Can be used to map a solid class to a corresponding table.

Daogenerator: The generator for entity classes and DAO components, Generateall based on the schema and directory parameters, first generate the file directory, and remove all entities from the schema, according to the Freemaker template to generate the corresponding entity and DAO components.


Three. Use the Greendao framework:

To create an Android project, I have to have the Android project named (the path that I specified above in Java is under this project): Androidtestgreendao


Here Greendao-1.3.0-beta-1.jar is Libs under the daoexample directory downloaded from GitHub.

To create a database:

public class Mainactivity extends Activity {private sqlitedatabase db;private Daomaster master;private daosession session ;p rivate Notedao Notedao; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main);        Create a database named Notes-dbdevopenhelper helper = new Daomaster.devopenhelper (This, "notes-db", null);d B = Helper.getwritabledatabase (); master = new Daomaster (db); session = Master.newsession (); Notedao = Session.getnotedao ();                //Add a data note Note = new Note (10L, "1", "1", New Date ()); Notedao.insert (note);}}


Note: The entity object, mapped to the table NOTE.

Notedao: The code shows that this class provides methods for generating and deleting tables, and that the class inherits Abstractdao (one of the Greendao core classes, encapsulating various methods of manipulating the data in the table).

Daomaster: Inherited from Abstractdaomaster, used to create a database and manage database connections.

Daosession: Inherits from Abstractdaosession, the entry of all DAO objects, through which DAO components of the entity are obtained.





Android Greendao Database Framework Learning (1)

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.