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: http://greenrobot.org/greendao/

advantage

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 {
        google()
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
        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'
}
dependencies{
    compile'org.greenrobot:greendao:3.0.1'
    compile'org.greenrobot:greendao-generator:3.0.0'
}
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

@Entity
public class User {
    @Id 
    private Long id; 
    private String name; 
    @Transient 
    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;

    @Override
    public void onCreate() {
        super.onCreate();
        context = this;
        setUpDatabase();
    }

    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;
    }
}
operation

/**
     * increase
     */
    private void addUser() {
        try {
            User user = new User(null, "wj");
            MyApplication.getContext().getSession().getUserDao().insert(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
/**
     * 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