文章目录
greenDao概述
- greenDao简介
SQLite是一个很棒的嵌入式关系数据库。尽管如此,编写SQL和解析查询结果仍然是一项非常繁琐且耗时的任务。而greenDAO是一个针对SQLite数据库的Android ORM开源框架。通过将Java对象映射到数据库表(称为ORM,“对象/关系映射”)将使用者从中解放出来。这样,便可以通过处理Java对象来处理对应的数据库表数据。
greenDao配置(Android Studio)
- 项目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
}
- 模块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'
}
- 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;
}
- 创建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<String> answer; // 答案
}
常见注解:
- @Entity:标注实体,生成表。
- @Id(autoincrement=true):主键(Long型),autoincrement为true时即设置自增。
- @Property(nameInDb = “USERNAME”):标注表字段名,可以自定义,默认是实体属性名大写,单词间使用下划线分割。外键不能使用此注解。
- @NotNull:属性值不能为空。
- @Transient:在表中不会创建此字段。
- @Unique:为相应列添加唯一约束,注意,SQLite会隐式地为该列创建索引。
- @Index(unique = true):为相应的列创建索引。
greenDao增
- long insert(T entity) : 插入单个实体。
- void insertInTx(T… entities):插入多个实体。
- void insertInTx(Iterable entities);
- void insertInTx(Iterable entities, boolean setPrimaryKey);
- long insertWithoutSettingPk(T entity) : 插入单个实体,无主键。
- long insertOrReplace(T entity): 插入或替换单个实体。
- void insertOrReplaceInTx(T… entities):插入或替换多个单个实体。
- void insertOrReplaceInTx(Iterable entities);
- void insertOrReplaceInTx(Iterable entities, boolean setPrimaryKey)
- void save(T entity):插入或修改指定主键的实体。
- void saveInTx(T… entities);
- void saveInTx(Iterable entities);
greenDao删
- void delete(T entity):删除单个实体。
- void deleteAll():删除所有实体。
- void deleteByKey(Long id):删除指定Id实体。
- void deleteByKeyInTx(Long…ids):删除多个指定Id实体。
- void deleteByKeyInTx(Iterable ids);
- void deleteInTx(T… entities):删除多个实体。
- void deleteInTx(Iterable entities);
greenDao改
- void update(T entity):更新单个实体。
- void updateInTx(T… entities):更新多个实体。
- void updateInTx(Iterable entities);
greenDao查
- T load(Long id):查询指定id的实体。
- List loadAll():查询所有实体。
- T loadByRowId();