Today, when I was watching other people's source code, I accidentally discovered a very useful database artifact. Let me share it here.
What is dbflow?
dbflow is a tool library for Android SQLite ORM that uses annotation manipulation. Simply put, it is a Java library that operates on the SQLite database.
ORM (Object-relational mapping), translated as object-relational mapping in Chinese, is a technology to solve the phenomenon of mismatch between object-oriented and relational databases. Simply put, ORM automatically persists objects in the program into a relational database by using metadata that describes the mapping between the object and the database.
Why use dbflow?
1. Speed
Based on AnnotationProcessing (annotation processor), code generation is generated at compile time, and the runtime performance is excellent. Through multiplexing objects and caching mechanisms, a good speed experience is obtained.
2. Extensibility
The data form is mapped to a data object, and the Model class is inherited through the object. Generally speaking, it is enough to inherit the BaseModel class and add annotations to the members in the class to generate the required form.
3. Query statement
If you have used other libraries such as greenDao, the query statements are very close to SQL statements.
4. Based on sqlite
There is no restriction on the platform, you can use dbflow wherever there is SQLite.
5. Open source
Source code is a good thing, and those with ability can check it out. https://github.com/Raizlabs/DBFlow
How to use dbflow
Configure the environment
No matter what you want to learn, the environment is the main thing. The first step for a three-party library is to import the class library.
First add the maven address in the main build.gradle:
allprojects { repositories { maven { url "https://jitpack.io" } }} Secondly, add dependencies in the base class module:
The code copy is as follows:
def dbflow_version = "4.2.4" dependencies { annotationProcessor "com.github.Raizlabs.DBFlow:dbflow-processor:${dbflow_version}" compile "com.github.Raizlabs.DBFlow:dbflow-core:${dbflow_version}" compile "com.github.Raizlabs.DBFlow:dbflow-rx2:${dbflow_version}" compile "com.github.Raizlabs.DBFlow:dbflow-rx2:${dbflow_version}" compile "com.github.Raizlabs.DBFlow:dbflow-sqlcipher:${dbflow_version}" }
If you need to add kotlin code, check the above source code address and see the instructions below.
use
Add init in Application
FlowManager.*init*(this);
Create a database
@Database(name = DbFlowData.*DBNAME*,version = DbFlowData.*VERSION*) public class DbFlowData { public static final String *DBNAME*="DbFlowData"; public static final int *VERSION*=1; }Use annotation Database, name database name version database version
Create a form
@Table(database = DbFlowData.class) public class DbFlowModel extends BaseModel { @PrimaryKey(autoincrement = true) public int id; @Column public String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; }} Use annotations Table database slave database objects, form attributes use various annotations, PrimaryKey primary keys, and Colum columns.
Note: The BaseModel we inherited here is provided by DBFlow, and it can not be inherited. The differences are only different in terms of addition, deletion, modification and search.
After creation, click Build->make and the necessary code will be generated under ../build/generated/source/apt/debug.
insert:
Create a form object, after assigning the value, call the save method to insert the operation.
The code copy is as follows: DbFlowModel dbFlowModel = new DbFlowModel(); dbFlowModel.name = name; dbFlowModel.save();
delete:
Code similar to SQL statements to operate
SQLite.*delete*() .from(DbFlowModel.class) .where(DbFlowModel_Table.*id*.eq(id), DbFlowModel_Table.*name*.eq(name)) .execute();
Query:
This has many complex query operations. If you want to know more, please read the official documentation.
The code copy is as follows: List<DbFlowModel> list = SQLite.*select*().from(DbFlowModel.class).queryList();
renew:
They are all similar.
SQLite.*update*(DbFlowModel.class) .set(DbFlowModel_Table.*name*.eq("PXXXX")) .where(DbFlowModel_Table.*name*.eq("P0000")) .execute(); Note: After creating a new form, you need to upgrade the database version, otherwise an error will be reported.
Here I will introduce the basic use of dbflow, mainly for sharing and recording some useful things.
Source code: https://github.com/xiaogoudandan/WilliamApp
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.