行业知识
Android GreenDao 使用全面讲解,1-3年Android开发工程师面试经验分享
- 高性能,下面是官方给出的关于GreenDao,OrmLite和ActiveAndroid三种ORM解决方案的数据统计图:
-
易于使用的强大API,涵盖关系和连接;
-
最小的内存消耗;
-
小库大小( Make Project,重写build项目,GreenDao集成完成!
2. 创建存储对象实体类
使用GreenDao存储数据只需要在存储数据类前面声明@Entity注解就让GreenDao为其生成必要的代码:
@Entity
public class Student {
@Id(autoincrement = true)
Long id;
@Unique
int studentNo;//学号
int age; //年龄
String telPhone;//手机号
String sex; //性别
String name;//姓名
String address;//家庭住址
String schoolName;//学校名字
String grade;//几年级
……getter and setter and constructor method……
}
3. GreenDao初始化
我们可以在Application中维持一个全局的会话。我们在Applicaiton进行数据库的初始化操作:
/**
- 初始化GreenDao,直接在Application中进行初始化操作
*/
private void initGreenDao() {
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, “aserbao.db”);
SQLiteDatabase db = helper.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster(db);
daoSession = daoMaster.newSession();
}
private DaoSession daoSession;
public DaoSession getDaoSession() {
return daoSession;
}
初始化完成之后重新rebuild一下项目会发现在设置的targetGenDir的目录生成三个类文件,这个是GreenDao自动生成的!说明数据库已经连接好了,咱们接下来只需要进行数据库的增删改查操作就行了。Let’s Go!
4. 使用GreenDao实现增删改查
1. 增
insert() 插入数据
@Override
public void insertData(Thing s) {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
for (int i = 0; i
Student student = new Student();
student.setStudentNo(i);
int age = mRandom.nextInt(10) + 10;
student.setAge(age);
student.setTelPhone(RandomValue.getTel());
String chineseName = RandomValue.getChineseName();
student.setName(chineseName);
if (i % 2 == 0) {
student.setSex(“男”);
} else {
student.setSex(“女”);
}
student.setAddress(RandomValue.getRoad());
student.setGrade(String.valueOf(age % 10) + “年纪”);
student.setSchoolName(RandomValue.getSchoolName());
daoSession.insert(student);
}
}
**insertOrReplace()**数据存在则替换,数据不存在则插入
@Override
public void insertData(Thing s) {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
for (int i = 0; i
Student student = new Student();
student.setStudentNo(i);
int age = mRandom.nextInt(10) + 10;
student.setAge(age);
student.setTelPhone(RandomValue.getTel());
String chineseName = RandomValue.getChineseName();
student.setName(chineseName);
if (i % 2 == 0) {
student.setSex(“男”);
} else {
student.setSex(“女”);
}
student.setAddress(RandomValue.getRoad());
student.setGrade(String.valueOf(age % 10) + “年纪”);
student.setSchoolName(RandomValue.getSchoolName());
daoSession.insertOrReplace(student);//插入或替换
}
}
2. 删
删除有两种方式:delete()和deleteAll();分别表示删除单个和删除所有。
@Override
public void deleteData(Student s) {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
daoSession.delete(s);
}
@Override
public void deleteAll() {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
daoSession.deleteAll(Student.class);
}
3. 改
通过update来进行修改:
@Override
public void updataData(Student s) {
DaoSession daoSession = ((AserbaoApplication) getApplication()).getDaoSession();
daoSession.update(s);
}
4. 查
查询的方法有:
- loadAll():查询所有数据。
- queryRaw():根据条件查询。
- queryBuilder() : 方便查询的创建,后面详细讲解。
public List queryAll(){
List students = daoSession.loadAll(Student.class);
return students;
}
@Override
public void queryData(String s) {
List students = daoSession.queryRaw(Student.class, " where id = ?", s);
mDataBaseAdapter.addNewStudentData(students);
}
4. QueryBuilder的使用
编写SQL可能很困难并且容易出现错误,这些错误仅在运行时才会被注意到。该QueryBuilder的类可以让你建立你的实体,而不SQL自定义查询,并有助于在编译时已检测错误。
我们先讲下QueryBuilder的常见方法:
- where(WhereCondition cond, WhereCondition… condMore): 查询条件,参数为查询的条件!
- or(WhereCondition cond1, WhereCondition cond2, WhereCondition… condMore): 嵌套条件或者,用法同or。
- and(WhereCondition cond1, WhereCondition cond2, WhereCondition… condMore): 嵌套条件且,用法同and。
- join(Property sourceProperty, Class destinationEntityClass):多表查询,后面会讲。 输出结果有四种方式,选择其中一种最适合的即可,list()返回值是List,而其他三种返回值均实现Closeable,需要注意的不使用数据时游标的关闭操作:
- list ()所有实体都加载到内存中。结果通常是一个没有魔法的 ArrayList。最容易使用。
- listLazy ()实体按需加载到内存中。首次访问列表中的元素后,将加载并缓存该元素以供将来使用。必须关闭。
- listLazyUncached ()实体的“虚拟”列表:对列表元素的任何访问都会导致从数据库加载其数据。必须关闭。
- listIterator ()让我们通过按需加载数据(懒惰)来迭代结果。数据未缓存。必须关闭。
- orderAsc() 按某个属性升序排;
- orderDesc() 按某个属性降序排;
GreenDao中SQL语句的缩写,我们也了解下,源码在Property中,使用的时候可以自己点进去查询即可:
- eq():“equal (‘=?’)” 等于;
- notEq() :“not equal (‘?’)” 不等于;
- like():" LIKE ?" 值等于;
- between():" BETWEEN ? AND ?" 取中间范围;
- in():" IN (" in命令;
- notIn():" NOT IN (" not in 命令;
- gt():“>?” 大于;
- lt():"
- 初始化GreenDao,直接在Application中进行初始化操作
上一篇:怎么部署服务器端口命令?
下一篇:招标备案资金来源证明怎么开