阳光沙滩
让学习编程变得简单
【总结2019 展望2020】(技术教程)简述GreenDao的一般使用步骤
发表于 2019-12-27    阅读次数 606

简述GreenDao的一般使用步骤

图片描述

1.在bulid.gradle(Module:app)中添加greendao如下配置(添加依赖)

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

2.编写MyApplication继承至Application

import android.app.Application;
import android.database.sqlite.SQLiteDatabase;

import com.example.smart.greendaodemo.db.DaoMaster;
import com.example.smart.greendaodemo.db.DaoSession;

public class MyApplication extends Application {

    private DaoSession daoSession;
    public static MyApplication myApplication;

    public static MyApplication getInstance() {
        return myApplication;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        initGreenDao();
    }

    /**
     * 初始化GreenDao
     */
    private void initGreenDao() {
        DaoMaster.DevOpenHelper openHelper = new DaoMaster.DevOpenHelper(this, "Info.db");
        SQLiteDatabase db = openHelper.getWritableDatabase();
        DaoMaster daoMaster = new DaoMaster(db);
        daoSession = daoMaster.newSession();
        myApplication = this;
    }

    /**
     * 获取 DaoSession
     *
     * @return
     */
    public DaoSession getDaoSession() {
        return daoSession;
    }

}

3.在AndroidManifest.xml清单文件中的application节点添加name属性,并指定为MyApplication的类名.包名。

清单文件中添加application节点的添加name属性

4.生成Bean类

import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Generated;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Unique;


    //TODO 在Bean类中需要在类中添加如下注解
@Entity
public class GreenDaoBean {

    @Unique
    @Id
    private String name;
    private String sex;
    private int age;
    private String phone;


    //TODO 如下构造方法皆为自动生成,一般不用自己生成
    @Generated(hash = 1389179617)
    public GreenDaoBean(String name, String sex, int age, String phone) {
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.phone = phone;
    }

    //TODO 如下构造方法皆为自动生成,一般不用自己生成
    @Generated(hash = 826843181)
    public GreenDaoBean() {
    }
    


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    //TODO 看自己需不需要toString
    @Override
    public String toString() {
        return "GreenDaoBean{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                ", phone='" + phone + '\'' +
                '}';
    }
}

5.使用时需要做的事情(固定写法)

private MyApplication myApplication = MyApplication.getInstance();
private DaoSession daoSession;
private List<GreenDaoBean> greenDaoBeans = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_green_dao);
    daoSession = myApplication.getDaoSession();
    //TODO 与数据库的相关操作请放到子线程中执行!(对数据库的操作为耗时操作)
    new Thread(new Runnable() {
        @Override
        public void run() {
            greenDaoBeans = daoSession.getGreenDaoBeanDao().loadAll();
        }
    }).start();
}