GreenDao 3.2 使用

来源:互联网 发布:250luxu乐乎 编辑:程序博客网 时间:2024/05/22 06:14

一、Android studio配置gradle

1.首先在根build.gradle中添加如下:

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

2.在项目的build.gradle中添加依赖如下:

// In your app projects build.gradle file:apply plugin: 'com.android.application'apply plugin: 'org.greenrobot.greendao' // apply plugin dependencies {    compile 'org.greenrobot:greendao:3.2.2' // add library}

3.定义greendao文件存储位置,版本等等

/** 配置GreenDao基本参数 * 1. schemaVersion:当前版本的数据库模式。这被OpenHelpers类用于在模式版本之间迁移。如果更改了实体/数据库模式,则必须增加该值。默认为1 2. daoPackage:生成的DaoDaoMasterDaoSession的包名称。 默认为源实体的包名称。 3. targetGenDir:生成源应存储在的位置。 默认为构建目录( build / generated / source / greendao)中生成的源文件夹。 4. generateTests: 设置为true以自动生成单元测试。 5. targetGenDirTests: 应存储生成的单元测试的基本目录。默认为 src / androidTest / java */greendao {    schemaVersion 1 //当前数据库版本}

greendao配置路径和版本要独立配置,配置位置如下:

android { ……}greendao {    schemaVersion 1 //当前数据库版本}dependencies {  ……}

上述为greendao的基本配置

二、操作数据库

操作数据库需要的内容有:数据库名、表名、字段名,Bean中的类名就相当于表明,类中的属性就相当于字段名

1.在MyApplication中创建数据库

public class MyApplication extends Application {        private static DaoSession daoSession;    @Override    public void onCreate() {        super.onCreate();        //配置数据库        setupDatabase();    }    /**     * 配置数据库     */    private void setupDatabase() {        //创建数据库shop.db"        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "shop.db", null);        //获取可写数据库        SQLiteDatabase db = helper.getWritableDatabase();        //获取数据库对象        DaoMaster daoMaster = new DaoMaster(db);        //获取Dao对象管理者        daoSession = daoMaster.newSession();    }    public static DaoSession getDaoInstant() {        return daoSession;    }}

配置数据库名如上,创建一个DaoSession用来操作数据库

2.创建Bean对象

操作数据库需要的内容有:数据库名、表名、字段名,Bean中的类名就相当于表明,类中的属性就相当于字段名

下面是创建bean类需要用

  1. @Entity:告诉GreenDao该对象为实体,只有被@Entity注释的Bean类才能被dao类操作
  2. @Id:对象的Id,使用Long类型作为EntityId,否则会报错。(autoincrement = true)表示主键会自增,如果false就会使用旧值
  3. @Property:可以自定义字段名,注意外键不能使用该属性
  4. @NotNull:属性不能为空
  5. @Transient:使用该注释的属性不会被存入数据库的字段中
  6. @Unique:该属性值必须在数据库中是唯一值
  7. @Generated:编译后自动生成的构造函数、方法等的注释,提示构造函数、方法等不能被修改
@Entitypublic class Student {    @Id(autoincrement = true)   // 设置自增长id    private Long stuId;    //    @Index(unique = false)   // 设置唯一性    private String stuNo;   // 学生编号    private String stuName; // 学员姓名    private String stuSex;  // 性别    private String stuScore;    // 成绩}
如上在类名上加入@Entity注解,在id上加入@Id注解和自增(自增可以不添加)
添加完毕后点击Build中的Make Project让GreenDao自动生成代码,其中自动生成的代码有Bean实体的构造方法,DaoMaster、DaoSession、DAOS类
生成完毕后可以通过
studentDao = MyApplication.getDaoInstant().getStudentDao();
获取想要操作的表然后进行增删改查
  • 增加单个数据 
    • getStudentDao().insert(student);
    • getStudentDao().insertOrReplace(student);
  • 增加多个数据 
    • getStudentDao().insertInTx(studentList);
    • getStudentDao().insertOrReplaceInTx(studentList);
  • 查询全部 
    • List< Student> list = getStudentDao().loadAll();
    • List< Student> list = getStudentDao().queryBuilder().list();
  • 查询附加单个条件 
    • .where()
    • .whereOr()
  • 查询附加多个条件 
    • .where(, , ,)
    • .whereOr(, , ,)
  • 查询附加排序 
    • .orderDesc()
    • .orderAsc()
  • 查询限制当页个数 
    • .limit()
  • 查询总个数 
    • .count()
  • 修改单个数据 
    • getStudentDao().update(shop);
  • 修改多个数据 
    • getStudentDao().updateInTx(shopList);
  • 删除单个数据 
    • getStudentDao().delete(student);
  • 删除多个数据 
    • getStudentDao().deleteInTx(studentList);
  • 删除数据ByKey 
    • getStudentDao().deleteByKey();









原创粉丝点击