LiteOrm是一个速度快、小巧、强大的android ORM框架类库,让你一行代码实现数据库的增删改查操作,以及实体关系的持久化和自动映射。
官方github: https://github.com/litesuits/android-lite-orm
本篇参考:
//www.greatytc.com/p/0d72226ef434(好理解)
https://my.oschina.net/u/2320057/blog/816059(联级多表操作)
LiteOrm基本使用:
第一步:导入
将github下载的jar包复制到项目的app中的libs文件夹下:
然后:右键选中jar包,点击:add as library,点击ok
现在就可以开始使用了
使用步骤:
1.建立LiteOrmMannger.class作为统一管理一个app的管理数据库(有比较好用的固定格式,每个app只需要改不同的数据库名称)
这里给出笔者现在使用的LiteOrmMannger.class(设计模式:单例模式)
package safetytaxfree.de.ecommerceandroid.LiteOrmTest;
import android.content.Context;
import android.text.TextUtils;
import com.litesuits.orm.LiteOrm;
import com.litesuits.orm.db.DataBaseConfig;
import com.litesuits.orm.db.assit.QueryBuilder;
import com.litesuits.orm.db.assit.WhereBuilder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
/**
* Created by jzhu on 2016/11/9.
*/
public class LiteOrmManager {
private static volatile LiteOrmManager sInstance;
private static LiteOrm liteOrm;
public LiteOrmManager(Context context) {
if (liteOrm == null) {
//这里修改数据库名 一般一个项目对应一个数据库 一个数据库对应一个manager
DataBaseConfig config = new DataBaseConfig(context, "smzcpsg.db");
config.debugged = true; // open the log
config.dbVersion = 1; // set database version
liteOrm = LiteOrm.newSingleInstance(config);
/*单表查询 多表是:liteOrm = LiteOrm.newCascadeInstance(config);
*
* 也有两者共同:
* LiteOrm liteOrm = LiteOrm.newCascadeInstance(this, "cascade.db");
liteOrm.cascade().save(user);//级联操作:保存[当前对象],以及该对象所有的[关联对象]以及它们的[映射关系]。
liteOrm.single().save(user);//非级联操作:仅保存[当前对象],高效率。
* */
}
liteOrm.setDebugged(true); // open the log
}
public static void deleteDatabase() {
liteOrm.deleteDatabase();
liteOrm.openOrCreateDatabase();
}
public static LiteOrmManager getInstance(Context context) {
if (null == sInstance) {
synchronized (LiteOrmManager.class) {
if (null == sInstance) {
sInstance = new LiteOrmManager(context);
}
}
}
return sInstance;
}
// public static DatabaseManager getInstance() {
// return ourInstance;
// }
// public LiteOrm getCascadeInstance() {
// return liteOrm.cascade();
// }
public LiteOrm getSingleInstance() {
return liteOrm.single();
}
public <T> int save(Collection<T> list, Class<T> clazz) {//清空并保存多个一个列表的对象,以某个model.class保存
LiteOrm lOrm = liteOrm.single();
lOrm.deleteAll(clazz);
return lOrm.save(list);
}
public <T> int save(Collection<T> list) {//保存多个一个列表的对象
LiteOrm lOrm = liteOrm.single();
return lOrm.save(list);
}
public <T> ArrayList<T> query(Class<T> clazz) {
LiteOrm lOrm = liteOrm.single();
return lOrm.query(clazz);
}
public <T> int delete(T t) {
LiteOrm lOrm = liteOrm.single();
return lOrm.delete(t);
}
public <T> int update(T t) {
LiteOrm lOrm = liteOrm.single();
return lOrm.update(t);
}
public <T> long save(T t) {
LiteOrm lOrm = liteOrm.single();
return lOrm.save(t);
}
public <T> int delete(Class<T> clazz) {
LiteOrm lOrm = liteOrm.single();
return lOrm.delete(clazz);
}
public <T> int deleteAll(Class<T> clazz) {
LiteOrm lOrm = liteOrm.single();
return lOrm.deleteAll(clazz);
}
public long queryCount(QueryBuilder var1) {
LiteOrm lOrm = liteOrm.single();
return lOrm.queryCount(var1);
}
public <T> int deleteByWhereBuilder(Class<T> clazz, String column, Object value) {
LiteOrm lOrm = liteOrm.single();
return lOrm.delete(new WhereBuilder(clazz).equals(column, value));
}
public <T> int deleteByWhereBuilder(Class<T> clazz, String column, Object value, String column1, Object value1) {
LiteOrm lOrm = liteOrm.single();
return lOrm.delete(new WhereBuilder(clazz).equals(column, value).and().equals(column1, value1));
}
public <T> int deleteWhereBuilderByAndEquals(Map<String, Object> whereArgs, Class<T> clazz) {
LiteOrm lOrm = liteOrm.single();
WhereBuilder builder = new WhereBuilder(clazz);
for (Map.Entry<String, Object> entry : whereArgs.entrySet()) {
builder.andEquals(entry.getKey(), entry.getValue());
}
return lOrm.delete(builder);
}
public <T> ArrayList<T> queryBuilderByAppendAndEqual(Map<String, Object> whereArgs, String ascKey, Class<T> clazz) {
LiteOrm lOrm = liteOrm.single();
QueryBuilder builder = new QueryBuilder(clazz);
for (Map.Entry<String, Object> entry : whereArgs.entrySet()) {
builder.whereAppendAnd().whereEquals(entry.getKey(), entry.getValue());
}
if (!TextUtils.isEmpty(ascKey))
builder.appendOrderDescBy(ascKey);
return lOrm.query(builder);
}
public <T> long saveObject(T t, Class<T> clazz) {
LiteOrm lOrm = liteOrm.single();
lOrm.delete(clazz);
return save(t);
}
public <T extends Object> T queryObject(Class<T> clazz) {
ArrayList<T> list = query(clazz);
if (list != null && list.size() > 0) {
return list.get(0);
}
return null;
}
public <T> ArrayList<T> queryByEqual(String column, Object keyword, Class<T> clazz) {
LiteOrm lOrm = liteOrm.single();
return lOrm.query(new QueryBuilder<>(clazz).whereEquals(column, keyword).appendOrderDescBy("id"));
}
public <T extends Object> T queryObjectByEqual(String name, Object keyword, Class<T> clazz) {
ArrayList<T> list = queryByEqual(name, keyword, clazz);
if (list != null && list.size() > 0) {
return list.get(0);
} else {
return null;
}
}
public <T> ArrayList<T> queryByEqual(String column, Object keyword, Class<T> clazz, boolean asc, String ascKey) {
LiteOrm lOrm = liteOrm.single();
if (TextUtils.isEmpty(ascKey)) {
return lOrm.query(new QueryBuilder<>(clazz).whereEquals(column, keyword));
}
if (asc) {
return lOrm.query(new QueryBuilder<>(clazz).whereEquals(column, keyword).appendOrderAscBy(ascKey));
} else {
return lOrm.query(new QueryBuilder<>(clazz).whereEquals(column, keyword).appendOrderDescBy(ascKey));
}
}
public <T> ArrayList<T> queryByEqual(String column, Object keyword, String column1, Object keyword1, Class<T> clazz) {
LiteOrm lOrm = liteOrm.single();
return lOrm.query(new QueryBuilder<>(clazz).where(column + "=?", keyword).whereAnd(column1 + "=?", keyword1));
}
//拓展manager
/**
* 插入一条记录
*
* @param t
*/
public <T> long insert(T t) {
return liteOrm.save(t);
}
/**
* 插入所有记录
*
* @param list
*/
public <T> void insertAll(List<T> list) {
liteOrm.save(list);
}
/**
* 查询所有
*
* @param cla
* @return
*/
public <T> List<T> getQueryAll(Class<T> cla) {
return liteOrm.query(cla);
}
/**
* 查询 某字段 等于 Value的值
*
* @param cla
* @param field
* @param value
* @return
*/
public <T> List<T> getQueryByWhere(Class<T> cla, String field, String[] value) {
return liteOrm.<T>query(new QueryBuilder(cla).where(field + "=?", value));
}
/**
* 查询 某字段 等于 Value的值 可以指定从1-20,就是分页
*
* @param cla
* @param field
* @param value
* @param start
* @param length
* @return
*/
public <T> List<T> getQueryByWhereLength(Class<T> cla, String field, String[] value, int start, int length) {
return liteOrm.<T>query(new QueryBuilder(cla).where(field + "=?", value).limit(start, length));
}
/**
* 删除集合中的数据
*
* @param list
* @param <T>
*/
public <T> void deleteList(List<T> list) {
liteOrm.delete(list);
}
}
2.建立存入数据库的model类
注意:此处的model是存入数据库的数据模型和retrofit请求中的实体类Bean不是同一个东西,比如retrofit请求到的实体为UserBean,要存放用户信息也需要重新建立UserModel
2.1确定表名
@Table设置表名
2.2确定主键 一般就是id
@PrimaryKey(AssignType.AUTO_INCREMENT)
@Column("id")
private long id;
2.3确定列名 可以有多个
@Column("searchKey")
private String searchKey;
package safetytaxfree.de.ecommerceandroid.LiteOrmTest;
import com.litesuits.orm.db.annotation.Column;
import com.litesuits.orm.db.annotation.PrimaryKey;
import com.litesuits.orm.db.annotation.Table;
import com.litesuits.orm.db.enums.AssignType;
@Table("user")
public class UserModel {
@PrimaryKey(AssignType.AUTO_INCREMENT)
@Column("id")
private long id;
@Column("firstName")
private String firstName;
@Column("lastName")
private String lastName;
@Column("passport")
private String passport;
@Column("profile")
private String profile;
@Column("shopUuid")
private String shopUuid;
@Column("token")
private String token;
@Column("uuid")
private String uuid;
public long getId() {
return id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getPassport() {
return passport;
}
public void setPassport(String passport) {
this.passport = passport;
}
public String getProfile() {
return profile;
}
public void setProfile(String profile) {
this.profile = profile;
}
public String getShopUuid() {
return shopUuid;
}
public void setShopUuid(String shopUuid) {
this.shopUuid = shopUuid;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
}
以上为最基础的设置 其他注解使用方法 以后补充
3.在TestActivity中导入LiteOrmMannger 不需要建立对象 可以直接使用:
LiteOrmMannger.getInstance(this).save(user1);
void saveinfo(Take take){//此处的Take就是retrofit请求得到的user实体对象
UserModel user1=new UserModel();
user1.setUuid(take.getContent().getUuid());
user1.setFirstName(take.getContent().getFirstName());
user1.setLastName(take.getContent().getLastName());
user1.setShopUuid(take.getContent().getShopUuid());
user1.setToken(take.getContent().getToken());
LiteOrmManager.getInstance(this).save(user1);
}