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 {
    @Override
    public void onCreate() {
        super.onCreate();
        DBManager.getInstance().init(this);
    }
}

Remember to register in the manifest file

android:name=".App"

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;

    //初始化,在Application
    public void init(Context context) {
        this.mContext = context;
    }

    //单例
    private static DBManager mDBManager;

    public static DBManager getInstance() {
        if (mDBManager == null) {
            mDBManager = new DBManager();
        }
        return mDBManager;
    }
    //获取数据库DaoMaster
    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) {
            getmDaoMaster();
        }
        if (mDaoSession == null) {
            mDaoSession = mDaoMaster.newSession();
        }
        return mDaoSession;
    }
}

increase

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"));

delete

//删除一条数据
DBManager.getInstance().getmDaoSession().delete(new User((long) 1, "你今天真好看"));
//删除全部数据
DBManager.getInstance().getmDaoSession().deleteAll(User.class);

change

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

check

//查询一条数据
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'

use:

LogUtils.json(users);