Objective:
Early this morning received Greendao 3.0 official release of the news, since 2014 contact Greendao so far, the project has been using the GREENDAO framework to handle database operations, I use the database route Sqlite---->ormlite----> Greendao. Today, the day has been busy with the company's project needs, can only work overtime at night to learn the latest Greendao 3.0 use of the way.
Greendao Introduction:
Greendao is an object-relational mapping (ORM) framework that provides an interface to manipulate relational databases by manipulating objects, which makes it easier and easier to manipulate databases. As shown in the following:
Website address: http://greenrobot.org/greendao/
Github:https://github.com/greenrobot/greendao
Greendao Advantages:
-
High performance, known as Android's fastest relational database
- Low memory footprint
-
Library files are smaller, less than 100K, compile time is low, and 65K method limits can be avoided
-
Support database encryption Greendao support Sqlcipher for database encryption Sqlcipher can refer to this blog Android data storage SQLite using Sqlcipher database encryption combat
- Simple and easy to use API
Greendao 3.0 Changes:
Those of you who have used Greendao know that it is tedious and expensive to generate Java Data Objects (entities) and DAO objects by creating a new Greendaogenerator project before 3.0.
Greendao 3.0 The biggest change is to use annotations to build Java Data Objects and DAO objects in a compiled manner.
Greendao 3.0 How to use: 1.) Add the following configuration in Build.gradle
Buildscript { repositories { mavencentral () } dependencies { classpath ' Org.greenrobot: greendao-gradle-plugin:3.0.0 ' }}apply plugin: ' Org.greenrobot.greendao ' dependencies { compile ' org.greenrobot:greendao:3.0.1 '}
2.) New entity
@Entity Public class User { @Id privatelong Id; Private String name; Private int Age ; // the following omitted Setter/getter}
At this point compile the automatic generation Daomaster, Daosession, Dao, the default location:
3.)Gradle plug-in configuration
For example, above you want to specify the Daomaster, Daosession, DAO locations
Greendao { targetgendir ' Src/main/java '}
- schemaversion: Database schema version, also can be understood as the database version number
- daopackage: Set Daomaster, Daosession, DAO package name
- targetgendir: Set Daomaster, Daosession, DAO directories
- targetgendirtest: Setting up the build Unit test catalog
- generatetests: Set up automatic generation of unit test cases
4.) Entity@Entity Annotations
- schema: tells Greendao which schema the current entity belongs to
- Active: marks an entity as active, with an update, delete, and Refresh method for the active entity
- nameindb: alias used in data, default is the class name of the entity
- indexes: Define an index that can span multiple columns
- createindb: tag Creation database table
5.) Basic attribute annotations
- @Id: primary key Long/long type, can be set by @Id (AutoIncrement = true) self-growth
- @Property: sets the column name for a non-default relationship map, by default, using the field name example: @Property (nameindb= "name")
- @NotNul: Setting the current column of a database table cannot be empty
- @Transient : Columns for database tables are not generated after you add a secondary tag
6.) Index annotations
- @Index: use @index as a property to create an index, set an index alias by name, or add a constraint to an index by using the unique
- @Unique: added a unique constraint to the database column
7.) Relationship Annotations
- @ToOne: Define a relationship with another entity (an entity object)
- @ToMany: define relationships with multiple entity objects
Greendao 3.0 Simple Combat: 1. We can get Daomaster, Daosession, DAO class by using the above method
This declares a database manager single case
Public classDBManager {Private Final StaticString dbName = "test_db"; Private StaticDBManager minstance; PrivateDaomaster.devopenhelper Openhelper; Privatecontext Context; PublicDBManager (Context context) { This. Context =context; Openhelper=NewDaomaster.devopenhelper (context, DbName,NULL); } /*** Get a singleton reference * *@paramContext *@return */ Public StaticDBManager getinstance (context context) {if(Minstance = =NULL) { synchronized(DBManager.class) { if(Minstance = =NULL) {minstance=NewDBManager (context); } } } returnminstance; }}2.) Get a readable writable database
Readable database
/** * Get a readable database * /private sqlitedatabase getreadabledatabase () { if NULL ) { newnull); } = openhelper.getreadabledatabase (); return db; }
Writable database
/** * Get writable database * /private sqlitedatabase getwritabledatabase () { if NULL ) { newnull); } = openhelper.getwritabledatabase (); return db; }
3.) Inserting data
/*** Insert a record * *@paramUser*/ Public voidinsertuser (user user) {daomaster daomaster=NewDaomaster (Getwritabledatabase ()); Daosession daosession=daomaster.newsession (); Userdao Userdao=Daosession.getuserdao (); Userdao.insert (user); } /*** Insert User Collection * *@paramUsers*/ Public voidInsertuserlist (list<user>users) { if(Users = =NULL||Users.isempty ()) { return; } daomaster Daomaster=NewDaomaster (Getwritabledatabase ()); Daosession daosession=daomaster.newsession (); Userdao Userdao=Daosession.getuserdao (); Userdao.insertintx (users); }4.) Delete Data
/** * Delete a record * @param user */public void deleteuser (user user) { new Daomaster (Getwritabledatabase ()); = daomaster.newsession (); = Daosession.getuserdao (); Userdao.delete (user); }
5.) Update data
/** * Update a record * @param user */public void updateUser (user user) { new Daomaster (Getwritabledatabase ()); = daomaster.newsession (); = Daosession.getuserdao (); Userdao.update (user); }
6.) Querying data
/*** Query user list*/ PublicList<user>queryuserlist () {daomaster daomaster=NewDaomaster (Getreadabledatabase ()); Daosession daosession=daomaster.newsession (); Userdao Userdao=Daosession.getuserdao (); QueryBuilder<User> QB =Userdao.querybuilder (); List<User> list =qb.list (); returnlist; } /*** Query user list*/ PublicList<user> Queryuserlist (intAge ) {Daomaster Daomaster=NewDaomaster (Getreadabledatabase ()); Daosession daosession=daomaster.newsession (); Userdao Userdao=Daosession.getuserdao (); QueryBuilder<User> QB =Userdao.querybuilder (); Qb.where (USERDAO.PROPERTIES.AGE.GT (age)). ORDERASC (UserDao.Properties.Age); List<User> list =qb.list (); returnlist; }7.) Test procedure
DBManager DBManager = dbmanager.getinstance ( This); for(inti = 0; I < 5; i++) {User User=NewUser (); User.setid (i); User.setage (i* 3); User.setname ("No." + i + "person"); Dbmanager.insertuser (user); } List<User> userlist =dbmanager.queryuserlist (); for(User user:userlist) {LOG.E ("TAG", "queryuserlist--before-->" + user.getid () + "--" + user.getname () + "--" +user.getage ()); if(User.getid () = = 0) {dbmanager.deleteuser (user); } if(User.getid () = = 3) {user.setage (10); Dbmanager.updateuser (user); }} userlist=dbmanager.queryuserlist (); for(User user:userlist) {LOG.E ("TAG", "Queryuserlist--after--->" + user.getid () + "---" + user.getname () + "--" +user.getage ()); }
Run results
Summarize:
This paper mainly introduces the use of annotations in Greendao 3.0, as well as the implementation of simple additions and deletions, the next step is to learn more Greendao more complex ways to use.
Greendao 3.0 of Android data storage (i)