Android ORM Framework Greendao

Source: Internet
Author: User

Performance comparison of Greendao Ormlite:


As you can see, the INSERT and update efficiency of Greeendao is about twice times faster than Ormlite, and load is exaggerated to 4 times times more.


We can be on the official website (http://search.maven.org/#search%7cga%7c1%7cg%3a%22de.greenrobot%22%20and%20a%3a%22greendao-generator% 22) go directly down to the GitHub project homepage to download the source code.

Create a new Java project below, note that Java engineering is not an Android project, project name greendaoexample . and manually create a new Src-gen folder in the project. Import Freemaker.jar and Greendao-generator.jar, add to build path. Build a class like this:

Import De.greenrobot.daogenerator.daogenerator;import De.greenrobot.daogenerator.entity;import De.greenrobot.daogenerator.property;import De.greenrobot.daogenerator.schema;import de.greenrobot.daogenerator.tomany;/** * generates entities and DAOs for the example project Daoexample. * * Run it as a Java application (not Android).                                                                                                                                                                                              * * @author Markus */public class exampledaogenerator{                                                                                                                          public static void Main (string[] args) throws Exception {Schema schema = new schema (3, "com.young.do");                                                                                                                          Addnote (Schema)        ;                                                                                                                                                                                                                          Addcustomerorder (schema);                                                                                                                          New Daogenerator (). Generateall (Schema, "..    /daoexample/src-gen ");                                                                                                                                                                                                                                                   }                                                                                                                          private static void Addnote (schema Schema)        {Entity Note = schema.addentity ("note");        Note.addidproperty ();        Note.addstringproperty ("text"). Notnull ();        Note.addstringproperty ("comment");                     Note.adddateproperty ("date");                                                                                                                                                                  }                                                                                                                          private static void Addcustomeror        Der (schema Schema) {Entity customer = schema.addentity ("Customer");        Customer.addidproperty (); Customer.addstringproperty ("Name"). Notnull ();         Entity order = schema.addentity ("Order"); Order.settablename ("ORDERS");        "ORDER" is a reserved keyword order.addidproperty ();        Property OrderDate = Order.adddateproperty ("date"). GetProperty ();        Property CustomerId = Order.addlongproperty ("CustomerId"). Notnull (). GetProperty ();                                                                                                                                                                                                              Order.addtoone (Customer, customerId);                                                                                                                          Tomany Customerto        Orders = Customer.addtomany (order, customerId);        Customertoorders.setname ("Orders");    CUSTOMERTOORDERS.ORDERASC (orderDate);                                                                                                                                                                                                                                                   }                                                                                                                          }

run directly, code generation



Automatically generate model and DAO, look at a model class:

Package com.young.do;//The This CODE was GENERATED by Greendao, does not EDIT. Enable "Keep" sections if you want to edit. /** * Entity mapped to table "NOTE".    */public class Note {private Long ID; /** not-null value.    */private String text;    Private String comment;    Private Java.util.Date Date;    Public note () {} public note (Long ID) {this.id = ID;        The public Note (Long ID, string text, string comment, Java.util.Date Date) {this.id = ID;        This.text = text;        This.comment = comment;    This.date = date;    } public Long GetId () {return id;    } public void SetId (Long id) {this.id = ID; }/** not-null value.    */Public String GetText () {return text; }/** Not-null value; Ensure this value is available before it's saved to the database.    */public void SetText (String text) {this.text = text;    } public String Getcomment () {return comment; } public void Setcomment (STRing comment) {this.comment = comment;    } public java.util.Date GetDate () {return Date;    The public void SetDate (java.util.Date date) {this.date = date; }}





Using Greendao in Android

First, add the Greendao.jar package and copy the generated classes from the Java project to the Android project. Then you can make the additional deletion check, the specific code is as follows.

public class Mainactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);D evopenhelper helper = new Daomaster.devopenhelper ( This, "greendao_test.db", null); Sqlitedatabase db = Helper.getwritabledatabase ();D aomaster daomaster = new Daomaster (db);D aosession DaoSession = Daomaster.newsession (); final Notedao Notedao = Daosession.getnotedao ();//Notedao.insertintx (new Arraylist<note > (), True); Findviewbyid (r.id.but). Setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) { TODO auto-generated method Stubnote Note = new Note (); Note.setdate (new Date ()); Note.setid (System.currenttimemillis () ); Note.settext ("Ttteesstt"); Notedao.insert (note);}}); Findviewbyid (R.id.get). Setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO Auto-generated method Stublist<note> L = Notedao.loadall (); StringBuilder builder = new StringBuilder (); (Note note:l) {builder.append (Note.getid ()). Append (""). Append (Note.gettext ()). Append (""). Append (Note.getdate ()). Append ("\ n");} Toast.maketext (Getapplicationcontext (), builder, 1). Show ();}});}}

Since the Notedao inherits from the Abstractdao, it can directly invoke the various additions and deletions to the search method


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android ORM Framework Greendao

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.