Currently the most popular ORM data on Android are Greendao, Ormlite, activeandroid and so on.
Where Greendao is not based on reflection, the performance is naturally the best, but the use of high cost ratio, ormlite, activeandroid is the use of annotations and reflection, encapsulation is better, the advantage is the ease of use strong. I personally think that most applications are not very high on the database, if saving or pulling the data is not very large, it is difficult to open a large gap, so compared to performance, I am more inclined to ease of use.
Dbflow, combining the advantages of activeandroid, Schematic, Ollie,sprinkles and other libraries. It is also not based on reflection, so the performance is very high and the efficiency follows Greendao immediately thereafter. Based on annotations, using apt technology, the operation classes are generated during the compilation process and are used in a similar manner to activeandroid.
Features: 1, seamless support of multiple databases, 2, the use of annotation processing increase speed; 3, Modelcontainer class library can directly parse data such as JSON, 4, increase the flexibility of the rich interface. GitHub Warehouse: Https://github.com/Raizlabs/DBFlow
Dbflow may use in the domestic people are not many, so the introduction of Chinese is very few, so there is this article, next let us learn dbflow together.
First, the introduction of dependency, initialization
Need to introduce apt and maven to configure the project's Build.gradle
Buildscript { repositories { jcenter () } dependencies { classpath ' com.android.tools.build: Gradle:2.0.0-beta6 ' classpath ' com.neenbedankt.gradle.plugins:android-apt:1.8 ' }}allprojects { repositories { jcenter () maven {URL "Https://jitpack.io"}}}
Configure the app's Build.gradle
Apply plugin: ' com.android.application ' Apply plugin: ' com.neenbedankt.android-apt ' def dbflow_version = ' 3.0.0-beta4 ' Android { compilesdkversion buildtoolsversion "23.0.2" defaultconfig { ApplicationID " Cn.taoweiji.dbflowexample " minsdkversion targetsdkversion versioncode 1 versionname" 1.0 " } Buildtypes { Release { minifyenabled false proguardfiles getdefaultproguardfile (' Proguard-android.txt '), ' Proguard-rules.pro ' } }}dependencies { compile filetree (dir: ' Libs ', Include: [' *.jar ']) apt "com.github.raizlabs.dbflow:dbflow-processor:${dbflow_version}" compile " Com.github.raizlabs.dbflow:dbflow-core:${dbflow_version} " compile" com.github.raizlabs.dbflow:dbflow:${ Dbflow_version} "}
The dbflow needs to be initialized in application's OnCreate
Class Myapplication { superFlowmanager. Init (this);}}
Remember to modify Androidmanifest.xml
<application android:name= ". MyApplication ". />
Second, database creation, table creation
Defining a Database
I have defined here a database named Appdatabase, which can be defined according to your liking.
@Database (name = appdatabase.name, Version = appdatabase.version) public class Appdatabase { //database name public Static final String NAME = "Appdatabase"; Database version number public static final int version = 1;}
To create a database object
Must inherit Basemodel,basemodel contains basic database operations (save, Delete, update, insert, exists), see the following code can find this table is associated with the database defined above, people ID is the self-increment ID.
@ModelContainer @table (Database = appdatabase.class) public class people extends Basemodel { //self-increment ID @PrimaryKey (AutoIncrement = True) public Long ID; @Column public String name; @Column public int gender;}
After writing the data Table object, click android Studio Build->make Project (Mac's child Shoes direct COMMAND+F9) will be compiled with APT,
View catalog (My People class is placed in cn.taoweiji.dbflowexample.db)
app/build/generated/source/apt/debug/cn/taoweiji/dbflowexample/db
You can see the automatic generation of People_adapter, People_container, people_table, which people_table in the later use has a great effect, it is recommended to look at its structure.
Third, increase the deletion
Because the data table object inherits the Basemodel, it already contains a lot of operations
People people = New people ();p eople.name = "Wiki";p Eople.gender = 1;people.save ();//people.update ();//people.delete (); LOG.E ("Test", String.valueof (people.id));
Delete, update and other operations on their own experience, there is not much to say.
Iv. Inquiries
Returns all query results list<people> peoples = new Select (). from (People.class). Querylist ();//Returns a single query result people people = new Select (). from (People.class). Querysingle ();//Query gender = 1 for all peoplelist<people> Peoples2 = new Select (). from ( People.class). where (People_Table.gender.eq (1)). Querylist ();
Dbflow the query way for reference activeandroid, but more powerful than the Activeandroid function.
Iv. transactions, bulk storage
A transaction is a data must have, if you save 10,000 data, a single save must be very slow, so you need to use transactions, batch storage. Dbflow's business is very powerful, but also very complex to use, here is a simple introduction to bulk storage, more information please see the official documents
Https://github.com/Raizlabs/DBFlow/blob/master/usage/Transactions.md
list<people> peoples = new arraylist<> (); for (int i = 0; i <; i++) { people people = New people (); people.name = "Wiki"; People.gender = 1; Peoples.add (people);} Save in real time, save new Savemodeltransaction<> (Processmodelinfo.withmodels (Peoples)). OnExecute ();//asynchronous Save, using Async, If you query immediately, you may not be able to find the results//transactionmanager.getinstance (). Addtransaction (New savemodeltransaction<> ( Processmodelinfo.withmodels (peoples));
V. Database Upgrade (Add table, add field, etc.)
If the new table does not require special handling, modify the Appdatabase version number directly.
If you need to add a new field, in addition to the need to modify Appdatabase version of the extra, also need to do special processing, Dbflow description is: Migrations.
Example: Add an email field to people
1th step, modify the database version number
@Database (name = appdatabase.name, Version = appdatabase.version) public class Appdatabase { //database name public Static final String NAME = "Appdatabase"; Database version number, modified here 2 public static final int version = 2;}
2nd step, you need to modify the data Table object structure, add email
@ModelContainer @table (Database = appdatabase.class) public class people extends Basemodel { //self-increment ID @PrimaryKey (AutoIncrement = True) public Long ID; @Column public String name; @Column public int gender; @Column public String email;}
3rd step, after the second step, build (Android studio Build->make Project, Mac's child Shoes direct COMMAND+F9), update people_table via APT, Next Write Migrations
@Migration (Version = 2, database = Appdatabase.class) public class Migration_2_people extends altertablemigration< people> {public migration_2_people (class<people> table) { super (table); } @Override public void Onpremigrate () { addcolumn (Sqlitetype.text, People_Table.email.getNameAlias (). GetName ());} }
The class name can be more self-liking definition, my personal rule is, according to the database version number and need to update the data table to name, need to note is: Version = 2
The database upgrade is done.
Summary: This article simply introduces the basic functions of Dbflow, Dbflow also has many powerful features, such as multi-database support, powerful Model caching, and also supports Kotlin language (the new language running in the Java Virtual machine). I only used Greendao, Activeandroid, afinal, Dbflow database, so in my opinion, Dbflow is the best database I have used, the performance is very good, the use is very simple, highly recommended.
I shared the Dbflow configuration on GitHub.
Https://github.com/taoweiji/DBFlowExample
Getting Started with Android High performance ORM Database Dbflow