MiniOrm-for-Android是什么?
MiniOrm-for-android 是一款简单,只能,灵活的android ORM框架,完全基于对象进行操作。主要帮助android程序员的快速开发。通过反射将查询的数据智能的转换成 Entity 。省去开发人员手动解析的时间。
关系的映射支持多对多、一对多,一对一,采用annotationProcessor 生成代理类,进行关系的映射查询,实现数据懒加载,侵入性小,优化了性能,提高了程序 的效率
github地址:https://github.com/MengLeiGitHub/miniOrm-for-android 欢迎大家提bug
结构图
功能特点:
类库小
使用简单,支持实体类注解方式,除了实体类之外只需创建一个DAO就可以进行操作。
支持原生的sql语句操作
耦合性低
结构模型:
接入方法
在你项目的 build.gradle 文件里添加如下配置
dependencies {
compile 'com.github.mengleigithub:miniorm-core:2.0.2'
annotationProcessor 'com.github.mengleigithub:miniorm-compiler:1.0.2'
}
使用方法:
框架初始化:
//框架初始化,也可放在activity中
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
//test.db数据库名称
//1数据库版本号
MiniOrm.init(this,1,"test.db");
//如果表中新增字段,需要升级数据库,需要指定那个该表对应的Dao
MiniOrm.addUpdateTable(TeacherDao.class);
//注:如果数据库版本需要升降级别,并且表未做任何改变,最好不要指定该Dao类,否则会做一些不必要的工作,浪费手机性能
}
}
实体创建:
import com.miniorm.android.ColumnType;
import com.miniorm.annotation.Table;
import com.miniorm.annotation.TableColumn;
import com.miniorm.annotation.TableID;
import com.miniorm.enumtype.Parmary;
@Table(name="student")
public class Student {
@TableID(name="sid",isPrimaryKey=true,defaultVal=0,type= Parmary.AutoIncrement,columnType= ColumnType.INTEGER)
private int id ;
@TableColumn(name="stuname",columnType=ColumnType.TEXT)
private String stuName;
@TableColumn(name="age",columnType=ColumnType.INTEGER)
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "age="+age+" stuname="+stuName+" id="+id;
}
}
注解说明:
- @Table(name=”student”)设置表名 为 “student”
- @TableID(name=”sid”,isPrimaryKey=true,defaultVal=0,type= Parmary.AutoIncrement,columnType= ColumnType.INTEGER)
依次顺序表示 : 字段名= sid ,字段为表 主键 ,默认数字为 0 ,主键为自动增长,字段类型是
Integer类型
注意:如果主键为自增长的话,那么columnType= ColumnType.INTEGER必须设置为INTEGER类型
如果主键是自设置的话,无限制,但只能为 整型(INTEGER)或字符型
含有外键对象实体的创建:
package com.test.test;
import com.miniorm.android.ColumnType;
import com.miniorm.annotation.Table;
import com.miniorm.annotation.TableColumn;
import com.miniorm.annotation.TableID;
import com.miniorm.enumtype.Parmary;
@Table(name="userTable")
public class Teacher {
@TableColumn(name="username",columnType= ColumnType.TEXT)
private String userName;
@TableColumn(name="pwd",columnType=ColumnType.TEXT)
private String pwd;
@TableID(isPrimaryKey=true,name="userid",defaultVal=0,type= Parmary.CUSTOM,columnType=ColumnType.INTEGER)
private int id;
@TableColumn(name="sid",isForeignkey=true,columnType=ColumnType.INTEGER,HierarchicalQueries = true)
private Student student;
@TableColumn(name="sex",columnType=ColumnType.VARCHAR)
private String sex;
@TableColumn(name="shengao",columnType=ColumnType.INTEGER)
private int shengao;
@TableColumn(name="isGril",columnType= ColumnType.BOOLEAN,IgnoreBooleanParam = false)
private boolean isGril;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getShengao() {
return shengao;
}
public void setShengao(int shengao) {
this.shengao = shengao;
}
public boolean isGril() {
return isGril;
}
public void setIsGril(boolean isGril) {
this.isGril = isGril;
}
}
####和普通实体创建唯一不同的地方就是含有外键的对象
* TableColumn(isForeignkey=true)设置注解的属性为外键
* HierarchicalQueries = true 表示为外键和主键级联查询,默认此属性为 true
* IgnoreBooleanParam=false 如果在实体类中含有布尔变量,并且该变量保存在了数据库,那么该值就是在按照
* teacherDao.queryListByEntity(teacher)或者 teacherDao.queryByEntity(teacher); 查询的时候,
* 忽视属性 isGril该值的变量
创建工具类
public class TeacherDao extends androidBaseDao< Teacher> {
@Override
public Teacher getQueryEntity() {
// TODO Auto-generated method stub
Teacher teacher=new Teacher();
return teacher;
}
}
import com.miniorm.android.androidBaseDao;
public class StuDao extends androidBaseDao< Student> {
}
创建表
StuDao stuDao=new StuDao();
stuDao.createTable();
TeacherDao teacherDao=new TeacherDao();
int i=teacherDao.createTable();
ResultType.SUCCESS==i 表示创建成功
ResultType.FAIL==i 表示创建失败
新增:
Student student=new Student();
student.setAge(2);
student.setStuName("王小明");
Int id=stuDao.save(student);
if(id!=ResultType.FAIL){
Id==新增 对象的id
}
批量新增:
ArrayList<Student> lis2t=new ArrayList<>();
for (int i2=0;i2<10000;i2++){
Student student1=new Student();
student1.setStuName("student"+i2);
student1.setAge(i2);
lis2t.add(student1);
}
stuDao.save(lis2t);
stuDao.saveOrUpdate(lis2t);
- stuDao.save(lis2t); 该方法执行的时候,两种情况
- 1.该表主键设置为自增长(type = Parmary.AutoIncrement) 该方法执行会在表中新增一条数据
- 2.设置为自定义主键 (type= Parmary.CUSTOM) 的话,指定的主键的值如果在表中含有相同的话,则不会执行成功
- stuDao.saveOrUpdate(lis2t); 同样的和save的方法对比
- 1.如果相同的数据,就算 不指定主键,如果其他的属性值和表中某条数据都相同,该方法也不会在数据库新增。相反 如果没有和该数据都相同的,就会新增
- 2.如果指定了主键 就会更新原来的数据
删除:
######Id删除
Student student1=new Student();
student.setId(2);
stuDao.delete(student);
######根据其他属性删除
Student student=new Student();
student.setStuName("kkkk");
student.setAge(2);
stuDao.delete(student);
#####删除全部
stuDao.deleteAll();
````
<div class="se-preview-section-delimiter"></div>
####更新
<div class="se-preview-section-delimiter"></div>
```java
注意,需指定ID)
student.setId(2);
student.setStuName("kkkk");
stuDao.update(student);
查询
按照实体查询
######(注意)
* 指定id,查询出唯一一个
* 设置其他参数,非id ,如有数据值返回第一个
)
Student student1= stuDao.QueryByEntity(student);
查询全部
List list=stuDao.queryAll();
按照ID查询
stuDao.queryById(1)||stuDao.queryById(“1”)
精确条件查询
可以写sql语句 然后直接调用方法
teacherDao.executeQuery("select * from usertable",teacherDao.getQueryEntity(),teacherDao.getReflexEntity() );
调用QueryBuilder
//查询 全部 性别为 女
List listaaa=teacherDao.getQueryBuilder().callQuery().queryAll().where(Where.handle().eq(“sex”,”女”)).executeQueryList();
List<Teacher> listaaa=teacherDao.getQueryBuilder().callQuery().queryAll().where(Where.handle().and("sex","=","女")).executeQueryList();
//模糊查询
List<CustomerBean> list = customerBeanDao.getQueryBuilder().callQuery().queryAll().where(Where.handle().and("userName", " like ", "%"+tiaojian+"%").or().and("company", " like ", "%"+tiaojian+"%").or().and("nickname", " like ", "%"+tiaojian+"%")).executeQueryList();
//对应的 sql select * from CustomerBean where userName like '%你曾%' or company like '%你曾%' or nickname like '%你曾%' ;
//根据主键分页
int lastid = teacherDao.queryLastInsertId();
String table = teacherDao.getReflexEntity().getTableEntity().getTableName();
String column = teacherDao.getReflexEntity().getTableIdEntity().getColumnName();
List list1 = teacherDao.getQueryBuilder().callQuery().queryAll().where(Where.handle().and(column, "<=", lastid).and(column, ">", lastid - 10).desc()).executeQueryList();
[类关系使用方法]
一对一
@Table(name="userTable")
public class Teacher {
@TableColumn(name="username",columnType= ColumnType.TEXT)
private String userName;
@TableColumn(name="pwd",columnType=ColumnType.TEXT)
private String pwd;
@TableID(isPrimaryKey=true,name="userid",defaultVal=0,type= Parmary.CUSTOM,columnType=ColumnType.INTEGER)
private long id;
@TableColumn(name="sid",isForeignkey=true,columnType=ColumnType.INTEGER,HierarchicalQueries = true)
private Student student;
@TableColumn(name="sex",columnType=ColumnType.VARCHAR)
private String sex;
@TableColumn(name="shengao",columnType=ColumnType.INTEGER)
private int shengao;
@TableColumn(name="isGril",columnType= ColumnType.BOOLEAN,IgnoreBooleanParam = true)
private boolean isGril;
private ArrayList<Student> students;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public long getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getShengao() {
return shengao;
}
public void setShengao(int shengao) {
this.shengao = shengao;
}
public boolean isGril() {
return isGril;
}
public void setIsGril(boolean isGril) {
this.isGril = isGril;
}
}
@Table(name="student")
public class Student {
@TableID(name="sid",isPrimaryKey=true,defaultVal=0,type= Parmary.AutoIncrement,columnType= ColumnType.INTEGER)
private int id ;
@TableColumn(name="stuname",columnType=ColumnType.TEXT)
private String stuName;
@TableColumn(name="age",columnType=ColumnType.INTEGER)
private int age;
private Teacher teacher;
private List<Teacher> teachers;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@OneToOne
public Teacher getTeacher() {
return teacher;
}
@OneToOne
public Teacher holderTeacher() {
return teacher;
}
@OneToMany
public List<Teacher> getTeachers() {
return teachers;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
@Override
public String toString() {
return "age="+age+" stuname="+stuName+" id="+id;
}
}
*一对一的写法需要,在两个表中的其中一个设置外键关联,然后在另外的一个类中取值的时候
*可以直接在 get方法上用 @OneToOne 标注,就可以直接拿取到关联对象
一对多:
*一对多和一对一在表的设计中大体相同,但是表的关联外键必须放在多的一方,
并且在少的一方取对象的时候 要用@OneToMany
如下:
@Table(name="student")
public class Student {
@TableID(name="sid",isPrimaryKey=true,defaultVal=0,type= Parmary.AutoIncrement,columnType= ColumnType.INTEGER)
private int id ;
@TableColumn(name="stuname",columnType=ColumnType.TEXT)
private String stuName;
@TableColumn(name="age",columnType=ColumnType.INTEGER)
private int age;
private Teacher teacher;
private List<Teacher> teachers;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@OneToOne
public Teacher getTeacher() {
return teacher;
}
@OneToMany
public List<Teacher> getTeachers() {
return teachers;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
@Override
public String toString() {
return "age="+age+" stuname="+stuName+" id="+id;
}
}
多对一
多对一和一对多一样只是将谁设置成多或者一 是根据业务上来定的,技术上无差别
多对多
MIniOrm2在设计多对多的表关系和大多的orm框架一样 都采用了第三个表,可以大大降低开发
的难度和维护的成本
下面来介绍一下多对多的表设置
@Table(name="student")
public class Student {
@TableID(name="sid",isPrimaryKey=true,defaultVal=0,type= Parmary.AutoIncrement,columnType= ColumnType.INTEGER)
private int id ;
@TableColumn(name="stuname",columnType=ColumnType.TEXT)
private String stuName;
@TableColumn(name="age",columnType=ColumnType.INTEGER)
private int age;
private List<Teacher> teachers;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@ManyToMany(bridgingTable=StudentTeacher.class)
public List<Teacher> getTeachers() {
return teachers;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
@Override
public String toString() {
return "age="+age+" stuname="+stuName+" id="+id;
}
}
@Table(name="userTable")
public class Teacher {
@TableColumn(name="username",columnType= ColumnType.TEXT)
private String userName;
@TableColumn(name="pwd",columnType=ColumnType.TEXT)
private String pwd;
@TableID(isPrimaryKey=true,name="userid",defaultVal=0,type= Parmary.CUSTOM,columnType=ColumnType.INTEGER)
private long id;
@TableColumn(name="sex",columnType=ColumnType.VARCHAR)
private String sex;
@TableColumn(name="shengao",columnType=ColumnType.INTEGER)
private int shengao;
@TableColumn(name="isGril",columnType= ColumnType.BOOLEAN,IgnoreBooleanParam = true)
private boolean isGril;
private ArrayList<Student> students;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public long getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getShengao() {
return shengao;
}
public void setShengao(int shengao) {
this.shengao = shengao;
}
public boolean isGril() {
return isGril;
}
public void setIsGril(boolean isGril) {
this.isGril = isGril;
}
@ManyToMany(bridgingTable=StudentTeacher.class)
public ArrayList<Student> getStudents(){
return students;
}
}
**中间表
@Table(name="StudentTeacher")
public class StudentTeacher {
@TableID(isPrimaryKey=true,name="utid",defaultVal=0,type= Parmary.AutoIncrement,columnType= ColumnType.INTEGER)
private long id;
@TableColumn(name="sid",isForeignkey=true,columnType=ColumnType.INTEGER,isPrimaryKey = true)
private Student student;
@TableColumn(name="tid",isForeignkey=true,columnType=ColumnType.INTEGER,isPrimaryKey = true)
private Teacher teacher;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
}
在设置多对多的关系的同时,要指定中间表的class类。
**注意:千万不要使用JSON工具直接打印对象 否则可能会出现死循环
代码混淆
-keep class com.miniorm.**
-keepclassmembers class com.miniorm.** { *; }
-keep enum com.miniorm.**
-keepclassmembers enum com.miniorm.** { *; }
-keep interface com.miniorm.**
-keepclassmembers interface com.miniorm.** { *; }
在使用中有任何问题,欢迎反馈给我