Android之greenDao(初识greenDao)

文章目录

        • greenDao概述
        • greenDao配置(Android Studio)
        • greenDao简单使用
            • greenDao实体类创建
            • greenDao增
            • greenDao删
            • greenDao改
            • greenDao查
        • greenDao使用持续更新中。。。

greenDao概述

  1. greenDao简介
    SQLite是一个很棒的嵌入式关系数据库。尽管如此,编写SQL和解析查询结果仍然是一项非常繁琐且耗时的任务。而greenDAO是一个针对SQLite数据库的Android ORM开源框架。通过将Java对象映射到数据库表(称为ORM,“对象/关系映射”)将使用者从中解放出来。这样,便可以通过处理Java对象来处理对应的数据库表数据。
    Android之greenDao(初识greenDao)_第1张图片

greenDao配置(Android Studio)

  1. 项目build.gradle中配置如下:
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
        classpath 'org.greenrobot:greendao-gradle-plugin:3.2.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
  1. 模块build.gradle中配置:
apply plugin: 'org.greenrobot.greendao'
buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        //依赖greendao插件
        classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1'
    }
}

greendao {
    schemaVersion 1 // 数据库版本号
    daoPackage 'com.sangto.sangtopay.entity.greendao'   // greenDao 自动生成的代码保存的包名
    targetGenDir 'src/main/java' //自动生成的代码存储的路径,默认是 build/generated/source/greendao.
    generateTests false //true的时候自动生成测试单元
    // targetGenDirTests:    // 测试单元的生成目录默认是 src/androidTest/java
}

dependencies {
    compile 'org.greenrobot:greendao:3.2.2' // add library
    compile 'org.greenrobot:greendao-generator:3.2.2'
}

  1. Application中进行初始化
    // greenDao  session
    private DaoSession mDaoSession;
    public void initDatabase(){
        DaoMaster.DevOpenHelper openHelper = 
                new DaoMaster.DevOpenHelper(context,"DB_NAME");
        Database db = openHelper.getWritableDb();
        DaoMaster daoMaster = new DaoMaster(db);
        mDaoSession = daoMaster.newSession();
    }
    
    public static DaoSession getDaoSession(){
        return mDaoSession;
    }

  1. 创建greenDao实体类后进行Build,程序会自动生成greenDao文件。

greenDao简单使用

greenDao实体类创建

/**
 * Description:  题目
 * Author: fpp
 * Date: 2018/6/5  10:59
 */
@Entity
public class TopicBean  implements Serializable {
    static final long serialVersionUID = 42L;
    @Id(autoincrement=true)
    private Long id;
    private int type; // 类型
    private int subject;  // 科目
    private int chapter;  // 章节
    @Property(nameInDb = "QUESTION")
    private String title;  // 问题
    private String ans_type;  // 题类型  1:判断  2:单选  3:多选
    private String analysis;  // 官方解释
    private String images;  // 图片
    private String image_name;  // 图片名称
    private byte[] image_byte;  // 图片数组
    @Convert(columnType = String.class, converter = StringConverter.class)
    private List answer;  // 答案
}

常见注解:

  1. @Entity:标注实体,生成表。
  2. @Id(autoincrement=true):主键(Long型),autoincrement为true时即设置自增。
  3. @Property(nameInDb = “USERNAME”):标注表字段名,可以自定义,默认是实体属性名大写,单词间使用下划线分割。外键不能使用此注解。
  4. @NotNull:属性值不能为空。
  5. @Transient:在表中不会创建此字段。
  6. @Unique:为相应列添加唯一约束,注意,SQLite会隐式地为该列创建索引。
  7. @Index(unique = true):为相应的列创建索引。
greenDao增
  1. long insert(T entity) : 插入单个实体。
  2. void insertInTx(T… entities):插入多个实体。
  3. void insertInTx(Iterable entities);
  4. void insertInTx(Iterable entities, boolean setPrimaryKey);
  5. long insertWithoutSettingPk(T entity) : 插入单个实体,无主键。
  6. long insertOrReplace(T entity): 插入或替换单个实体。
  7. void insertOrReplaceInTx(T… entities):插入或替换多个单个实体。
  8. void insertOrReplaceInTx(Iterable entities);
  9. void insertOrReplaceInTx(Iterable entities, boolean setPrimaryKey)
  10. void save(T entity):插入或修改指定主键的实体。
  11. void saveInTx(T… entities);
  12. void saveInTx(Iterable entities);
greenDao删
  1. void delete(T entity):删除单个实体。
  2. void deleteAll():删除所有实体。
  3. void deleteByKey(Long id):删除指定Id实体。
  4. void deleteByKeyInTx(Long…ids):删除多个指定Id实体。
  5. void deleteByKeyInTx(Iterable ids);
  6. void deleteInTx(T… entities):删除多个实体。
  7. void deleteInTx(Iterable entities);
greenDao改
  1. void update(T entity):更新单个实体。
  2. void updateInTx(T… entities):更新多个实体。
  3. void updateInTx(Iterable entities);
greenDao查
  1. T load(Long id):查询指定id的实体。
  2. List loadAll():查询所有实体。
  3. T loadByRowId();

greenDao使用持续更新中。。。

你可能感兴趣的:(greenDao)