Simple use of GreenDao

Written in front

Sqlite is often used in the development process. The SqliteAssetsHelper of the third party was used before, which is very easy to use. Only need to integrate and write some necessary methods, and provide external methods of addition, deletion, modification and query. So let's look at another mature third party today -GreenDao.

brief introduction

Official website:


1. A streamlined Library
2. Maximize performance
3. Minimize memory overhead
4. Easy to use APIs
5. Highly optimized for Android

Integration steps
Step 1: add dependency

Add to build.gradle in the root directory of the project

buildscript {

    repositories {
    dependencies {
        classpath ''
        classpath 'org.greenrobot:greendao-gradle-plugin:3.0.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files

Add in build.gradle under app directory

apply plugin: 'org.greenrobot.greendao'

greendao {
    schemaVersion 1
    daoPackage 'com.wj.greendao.dao'
    targetGenDir 'src/main/java'
Property Description:
schemaVersion specifies the database schema version number, which will be used for migration and other operations
The package name of daoPackage dao. By default, the package name is the package of entity
targetGenDir is the directory where database files are generated. Daomaster, daosession and xxxdao are all placed in this directory.

Step 2 create a Bean

Create a Java Bean

public class User {
    private Long id; 
    private String name; 
    private int tempUsageCount; 

This basic configuration is complete.

Step 3 compile

Click build - > make project of AS to automatically generate these three files in the specified directory.

Specific use

It is usually initialized in Application.

public class MyApplication extends Application {
    private static final String DATA_NAME = "user";
    private DaoSession session;
    private static Context context;

    public void onCreate() {
        context = this;

    public static synchronized MyApplication getContext() {
        return (MyApplication) context;

    public void setUpDatabase() {
        DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(this, DATA_NAME);
        Database database = devOpenHelper.getWritableDb();
        DaoMaster daoMaster = new DaoMaster(database);
        session = daoMaster.newSession();

    public DaoSession getSession() {
        return session;

     * increase
    private void addUser() {
        try {
            User user = new User(null, "wj");
        } catch (Exception e) {
     * delete
    private void deleteUser(){
        MyApplication.getContext().getSession().getUserDao().deleteByKey(null);//Delete by key
//        MyApplication.getContext().getSession().getUserDao().deleteAll(); / / delete all
//        MyApplication.getContext().getSession().getUserDao().delete(); / / delete a bean
     * Update data
    private void updateUser(){
//        MyApplication.getContext().getSession().getUserDao().update();
     * Query data
    private void searchUser(){
//        User user = MyApplication.getContext().getSession().getUserDao().load(1);
//        List<User> users = MyApplication.getContext().getSession().getUserDao().loadAll();
//        List<User> list = MyApplication.getContext().getSession().getUserDao().queryBuilder().where(UserDao.Properties.Name.eq("")).list();
//        MyApplication.getContext().getSession().getUserDao().queryRaw("wj","");

Written in the back

Simple integration and operation calls are written here. After that, we will update our study and publish [2].

Tags: Gradle Database Session Android

Posted on Mon, 13 Apr 2020 09:02:29 -0700 by adamhhh