Android ORM框架介绍之android-liteorm注解与封装
LiteOrm
代号: archiver
LiteOrm 是一个速度快、小巧却强大的 Android ORM 框架类库,速度快、体积小、性能高,开发者基本一行代码实现数据库的增删改查操作,以及实体关系的持久化和自动映射。 自动化且比系统自带数据库操作快1倍!
liteorm注解
常用注解
-
@Table:用于类,默认使用类名,也可以自定义。如:
@Table(“tb_user”)
-
@PrimaryKey:主键,通过AssignType枚举来设置值,如:
@PrimaryKey(AssignType.AUTO_INCREMENT)
- BY_MYSELF,
- AUTO_INCREMENT;//自增
- @Unique:唯一字段(行)
- @UniqueCombine:唯一(列中字段值唯一)
- @NotNull:字段部位none
- @Ingore:使用该注解表示字段不存入数据库
-
@Mapping:表示关联关系,通过枚举Relation设置值,如:
@Mapping(Relation.OneToOne)
- ManyToMany,//多对多
- OneToMany,//一对多
- ManyToOne,//多对一
- OneToOne;//一对一
-
@Column:自定义字段名(列名)。如:
@Column(“name”)
-
@Conflict:数据库冲突策略,通过枚举Strategy设置值,可用于表,也可用于字段上
- ROLLBACK(” ROLLBACK “),//回滚
- ABORT(” ABORT “),//中断
- FAIL(” FAIL “),//失败
- IGNORE(” IGNORE “),//忽略
- REPLACE(” REPLACE “);//替换
liteorm的封装
public class DataBaseManager {
private static DataBaseManager manager;
private static final String DB_NAME = "my.db";
private LiteOrm liteOrm;
private DataBaseManager(Context context) {
if (liteOrm == null) {
liteOrm = LiteOrm.newSingleInstance(context, DB_NAME);
}
}
public static DataBaseManager getInstance(Context context) {
context = context.getApplicationContext();
if (manager == null) {
synchronized (DataBaseManager.class) {
if (manager == null) {
manager = new DataBaseManager(context);
}
}
}
return manager;
}
/**
* 插入一条记录
*/
public <T> long insert(T t) {
return liteOrm.save(t);
}
/**
* 插入所有记录
*/
public <T> void insertAll(List<T> list) {
liteOrm.save(list);
}
/**
* 根据对象查询数据
*/
public <T> int update(T t) {
return liteOrm.update(t);
}
/**
* 查询所有数据
*/
public <T> int updateAll(List<T> list) {
return liteOrm.update(list);
}
/**
* 根据id查询
*/
public <T> T query(long id, Class<T> clazz) {
return liteOrm.queryById(id, clazz);
}
/**
* 查询所有
*/
public <T> List<T> queryAll(Class<T> cla) {
return liteOrm.query(cla);
}
/**
* 查询 某字段 等于 Value的值
*/
@SuppressWarnings("unchecked")
public <T> List<T> queryByWhere(Class<T> cla, String field, String[] value) {
return liteOrm.query(new QueryBuilder(cla).where(field + "=?", value));
}
/**
* 查询 某字段 等于 Value的值 可以指定从1-20,就是分页
*/
@SuppressWarnings("unchecked")
public <T> List<T> queryByWhereLength(Class<T> cla, String field, String[] value, int start, int length) {
return liteOrm.query(new QueryBuilder(cla).where(field + "=?", value).limit(start, length));
}
/**
* 删除一个数据
*/
public <T> void delete(T t) {
liteOrm.delete(t);
}
/**
* 删除所有数据
*/
public <T> void deleteAll(Class<T> clazz) {
liteOrm.deleteAll(clazz);
}
/**
* 删除集合中的数据
*/
public <T> void deleteList(List<T> list) {
liteOrm.delete(list);
}
/**
* 删除数据库
*/
public void deleteDatabase() {
liteOrm.deleteDatabase();
}
}
liteorm的使用
@Table("tb_user")
public class User {
@PrimaryKey(AssignType.AUTO_INCREMENT)//主键,自增
private long id;
@NotNull
private String name;
private int age;
public User(){}
public User(long id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
MainActivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button insert;
private Button update;
private Button query;
private Button queryAll;
private Button delete;
private Button deleteAll;
private TextView tv_query;
private TextView tv_queryall;
private DataBaseManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
insert = (Button) findViewById(R.id.btn1);
update = (Button) findViewById(R.id.btn2);
query = (Button) findViewById(R.id.btn3);
queryAll = (Button) findViewById(R.id.btn4);
delete = (Button) findViewById(R.id.btn5);
deleteAll = (Button) findViewById(R.id.btn6);
tv_query = (TextView) findViewById(R.id.tv_query);
tv_queryall = (TextView) findViewById(R.id.tv_queryall);
insert.setOnClickListener(this);
update.setOnClickListener(this);
query.setOnClickListener(this);
queryAll.setOnClickListener(this);
delete.setOnClickListener(this);
deleteAll.setOnClickListener(this);
manager = DataBaseManager.getInstance(MainActivity.this);
}
private void deleteAll() {
manager.deleteAll(User.class);
}
private void deleteData() {
User user = new User();
user.setId(1);
manager.delete(user);
}
private List<User> queryAll() {
return manager.queryAll(User.class);
}
private User queryData() {
return manager.query(1, User.class);
}
private void updateData() {
User user = new User(1, "lisi", 28);
manager.update(user);
}
private void insertData() {
User user1 = new User(1, "zhangsan", 20);
manager.insert(user1);
User user2 = new User(2, "zhangsan", 20);
manager.insert(user2);
User user3 = new User(3, "zhangsan", 20);
manager.insert(user3);
User user4 = new User(4, "zhangsan", 20);
manager.insert(user4);
//inserAll
// List<User> list = new ArrayList<>();
// list.add(user1);
// list.add(user2);
// list.add(user3);
// list.add(user4);
// manager.insertAll(list);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn1:
insertData();
break;
case R.id.btn2:
updateData();
break;
case R.id.btn3:
User user = queryData();
if (user == null) {
tv_query.setText("查询的数据不存在");
} else {
tv_query.setText(user.toString());
}
break;
case R.id.btn4:
List<User> list = queryAll();
if (list == null) {
tv_queryall.setText("查询的数据不存在");
} else {
tv_queryall.setText(list.toString());
}
break;
case R.id.btn5:
deleteData();
break;
case R.id.btn6:
deleteAll();
break;
}
}
}
版权声明:本文为qq_33689414原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。