GreenDao 基本使用

      近期调研数据库框架,结合调研锁得出的结论,个人还是觉得GreenDao  比较好用,因此我就大致写一下此框架的基本用法,加深一下自己对这个框架的印象

  第一步:在app的Build.gradle中添加如下配置:

     apply plugin: 'org.greenrobot.greendao'

      在android中添加

   greendao {
         schemaVersion 1//数据库版本号
         daoPackage 'com.com.sky.downloader.greendao'//设置DaoMaster、DaoSession、Dao包名
         targetGenDir 'src/main/java'//设置DaoMaster、DaoSession、Dao目录
        }

在build.gradle(project "")中加入

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

 最后加入依赖

       implementation 'org.greenrobot:greendao:3.2.0'

 第二步:写实体类

public class User {
    private Long id;
    private String name;
    private int age;   
}

点击Build后make project后GreenDao会自动帮你生成get/set方法如下:

public class User {

    @Id(autoincrement = true)
    private Long id;
    private String name;
    private int age;
    @Generated(hash = 1309193360)
    public User(Long id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }
    @Generated(hash = 586692638)
    public User() {
    }
    public Long getId() {
        return this.id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getName() {
        return this.name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return this.age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    
}

封装GreenDaoManager

public class GreenDaoManager {

    private DaoMaster mDaoMaster;
    private DaoSession mDaoSession;
    private static GreenDaoManager mInstance; //单例

    private GreenDaoManager(){
        if (mInstance == null) {
            DaoMaster.DevOpenHelper devOpenHelper = new
                    DaoMaster.DevOpenHelper(MyApplication.getContext(), "user1-db", null);//此处为自己需要处理的表
            mDaoMaster = new DaoMaster(devOpenHelper.getWritableDatabase());
            mDaoSession = mDaoMaster.newSession();
        }
    }

    public static GreenDaoManager getInstance() {
        if (mInstance == null) {
            synchronized (GreenDaoManager.class) {//保证异步处理安全操作

                if (mInstance == null) {
                    mInstance = new GreenDaoManager();
                }
            }
        }
        return mInstance;
    }

    public DaoMaster getMaster() {
        return mDaoMaster;
    }
    public DaoSession getSession() {
        return mDaoSession;
    }
    public DaoSession getNewSession() {
        mDaoSession = mDaoMaster.newSession();
        return mDaoSession;
    }
}

在Application中配置好:

public class MyApplication extends Application {

    private static Context mContext;
    @Override
    public void onCreate() {
        super.onCreate();
        mContext = getApplicationContext();
        //greenDao全局配置,只希望有一个数据库操作对象
        GreenDaoManager.getInstance();
    }
    public static Context getContext() {
        return mContext;
    }
}

这就配置好了,然后就可以使用了。

下面就是基本的使用啦

private void insertdata() {
    //插入数据
    User insertData = new User(null, "插入数据", 22, false);
    getUserDao().insert(insertData);
}

private void updatadata() {
    //更改数据
    List userss = getUserDao().loadAll();
    User user = new User(userss.get(0).getId(), "更改后的数据用户", 22, true);
    getUserDao().update(user);

}

private void querydata() {
    //查询数据详细
    List users = getUserDao().loadAll();
    Log.i("tag", "当前数量:" + users.size());
    for (int i = 0; i < users.size(); i++) {

    }

}
public void deleteAllNote(){
    getUserDao().deleteAll();
}

其他方法就留个各位自己研究啦,小弟先行告退。

你可能感兴趣的:(GreenDao 基本使用)