android 超轻量级的ORM框架

时间:2021-09-13 20:36:55

一、测试性能Demo

首先给大家一个性能测试的Demo,看看性能如何。 Demo.apk android 超轻量级的ORM框架

二、相关操作CRUD

Entity:

import android.annotation.SuppressLint;
//依赖注入配置
@SuppressLint("UseValueOf")
public class Entity {

/**
* 默认主键,自增。最好有id主键,便于debug和逻辑判断。
*
* 暂不支持自定义主键
*/
int id;

public boolean base_boolean;
public double base_double;
public float base_float;
public int base_int;
public long base_long;

public Boolean mBoolean;
public Double mDouble;
public Float mFloat;
public Integer mInteger;
public Long mLong;
public String mString;
}

initDB:
private DB db= DB.getInstance(mContext);
//依赖注入
@SuppressLint("UseValueOf")
1.增加:
Entity entity = new Entity();
entity.base_boolean = true;
entity.base_double = 10.2;
entity.base_float = 9.1f;
entity.base_int = 99;
entity.base_long = System.currentTimeMillis();
entity.mBoolean = new Boolean(true);
entity.mDouble = new Double(10.2);
entity.mFloat = new Float(9.1);
entity.mInteger = new Integer(99);
entity.mLong = new Long(System.currentTimeMillis() / 1000);
entity.mString = new String("test");
db.save(entity);

2.删除
List<Entity> list = db.findAll(Entity.class);
if (list != null && list.size() > 0) {
Entity entity = list.get(0);
db.delete(entity);
}
//删除所有// db.deleteAll(Entity.class);
//按条件删除// db.deleteByWhere(Entity.class, "");

3.更新
List<Entity> list = db.findAll(Entity.class);
if (list.size() > 0) {
<span style="white-space:pre"></span>Entity entity = list.get(0);
entity.base_int = 23482374;
<span style="white-space:pre"></span>//按条件更新// db.update(Entity, "id=1");db.update(entity);}

4.查询
List<Entity> list = db.findAll(Entity.class);
//条件查询
//db.findAllByWhere(Entity.class, "id=1");

5.删除表
db.dropTable(Entity.class);

6.添加1000条数据,添加事务
// 开始事务。提高效率(不用事务,插入1000条数据可能要十几秒,用事务可能不到1秒,具体要看类是否复制)
db.beginTrancation();
<span style="white-space:pre"></span>for (int i = 0; i < 1000; i++) {db.save(entity);}
<span style="white-space:pre"></span>db.endTrancation();

Demo:源码和jar包:
http://pan.baidu.com/s/1gd7ZRTh