GreenDao3.X的基本使用

1、在project的Gradle中配置:

buildscript {
    repositories {
        jcenter()
        mavenCentral() // add repository
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.0'
        classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' // add plugin
    }
}

2、在app的Gradle中配置:

apply plugin: 'com.android.application'
apply plugin: 'org.greenrobot.greendao' // apply plugin
 
dependencies {
    compile 'org.greenrobot:greendao:3.2.2' // add library
}

3、在app的Gradle中配置数据库版本等信息:

greendao {
    schemaVersion 1
    daoPackage 'com.tao.greendao.gen'
    targetGenDir 'src/main/java'
}

schemaVersion :当前的版本号;
daoPackage :由GreenDao自动生成的代码(DaoMaster,DaoSession,XXDao)所在的包名,默认的是在项目包下面新建一个gen;
targetGenDir :自动生成代码的目录。

4、自定义实体类:

@Entity
public class Student {
    @Id(autoincrement = true)
    private Long id;
    private String sNum;
    private String name;
    private String age;
    private String sex;
}

@Entity 标识实体类,greenDAO会映射成sqlite的一个表,表名为实体类名的大写形式
@Id 标识主键,该字段的类型为long或Long类型,autoincrement设置是否自动增长
@Property 标识该属性在表中对应的列名称,默认是实体类属性名称的大写, nameInDb设置名称
@Transient 标识该属性将不会映射到表中,也就是没有这列
@NotNull 设置表中当前列的值不可为空
@Convert 指定自定义类型(@linkPropertyConverter)
@Generated greenDAO运行所产生的构造函数或者方法,被此标注的代码可以变更或者下次运行时清除
@Index 使用@Index作为一个属性来创建一个索引;定义多列索引(@link Entity#indexes())
@JoinEntity 定义表连接关系
@JoinProperty 定义名称和引用名称属性关系
@Keep 注解的代码段在GreenDao下次运行时保持不变
1.注解实体类:默认禁止修改此类
2.注解其他代码段,默认禁止修改注解的代码段
@OrderBy 指定排序
@ToMany 定义与多个实体对象的关系
@ToOne 定义与另一个实体(一个实体对象)的关系
@Unique 向数据库列添加了一个唯一的约束

5、创建好实体类后Build->Make Project对项目进行重新构建,会在daoPackage 下自动生成代码:

@Entity
public class Student {
    @Id(autoincrement = true)
    private Long id;
    private String sNum;
    private String name;
    private String age;
    private String sex;
    @Generated(hash = 672840549)
    public Student(Long id, String sNum, String name, String age, String sex) {
        this.id = id;
        this.sNum = sNum;
        this.name = name;
        this.age = age;
        this.sex = sex;
    }
    @Generated(hash = 1556870573)
    public Student() {
    }
    public Long getId() {
        return this.id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getSNum() {
        return this.sNum;
    }
    public void setSNum(String sNum) {
        this.sNum = sNum;
    }
    public String getName() {
        return this.name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAge() {
        return this.age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    public String getSex() {
        return this.sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }

}

6、新建一个操作GreenDao的工具类:

/**
 * 作者:WangJintao
 * 时间:2017/4/6
 * 邮箱:[email protected]
 */

public class GreenDaoManager {
    private static DaoMaster mDaoMaster;
    private static DaoSession mDaoSession;
    private static volatile GreenDaoManager mInstance = null;
    private static final String DB_NAME = "GreenDao.db";

    private GreenDaoManager() {
    }

    public static void init(Context context) {
        DaoMaster.DevOpenHelper devOpenHelper = new
                DaoMaster.DevOpenHelper(context, DB_NAME);
        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 DaoSession getSession() {
        return mDaoSession;
    }
}

在Application中初始化就可以使用了。
初始化代码:GreenDaoManager.init(this);

7、增删查改操作:

a)增:

Student student = new Student();
                student.setSNum(id);
                student.setName(name);
                student.setAge(age);
                student.setSex(sex);
                GreenDaoManager.getInstance().getSession().getStudentDao().insert(student);

b)删:

Student student2 = new Student();
                student2.setId(_id);//数据在表中自动生成的id
                student2.setSNum(id);
                student2.setName(name);
                student2.setAge(age);
                student2.setSex(sex);
                GreenDaoManager.getInstance().getSession().getStudentDao().delete(student2);

c)查:

GreenDaoManager.getInstance().getSession().getStudentDao().load(id);

d)改:

Student student3 = new Student();
                student3.setId(_id);//数据在表中自动生成的id
                student3.setSNum(id);
                student3.setName(name);
                student3.setAge(age);
                student3.setSex(sex);
GreenDaoManager.getInstance().getSession().getStudentDao().update(id);

你可能感兴趣的:(GreenDao3.X的基本使用)