Super simple basic use of GreenDao (addition, deletion, modification and check)

The original article is here~click me

The first step: add build.gradle in the root directory of the project

classpath 'org.greenrobot:greendao-gradle-plugin:3.3.0'

Step 2: add build.gradle of app

implementation 'org.greenrobot:greendao:3.3.0'

The third step: app’s build.gradle android{} add

greendao {
        // 指定数据库schema版本号,迁移等操作会用到
        schemaVersion 1
        // 设置生成数据库文件的目录,默认是在build中,可以将生成的文件放到我们的java目录中
        targetGenDir 'src/main/java'
        // 这里 'test' 是数据库名  可以自行更改
        daoPackage 'com.test.greendao'

Step 4: Create an entity class

//user 表名
@Entity(nameInDb = "user")
public class User {
    @Id(autoincrement = true)
    private Long _ID = null;

    private String name;

Step 5: Hammer with a small hammer,

Insert picture description here

three classes will be automatically generated

Insert picture description here

Step 6: Application initialization

public class App extends Application {
    public void onCreate() {

Remember to register in the manifest file


Step 7: Encapsulate the database operation class

public class DBManager {
    //数据库名  要和build.gradle 中写的表名一样哦~
    private final String Name = "yuanmeng.db";
    private DaoSession mDaoSession;
    private DaoMaster mDaoMaster;
    private Context mContext;

    public void init(Context context) {
        this.mContext = context;

    private static DBManager mDBManager;

    public static DBManager getInstance() {
        if (mDBManager == null) {
            mDBManager = new DBManager();
        return mDBManager;
    public DaoMaster getmDaoMaster() {
        if (mDaoMaster == null) {
            DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(mContext, Name);
            mDaoMaster = new DaoMaster(devOpenHelper.getWritableDatabase());
        return mDaoMaster;
    //获取数据库CRUD DaoSession
    public DaoSession getmDaoSession() {
        if (mDaoMaster == null) {
        if (mDaoSession == null) {
            mDaoSession = mDaoMaster.newSession();
        return mDaoSession;


insert insert a data entity object as a parameter

insertInTx batch insert data List<> type parameter

insertOrReplace insert data, if the primary key of the incoming object exists in the database, update if there is, otherwise insert the entity object as a parameter

insertOrReplaceInTx batch insert data List<> type parameter

DBManager.getInstance().getmDaoSession().insert(new User(null, "你今天真好看"));
DBManager.getInstance().getmDaoSession().insertOrReplace(new User((long) 1, "你今天真好看ya"));


DBManager.getInstance().getmDaoSession().delete(new User((long) 1, "你今天真好看"));


DBManager.getInstance().getmDaoSession().update(new User((long) 2, "你今天真丑"));


DBManager.getInstance().getmDaoSession().load(User.class, (long) 3);
List<User> users = DBManager.getInstance().getmDaoSession().loadAll(User.class);

Add LogUtils third-party library dependency to print the data in the database table

implementation 'com.blankj:utilcodex:1.26.0'