It 's time to forget about Android SQLite! Android Official ORM Database Bedroom Technology Solution Brief (i)
Android's Romm is a package of ORM database solutions that are officially integrated by Android. The Android House and the historical ORM database such as Android Ormlite (see appendix article), Android Greendao and so on, have many same design ideas and concepts, but the Android family also absorbs the length of the public ORM database, Have their own new innovations and improvements. I will write a number of separate articles, point by step Introduction to the official Android ORM Database Technology solution: the office.
(a) using the Android bedroom, first to add a reference to Gradle, I am the version 1.0 based on the bedroom. 0:
Implementation ' android.arch.persistence.room:runtime:1.0.0 ' annotationprocessor ' android.arch.persistence.room:compiler:1.0.0 '
(b) Similar to other ORM databases, the Android guest needs to build a model of database tables and Java objects, User.java:
Package Zhangphil.demo;import Android.arch.persistence.room.columninfo;import Android.arch.persistence.room.entity;import android.arch.persistence.room.primarykey;/** * Created by Phil on 2017/11 /22. */@Entity (tableName = "user_table") public class User { @PrimaryKey (AutoGenerate = True) the public int id; @ColumnInfo (name = "UserName") public String name; @ColumnInfo (name = "Userage") public int age; @ColumnInfo (name = "UpdateTime") public long UpdateTime;}
Note @entity tells the Android class that it will be a table in a database. TableName If the developer does not customize, then the Android system will use the class name as the table names in the database by default. In this example, the user specifies that the table is named "User_table", and the developer can name it according to his own situation.
@PrimaryKey Callout This field is the primary key in the table, which can be based on the primary key additions and deletions of the database and so on. When set AutoGenerate = True, the primary key will grow from itself. @ColumnInfo the name followed by the annotation, indicating that the defined Java variable will be used as a column in the table. For example, in user
@ColumnInfo (name = "UserName") public String name;
Then the data stored in a column of username tables in the database is name.
(iii) building a DAO for user. Userdao.java:
Package Zhangphil.demo;import Android.arch.persistence.room.dao;import Android.arch.persistence.room.Delete; Import Android.arch.persistence.room.insert;import Android.arch.persistence.room.onconflictstrategy;import Android.arch.persistence.room.query;import Android.arch.persistence.room.update;import java.util.List;/** * Created by Phil on 2017/11/22. */@Daopublic interface Userdao { /** * Query * * @return * /@Query ("SELECT * from User_table") Public list<user> getAllUsers (); /** * Add * * @param users * /@Insert (onconflict = onconflictstrategy.replace) public void Insertuser (User ... users); /** * Update * * @param users * * @Update public void UpdateUser (User ... users); /** * Delete * * @param users * * @Delete public void DeleteUser (User ... users);}
The @Dao note indicates that the Java class is a DAO object in an Android box. DAO in the Android interface can be defined in the form of an interface. Queries in the Android room are defined in SQL standard statements. For example, defined in Userdao:
/** * Query * * @return * /@Query ("SELECT * from User_table") public list<user> GetAllUsers ();
The SQL statement will query out all of the contents from the Block table "user_table" that I created earlier, and then return a collection. Additional operations on the database (@Insert), deletion (@Delete), modification (update, @Update) are indicated by annotations. These basic operations can be done in accordance with the @primarykey primary key defined in user.
(iv) construct the database.
This step is basically a normalized code in the Android bedroom technology, which is written in a more disciplined and Userdatabase.java form:
Package Zhangphil.demo;import Android.arch.persistence.room.database;import android.arch.persistence.room.roomdatabase;/** * Created by Phil on 2017/11/22. */@Database (entities = {User.class}, Version = 1) public abstract class Userdatabase extends Roomdatabase { Public abstract Userdao Getuserdao ();}
The most important function of userdatabase is to provide the developer with various DAO. Once the developer gets the DAO, it can manipulate the data in the Android data in the same way as a basic Java object instance.
The final code structure
(v) test my Android Mainactivity.java in the e-book. Note that database operations are involved and are to be run in a background thread. Mainactivity.java:
Package Zhangphil.demo;import Android.arch.persistence.room.room;import android.support.v7.app.AppCompatActivity; Import Android.os.bundle;import Android.util.log;import Java.util.list;public class Mainactivity extends appcompatactivity {private String TAG = "Output"; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); New Thread (New Runnable () {@Override public void run () {databaseoperation (); }}). Start (); } private void Databaseoperation () {Userdatabase muserdatabase = Room.databasebuilder (Getapplicationcontext (), Userdatabase.class, "users"). Build (); Userdao Muserdao = Muserdatabase.getuserdao (); Write Database Log.d (TAG, "Start writing data ..."); Writedatabase (Muserdao, "Zhang San", 18); Writedatabase (Muserdao, "John Doe", 19); LOG.D (TAG, "write database Complete."); Read Database Log.d (TAG, "1th reading Database"); Readdatabase (Muserdao); Number of updatesAccording to the library UpdateUser (Muserdao); Read Database Log.d (TAG, "2nd reading database"); Readdatabase (Muserdao); Delete data, according to the primary key ID DeleteUser (Muserdao, 1); Read Database Log.d (TAG, "3rd reading Database"); Readdatabase (Muserdao); LOG.D (TAG, "========================"); LOG.D (TAG, "full end of the database Operations transaction"); LOG.D (TAG, "========================"); } private void Readdatabase (Userdao dao) {log.d (TAG, "read database ..."); list<user> users = Dao.getallusers (); for (User u:users) {log.d (TAG, U.id + "," + U.name + "," + U.age + "," + U.updatetime "); } log.d (TAG, "read database completed."); } private void Writedatabase (Userdao dao, String name, int age) {User user = new user (); User.Name = name; User.age = age; User.updatetime = System.currenttimemillis (); Dao.insertuser (user); } private void UpdateUser (Userdao dao) {log.d (TAG, "Update Database ..."); User U = new user (); U.id = 2; u.name = "Zhao Wu"; U.age = 20; U.updatetime = System.currenttimemillis (); Dao.updateuser (U); LOG.D (TAG, "Update database complete."); } private void DeleteUser (Userdao dao, int id) {LOG.D (TAG, "delete Database ..."); User U = new user (); U.id = ID; Dao.deleteuser (U); LOG.D (TAG, "delete database complete."); }}
Mainactivity.java implements the basic functionality of adding two data entries to the database. Then in the first read database operation, read from the data to see if it has been written in.
Then update the database, according to the user's PrimaryKey primary key ID update, I have the database in the primary key ID 2 of the data row update. Read the database again for the 2nd time to verify that my update operation was successful.
Then, according to the primary key to delete the Id=1 database data row, the 3rd time to read the database, verify the operation of the database deletion. Logcat output After code is run:
11-23 10:49:22.631 19616-19641/zhangphil.demo d/output: Start writing data ... 11-23 10:49:22.688 19616-19641/zhangphil.demo d/output: Write to database complete. 11-23 10:49:22.688 19616-19641/zhangphil.demo d/output: 1th Read Database 11-23 10:49:22.688 19616-19641/zhangphil.demo d/output: Read database ... 11-23 10:49:22.689 19616-19641/zhangphil.demo d/output: 1, Zhang San, 18,151140536263111-23 10:49:22.689 19616-19641/ Zhangphil.demo d/output: 2, John Doe, 19,151140536267411-23 10:49:22.689 19616-19641/zhangphil.demo d/output: Read database complete. 11-23 10:49:22.689 19616-19641/zhangphil.demo d/output: Update database ... 11-23 10:49:22.692 19616-19641/zhangphil.demo d/output: Update database complete. 11-23 10:49:22.692 19616-19641/zhangphil.demo d/output: 2nd Read Database 11-23 10:49:22.692 19616-19641/zhangphil.demo d/output: Read database ... 11-23 10:49:22.693 19616-19641/zhangphil.demo d/output: 1, Zhang San, 18,151140536263111-23 10:49:22.693 19616-19641/ Zhangphil.demo d/output: 2, Zhao, 20,151140536268911-23 10:49:22.693 19616-19641/zhangphil.demo d/output: Read database complete. 11-23 10:49:22.693 19616-19641/zhangphil.demo d/output: Delete database ... 11-23 10:49:22.696 19616-19641/zhangphil.demo d/output: DeleteExcept the database is complete. 11-23 10:49:22.696 19616-19641/zhangphil.demo d/output: 3rd read database 11-23 10:49:22.696 19616-19641/zhangphil.demo d/output: Read the database ... 11-23 10:49:22.698 19616-19641/zhangphil.demo d/output: 2, Zhao, 20,151140536268911-23 10:49:22.698 19616-19641/ Zhangphil.demo d/Output: Read database completed. 11-23 10:49:22.698 19616-19641/zhangphil.demo d/output: ========================11-23 10:49:22.698 19616-19641/zhangphil.demo d/output: This round of database operations transactions all over 11-23 10:49:22.699 19616-19641/zhangphil.demo d/output: ====== ==================
Appendix:
1, "Introduction to Android Ormlite database" link: http://blog.csdn.net/zhangphil/article/details/46878075
2, "Android ormlite foreigncollection Associated External Collection" Link: http://blog.csdn.net/zhangphil/article/details/46891021
Android Official ORM Database Bedroom Technology Solution Brief (i)