Android GreenDao database framework Learning (1), androidgreendao

Source: Internet
Author: User

Android GreenDao database framework Learning (1), androidgreendao

1. GreenDao Overview:

GreenDAO Is An ORM solution that helps Android Developers quickly map Java objects to SQLite database forms. GreenDAO features the highest operating efficiency and the lowest memory consumption. Official Website: http://greendao-orm.com /... Github: https://github.com/greenrobot/greendao.

1. DaoCore: the core library of greenDAO.
2. DaoExample: greenDAO sample project.
3. DaoExampleGenerator: the entity class of the greenDAO sample and the generator project of the DAO component.
4. DaoGenerator: the DAO Component Generator project of greenDAO.
5. DaoTest: greenDAO test project.
6. PerformanceTestOrmLite: ormlite framework performance testing project

Ii. Generate the GreenDao framework:

Before using the download greendao-generator.jar and freemarker. jar two packages, freemarker needs this to generate a java file, to see how to use:

(1). First generate 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 with the following name: 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 the database version number, the second is to automatically generate the path Schema schema = new Schema (1, "de. greenrobot. daoexample "); addNote (schema); // generate the path under which the file is generated, my path is to create a new DaoGenerator () under the src-gen directory of the new android project (). generateAll (schema ,".. /AndroidTestGreenDao/src-gen ");} private static void addNote (Schema schema) {// Entity is an Entity named Note Entity note = schema. addEntity ("Note"); // primary key, which can be set to auto-increment. autoincrement () note. addIdProperty (); // The attribute in the object class (that is, the field in the Table). It cannot be blank. You can see the method name for other settings to see what the note means. addStringProperty ("text "). notNull (); note. addStringProperty ("comment"); note. addDateProperty ("date") ;}</span>

Note: src-gen must be created first to succeed.
Generated successfully:



Schema class: a container that stores objects.

Entity class: it can be used to map object classes to corresponding tables.

DaoGenerator: the builder of entity classes and DAO components. generateAll generates a file storage directory based on schema and directory parameters, and extracts all entity from the schema, the corresponding entities and DAO components are generated based on the freemaker template.


3. Use the GreenDao framework:

Create an android project named android (the path specified by java above is under this project): AndroidTestGreenDao


Here the greendao-1.3.0-beta-1.jar is taken out under libs under the DaoExample directory downloaded by github.

Create a database:

Public class MainActivity extends Activity {private SQLiteDatabase db; private DaoMaster master; private DaoSession session; private 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); db = helper. getWritableDatabase (); master = new DaoMaster (db); session = master. newSession (); noteDao = session. getNoteDao (); // Add a data Note = new note (10L, "1", "1", new Date (); noteDao. insert (note );}}


Note: ing object to table NOTE.

NoteDAO: The Code shows that this class provides methods for generating, creating, and deleting tables, and the class inherits AbstractDao (one of greenDAO core classes, encapsulates various methods for operating table data ).

DaoMaster: inherits from AbstractDaoMaster to create databases and manage database connections.

DaoSession: inherits from AbstractDaoSession, which is the entry of all DAO objects and obtains the DAO component of the entity.





Related Article

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.