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 for you to manipulate the database. As shown in the following illustration:
Website address: http://greenrobot.org/greendao/
Github:https://github.com/greenrobot/greendao
Greendao Advantages:
High performance, known as Android fastest relational database memory footprint Small
The library file is small, less than 100K, compile time is low, and can avoid the 65K method limit
Support for database encryption Greendao support Sqlcipher for database encryption Sqlcipher can refer to this blog of the Android data store SQLite use Sqlcipher database encryption combat simple and Easy-to-use API
Configure Greendao: Engineering Gradle:
Classpath ' org.greenrobot:greendao-gradle-plugin:3.1.0 '
moudle Gradle:
Apply plugin: ' Org.greenrobot.greendao '
greendao{
schemaversion 1//version number
Targetgendir ' Src/main/java '// Default file generation location
}
compile ' org.greenrobot:greendao:3.1.0 '
code Example:
To create an entity class:
@Entity public
class User {
@Id
private Long Id;
@Property (nameindb = "USERNAME")
private String USERNAME;
@Property (nameindb = "nickname")
private String nickname;
}
Then make project. Automatically generate Daomaster, Daosession, Userdao classes.
Note:
1.) Entity @entity AnnotationSchema: tells Greendao which schema active the current entity belongs to: Mark an entity active, active entities have update, delete, and Refresh methods nameindb: Aliases used in data, using the entity's class name by default indexes: Defining indexes, Can span multiple columns CREATEINDB: Markup to create a database table
2.) Basic attribute Annotation@Id: Primary key long, can be set by @Id (AutoIncrement = True) from the growth @Property: set a non-default relationship map corresponding to the name of the column, the default is the use of field names for example: @Property (nameindb= "name") @NotNul: Set the current column of the database table cannot be empty @Transient: columns of the database table are not generated after the secondary tag is added
3.) Index annotation@Index: Create an index using @index as a property, set the index alias by name, or add a constraint to the index by unique @Unique: A unique constraint was added to the database column
4.) Relationship Annotation@ToOne: Define a relationship to another entity (an entity object) @ToMany: Define relationships to multiple entity objects
single-instance reference initialization class:
public class DBHelper {private static daomaster.devopenhelper mhelper;
private static Sqlitedatabase db;
private static Daomaster Mdaomaster;
private static daosession mdaosession;
private static DBHelper minstance;
Public DBHelper () {}/** * Single case Reference * * @return/public static DBHelper getinstance () {
if (minstance = = null) {synchronized (Dbhelper.class) {if (minstance = = null) {
Minstance = new DBHelper ();
}} return minstance; /** * Initialize Greendao * @param name of data table/public daosession initdatabase (context Context,strin
G name) {//through Daomaster's inner class devopenhelper, you can get a handy sqliteopenhelper object.
You may have noticed that you don't need to write SQL statements like "create table", because Greendao has done it for you.
Note: The default daomaster.devopenhelper deletes all tables when the database is upgraded, meaning that this will result in loss of data.
Therefore, in the formal project, you should also do a layer of encapsulation, to achieve database security upgrades. Mhelper = new Daomaster.devopenhelper (context, name, NULL);
db = Mhelper.getwritabledatabase ();
Note: This database connection belongs to Daomaster, so multiple sessions refer to the same database connection.
Mdaomaster = new Daomaster (db);
Mdaosession = Mdaomaster.newsession ();
return mdaosession;
}
}
Mainactivity.java (including Add, delete, check, change)
public class Mainactivity extends Appcompatactivity implements View.onclicklistener {private Button Madd, Mdelete, M
Update, Mfind;
Private TextView Mcontext;
Private Userdao Userdao;
Private DBHelper DBHelper = Dbhelper.getinstance ();
Private String table_name = "MY.DB3";//data table name @Override protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Initview ();
Initevent ();
Initialize Object Userdao = Dbhelper.initdatabase (this, table_name). Getuserdao ();
private void Initevent () {Madd.setonclicklistener (this);
Mdelete.setonclicklistener (this);
Mupdate.setonclicklistener (this);
Mfind.setonclicklistener (this);
private void Initview () {mcontext = (TextView) Findviewbyid (R.id.textview);
Madd = (Button) Findviewbyid (R.id.button);
Mdelete = (Button) Findviewbyid (R.id.button2); Mupdate = (Button) Findviewbyid (R.id.button3);
Mfind = (Button) Findviewbyid (R.ID.BUTTON4);
@Override public void OnClick (View v) {switch (V.getid ()) {case R.id.button:
Adddate ();
Break
Case R.id.button2:deletedate ();
Break
Case R.id.button3:updatedate ();
Break
Case R.id.button4:finddate ();
Break }/** * Add data/private void Adddate () {User user = new user (null, "Zhangsan" + new Rando
M (). Nextint (100), "John");
Userdao.insert (user);
Mcontext.settext (User.getusername () + user.getnickname ());
/** * Delete data * * private void Deletedate () {Deleteuserbyid (2); /** * Delete User * Based on primary key ID/public void Deleteuserbyid (long id) {u SerdaO.deletebykey (ID);
/** * Change data/private void Updatedate () {User user = new user ((long) 5, "Lisi", "Dick");
Userdao.update (user);
/** * Find data * * private void Finddate () {list<user> users = Userdao.loadall ();
String userName = ""; for (int i = 0; i < users.size (); i++) {userName + = Users.get (i) getusername () + "," + users.get (i). getnic
Kname () + "\ n";
} mcontext.settext ("Query all data ==>" + userName); }
}
XML
<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android= "http://schemas.android.com/apk/res/" Android "xmlns:tools=" Http://schemas.android.com/tools "android:layout_width=" Match_parent "Android:layout_hei ght= "Match_parent" > <textview android:layout_width= "match_parent" W
Rap_content "android:text=" Hello world! "
Android:id= "@+id/textview" android:layout_below= "@+id/button3" android:layout_alignparentstart= "true" Android:layout_marginstart= "11DP" android:layout_margintop= "36DP"/> <button android:layout _width= "Wrap_content" android:layout_height= "wrap_content" android:text= "Increase" android:id= "@+id/butto" N "android:layout_alignparenttop=" true "android:layout_alignparentstart=" true "Android:layout_marg Instart= "27DP" android:layout_margintop= "12DP"/> <button android:layoUt_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "delete" android:id= "@+id/but Ton2 "android:layout_alignbaseline=" @+id/button "android:layout_alignbottom=" @+id/button "Android: Layout_alignstart= "@+id/button4" android:layout_marginstart= "18DP"/> <button android:layout_wi
Dth= "Wrap_content" android:layout_height= "wrap_content" android:text= "Change" android:id= "@+id/button3" android:layout_margintop= "24DP" android:layout_below= "@+id/button" android:layout_alignstart= "@+i
D/button "/> <button android:layout_width= wrap_content" android:layout_height= "Wrap_content" android:text= "Check" android:id= "@+id/button4" android:layout_alignbaseline= "@+id/button3" Andr Oid:layout_alignbottom= "@+id/button3" android:layout_toendof= "@+id/button" android:layout_marginstart= "50d P "/> </relativelAyout>
DemoDemo
Reference:http://blog.csdn.net/u012702547/article/details/52226163
Http://www.cnblogs.com/whoislcj/p/5651396.html