Android GreenDAO3.0 -- Introduction, androidgreendao3.0
Introduction
Recently, learning is scattered and all kinds of knowledge are mixed, so you can write down the learning records to avoid forgetting.
Http://greenrobot.org/greendao/documentation/introduction/
First, we will introduce what greenDAO is, and add GreenDAO between the java object and the SQLite database, so that our application does not directly operate the database, but calls the interface provided by greenDAO to complete database operations.
The advantage of doing so is that we can avoid writing a lot of database operation code, and avoid some low-level errors due to fatigue.
GreenDAO core class
- DaOMaster -- this class holds database objects, manages DAO classes (a class file), and contains methods for creating and deleting table data tables. Its internal classes OpenHelper and DevOpenHelper are the implementation of the SQLite interface SQLiteOpenHelper. Therefore, this class has the potential to operate databases.
- DaoSession: manages all available DAO objects. DaoSession provides methods similar to insert, load, update, refresh, and delete.
- DAOs -- all called Data access objects. For Entity, DAO can be generated through GreenDao. In addition, the database operation methods provided by this object are redundant DAOSession.
- Entity -- java POJO or JavaBean
It is the relationship between core classes. It is a very obvious structural relationship.
GreenDAO core class initialization
This code can be inserted in the application of the Android app project.
// do this once, for example in your Application classhelper = new DaoMaster.DevOpenHelper(this, "notes-db", null);db = helper.getWritableDatabase();daoMaster = new DaoMaster(db);daoSession = daoMaster.newSession();// do this in your activities/fragments to get hold of a DAOnoteDao = daoSession.getNoteDao();