本文作者:speedy
CSDN 专栏:http://blog.csdn.net/speedystone/article/details/74193053
掘金专栏:https://juejin.im/post/595a12ba6fb9a06b9a5175bd
在 Android ORM 框架:GreenDao 使用详解(基础篇) 中,我们了解了 GreenDao 的基本使用,本文我们将深入讲解 GreenDao 的使用 。
@Entity
public class Order {
@Id
private Long id;
private long customerId;
@ToOne(joinProperty = "customerId")
private Customer customer;
}
@Entity
public class Customer {
@Id
private Long id;
}
@Entity
public class Customer {
@Id
private Long id;
@ToMany(referencedJoinProperty = "customerId")
@OrderBy("date ASC")
private List orders;
}
@Entity
public class Order {
@Id private Long id;
private Date date;
private long customerId;
}
@Entity
public class Product {
@Id private Long id;
@ToMany
@JoinEntity(
entity = JoinProductsWithOrders.class,
sourceProperty = "productId",
targetProperty = "orderId"
)
private List ordersWithThisProduct;
}
@Entity
public class JoinProductsWithOrders {
@Id private Long id;
private Long productId;
private Long orderId;
}
@Entity
public class Order {
@Id private Long id;
}
boolean, Boolean
int, Integer
short, Short
long, Long
float, Float
double, Double
byte, Byte
byte[]
String
Date
例如:将枚举类型转换成整形
@Entity
public class User {
@Id
private Long id;
@Convert(converter = RoleConverter.class, columnType = Integer.class)
private Role role;
public enum Role {
DEFAULT(0), AUTHOR(1), ADMIN(2);
final int id;
Role(int id) {
this.id = id;
}
}
public static class RoleConverter implements PropertyConverter<Role, Integer> {
@Override
public Role convertToEntityProperty(Integer databaseValue) {
if (databaseValue == null) {
return null;
}
for (Role role : Role.values()) {
if (role.id == databaseValue) {
return role;
}
}
return Role.DEFAULT;
}
@Override
public Integer convertToDatabaseValue(Role entityProperty) {
return entityProperty == null ? null : entityProperty.id;
}
}
方法一:
List joes = userDao.queryRaw("where AGE>?","10");//查询年龄大于10的用户
方法二:
List joes = userDao.queryBuilder().where(UserDao.Properties.Age.gt("10")).list();
// order by last name
queryBuilder.orderAsc(Properties.LastName);
// in reverse
queryBuilder.orderDesc(Properties.LastName);
// order by last name and year of birth
queryBuilder.orderAsc(Properties.LastName).orderDesc(Properties.YearOfBirth);
limit(int) : 限制查询返回的结果的数量。
offset(int): 设置起始位置
// 从第二条记录开始查询5条记录
List list = userDao.queryBuilder()
.offset(2)
.limit(5)
.list();
LazyList lazyList = userDao.queryBuilder().listLazy();
for (User u:lazyList) {
Log.i(TAG, "用户名:"+u.getName());
}
lazyList.close(); //不再使用时必须关闭,否则会导致数据库游标未关闭,从而导致内存泄漏
LazyList 是 GreenDao 实现的集合类,实现了 List 接口,所以,直接把 LazyList 当做List 来使用即可,
public class LazyList<E> implements List<E>, Closeable {
//省略了具体实现
}
注意: LazyList 不再使用时必须调用用 close() 方法关闭,否则会导致内存泄漏 。
lazyList.close();
很多时候我们在查询的时候不能查询到我们期望的结果,这个时候我们可以通过修改 QueryBuilder 的两个静态成员变量来打印 SQL 日志,便于排查问题。
QueryBuilder.LOG_SQL = true;
QueryBuilder.LOG_VALUES = true;
QueryBuilder queryBuilder = userDao.queryBuilder();
queryBuilder.join(Address.class, AddressDao.Properties.userId)
.where(AddressDao.Properties.Street.eq("Sesame Street"));
List users = queryBuilder.list();
### greenDAO 3
-keepclassmembers class * extends org.greenrobot.greendao.AbstractDao {
public static java.lang.String TABLENAME;
}
-keep class **$Properties
# If you do not use SQLCipher:
-dontwarn org.greenrobot.greendao.database.**
# If you do not use RxJava:
-dontwarn rx.**
### greenDAO 2
-keepclassmembers class * extends de.greenrobot.dao.AbstractDao {
public static java.lang.String TABLENAME;
}
-keep class **$Properties
greenDAO 支持 SQLCipher 直接绑定。
compile 'net.zetetic:android-database-sqlcipher:3.5.7@aar'
DevOpenHelper helper = new DevOpenHelper(this, "notes-db-encrypted.db");
Database db = helper.getEncryptedWritableDb("" );
daoSession = new DaoMaster(db).newSession();
当然,如果你不是使用 GreenDao 数据库,同样可以使用 SQLCipher 加密保护我们的数据,详细请参考 SQLCipher for Android
Rxjava 的火爆程度已经是如日中天了,GreenDao 对当然也是对提供对 Rxjava 的支持。(比较失望的是目前 GreedDao 仅支持 Rxjava 1,不支持 Rxjava 2)
1,引入 Rxjava 依赖
compile 'io.reactivex:rxandroid:1.2.1'
compile 'io.reactivex:rxjava:1.2.9'
3,初始化 GreedDao 配置
public class App extends Application {
/**
* 加密标识符
*/
public static final boolean ENCRYPTED = true;
private DaoSession daoSession;
@Override
public void onCreate() {
super.onCreate();
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this,ENCRYPTED ? "notes-db-encrypted" : "notes-db");
Database db = ENCRYPTED ? helper.getEncryptedWritableDb("super-secret") : helper.getWritableDb();
daoSession = new DaoMaster(db).newSession();
}
public DaoSession getDaoSession() {
return daoSession;
}
}
3, 获取 RxDao
User user = new User();
user.setUserId(10);
user.setName("小红");
user.setAge(18);
DaoSession daoSession = ((MyApp) getApplication()).getDaoSession();
RxDao userDao = daoSession.getUserDao().rx();
userDao.insert(user)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1() {
@Override
public void call(User user) {
Log.i(TAG, "保存成功 ");
}
});