Simple Use of GreenDao
GreenDao is an open source SQLite-based database ORM (Object Relational Mapping). When we use SQLite to cache and read data, we need to write CRUD statements by ourselves. We also need to convert the read data into java objects, or convert java objects into records stored in the database. This is not easy for android developers who are not familiar with SQL statements. The greater significance of Dao's existence is that it can automatically link java objects to database tables through a few lines of code, thus greatly saving developers'time.
Characteristic
- Best Use of Android ORM Tools
- Powerful APIs support
- Very small memory overhead
- The introduced jar package is very small in size
- Using SQL Cipher to ensure database data security
- Needless to say, there are many people who use it, and the natural performance can stand the test.
Specific use (greendao version: 3.2.2, using IDE for Android Studio)
1. Introducing library dependencies under build.gradle(app)
apply plugin: 'com.android.application'
apply plugin: 'org.greenrobot.greendao' // Generate a module(library)
android {
......
}
//Initialize the database
greendao {
schemaVersion 1 //Database version
daoPackage 'com.leer.greendaotest.gen'//Generated DAOs, DaoMaster, and DaoSession stored package names
targetGenDir 'src/main/java' //Location of the daoPackage above in the project
}
dependencies {
......
compile 'org.greenrobot:greendao:3.2.2' // Adding greendao's jar package dependencies automatically downloads
}
2. Add greendao plug-in dependencies under build.gradle(project)
buildscript { repositories { jcenter() } dependencies { ...... classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' // Add plug-ins } }
3. Create the Entity corresponding to the table in the database
@Entity
public class Student {
@Id(autoincrement = true)
private Long id;
@Property
private String studentName;
@Property(nameInDb = "sex")
private String studentSex;
@Transient
private String isChecked;
}
-
@ Entity: A table that represents the Student class mapped to the database is called "Student"; in fact, we can change the point of view, under each table is a record, each record contains various attributes of a single Student, then a Student object is mapped to a record under the database Student table.
- @ Id: Corresponding to the primary key, autoincrement = true, represents self-growth. We don't need to assign a value to this field when inserting a worthwhile value into the database.
- @ Property: As the name implies, it is an attribute, which corresponds to a column name in a table, of course Id is also a column, but it is a primary key column; nameInDb = sex: the sex in the database corresponds to studentSex in the entity class;
- @ Transient: This field will not be written to the database. It will only be used as a common java field to temporarily store data. It will not be persisted to the database.
- There are many other annotations, but here are just a few simple and often used ones.
- For more notes, please move on: http://m.blog.csdn.net/kongxingxing/article/details/5205233
Use
Preparations have been completed, and it's time to use them. It's easy to use -> build -> Make Project. Then something magical happens. Look at your project catalogue:
-
Before clicking Make Project:
-
After clicking Make Project:
You can see three more automatically generated classes: DaoMaster, DaoSession, StudentDao
What is the use of these three categories?
- DaoMaster: Contains an internal class inherited from OpenHelper to complete the creation and update of the database, create tables in the database, and so on, also known as the session layer, and have a conversation with SQLLiteDatabase.
- DaoSession: Managing Daos class files; also includes some basic methods for adding, deleting, and modifying checks
- XXXDao: Data access object. Each table corresponds to an Entity entity class. dao is the object that accesses the data under a specific table.
DaoMaster.DevOpenHelper openHelper = new DaoMaster.DevOpenHelper(this, "students.db",null);
Database database = openHelper.getWritableDataBase();
//Pass database into daomaster to create daomaster object
DaoMaster daoMaster = new DaoMaster(database);
//Creating DaoSession objects, in fact, through DaoSession objects, we can also complete the addition, deletion and modification of data, but using XXXDao objects, the method will be more comprehensive.
DaoSession daoSession = daoMaster.newSession();
//Get the data access object for each table
StudentDao studentDao = daoSession.getStudentDao();
Student student = new Student();
student.id = 1001;
student.name = Zhang San;
student.sex = male;
//insert data
studentDao.insert(student);
StudentDao also has methods like update(),delete(),query(), and so on. For more information, please check out my simple use of GreenDao 2 and write it here first.