GreenDao的初次使用--号称Android最快的关系型数据库
一、准备工作
1、项目build.gradle文件下的dependencies中引入插件:
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1'
2、在module的build.gradle文件下顶部依赖插件:
apply plugin: 'org.greenrobot.greendao'
3、在module的build.gradle文件下的dependencies中引入依赖:
compile 'org.greenrobot:greendao:3.2.0'
二、greendao的注解
1、实体@entity注解:
schema:告知greendao当前实体属于哪个schema
active:标记一个实体处于活动状态,活动实体有更新、删除和刷新方法
nameindb:在数据中使用的别名,默认使用的是实体的类名
indexes:定义索引,可以跨越多个列
createindb:标记创建数据库表
2、基础属性注解
@id :主键 long型,可以通过@id(autoincrement = true)设置自增长
@property:设置一个非默认关系映射所对应的列名,默认是的使用字段名 举例:@property (nameindb="name")
@notnul:设置数据库表当前列不能为空
@transient :添加次标记之后不会生成数据库表的列
3、索引注解
@index:使用@index作为一个属性来创建一个索引,通过name设置索引别名,也可以通过unique给索引添加约束
@unique:向数据库列添加了一个唯一的约束
4、关系注解
@toone:定义与另一个实体(一个实体对象)的关系
@tomany:定义与多个实体对象的关系
三、简单使用----使用管理类dbmanager,操作名称为test_db的数据库,完成数据库中用户实体类user的增删改查等操作
1、创建实体用户类user,使用greendao进行注解,rebuild项目。
/** * <p>description: */ @entity public class user { @id(autoincrement = true) private long id ; private string name ; private int age ; public user(string name,int age){ this.name = name ; this.age = age ; } @generated(hash = 1309193360) public user(long id, string name, int age) { this.id = id; this.name = name; this.age = age; } @generated(hash = 586692638) public user() { } public long getid() { return this.id; } public void setid(long id) { this.id = id; } public string getname() { return this.name; } public void setname(string name) { this.name = name; } public int getage() { return this.age; } public void setage(int age) { this.age = age; } }
2、创建管理类dbmanager,完成对user的增删改查方法
public class dbmanager { private static dbmanager instance ; private final static string dbname = "test_db"; private context mcontext ; private dbmanager(context context){ this.mcontext = context ; openhelper = new daomaster.devopenhelper(context,dbname); } private daomaster.devopenhelper openhelper = null ; public static dbmanager getinstance(context context){ if(instance == null){ synchronized (dbmanager.class){ if(instance == null){ instance = new dbmanager(context); } } } return instance ; } private sqlitedatabase getwriteabledatabase(){ if(openhelper == null){ openhelper = new daomaster.devopenhelper(mcontext,dbname); } return openhelper.getwritabledatabase(); } private sqlitedatabase getredadabledatabase(){ if(openhelper == null){ openhelper = new daomaster.devopenhelper(mcontext,dbname); } return openhelper.getreadabledatabase() ; } public void saveuser(user user){ daomaster daomaster = new daomaster(getwriteabledatabase()); daosession daosession = daomaster.newsession(); userdao userdao = daosession.getuserdao(); //存入user对象 userdao.save(user); } public void saveusers(list<user> users){ daomaster daomaster = new daomaster(getwriteabledatabase()); daosession daosession = daomaster.newsession(); userdao userdao = daosession.getuserdao(); userdao.saveintx(users); } public void delteuser(user user){ daomaster daomaster = new daomaster(getwriteabledatabase()); daosession daosession = daomaster.newsession(); userdao userdao = daosession.getuserdao(); userdao.delete(user); } public void updateuser(user user){ daomaster daomaster = new daomaster(getwriteabledatabase()); daosession daosession = daomaster.newsession(); userdao userdao = daosession.getuserdao(); userdao.update(user); } public list<user> queryuser(){ daomaster daomaster = new daomaster(getredadabledatabase()); daosession daosession = daomaster.newsession(); userdao userdao = daosession.getuserdao(); querybuilder<user> qb = userdao.querybuilder(); list<user> list = qb.list(); return list ; } public list<user> queryuser(int age){ daomaster daomaster = new daomaster(getredadabledatabase()); daosession daosession = daomaster.newsession(); userdao userdao = daosession.getuserdao(); querybuilder<user> qb = userdao.querybuilder(); //添加查询条件 qb.where(userdao.properties.age.eq(age)); list<user> list = qb.list(); return list ; } }
上一篇: JNA简单使用