[Android]Android端ORM框架——RapidORM(v2.0)
以下内容为原创,欢迎转载,转载请注明
来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/5626716.html
[Android]Android端ORM框架——RapidORM(v2.0)
RapidORM:Android端轻量高性能的ORM框架
GitHub: https://github.com/wangjiegulu/RapidORM
1. RapidORM v1.0
v1.0博客文档:http://www.cnblogs.com/tiantianbyconan/p/4748077.html
v1.0版本支持使用反射和非反射(模版生成)两种方式实现执行SQL。
1.1. v1.0缺点:
其中默认为反射实现,对性能有一定的影响。
而如果要采用非反射实现,则需要使用RapidORM提供的模版工具类手动生成相关的帮助类,当数据表需要修改时,必须要手动手动生成帮助类,有潜在的风险。
非反射时生成的文件是通过
getter/setter方法调用的,但是getter/setter方法名可能会不一致,导致需要手动调整setter/getter方法名称。
2. RapidORM v2.0
相比较于v1.0版本,v2.0版本则更加侧重于非反射操作,所有默认都是非反射的。
通过在编译时期根据@Table、@Column等注解自动生成辅助类Xxx_RORM.java文件,RapidORM库会使用这些生成的辅助类来进行数据表的初始化、执行SQL等操作,如果数据表有结构有改动,则会自动重新生成或者rebuild来手动生成。
2.1 v2.0 使用指南
与
v1.0相同部分省略。
2.1.1 同样,创建持久类Person
/**
* Author: wangjie
* Email: tiantian.china.2@gmail.com
* Date: 6/25/15.
*/
@Table
public class Person implements Serializable {
@Column(primaryKey = true)
Integer id;
@Column(primaryKey = true, name = "type_id")
Integer typeId;
@Column
String name;
@Column
int age;
@Column
String address;
@Column
Long birth;
@Column
Boolean student;
@Column(name = "is_succeed")
boolean isSucceed;
// getter/setter
}
注意,v2.0版本不支持变量为private类型,这是为了避免使用getter/setter方法来进行数据绑定。如果变量为private类型,则编译会报错,如下:
Error:Execution failed for task ':example:compileDebugJavaWithJavac'.
> java.lang.RuntimeException: id in com.wangjie.rapidorm.example.database.model.Person can not be private!
2.1.2 Rebuild Project
Android Studio -> Build -> Rebuild Project
Build成功后,在主项目build/generated/source/apt/目录下会生成Person_RORM类, 如下:
// GENERATED CODE BY RapidORM. DO NOT MODIFY! "2016-06-29 14:08:504", Source table: "com.wangjie.rapidorm.example.database.model.Person"
package com.wangjie.rapidorm.example.database.model;
import android.database.Cursor;
import com.wangjie.rapidorm.core.config.ColumnConfig;
import com.wangjie.rapidorm.core.config.TableConfig;
import java.util.List;
public class Person_RORM extends TableConfig<Person> {
/**
* Column name: "id", field name: {@link Person#id}
*/
public static final String ID = "id";
/**
* Column name: "type_id", field name: {@link Person#typeId}
*/
public static final String TYPE_ID = "type_id";
/**
* Column name: "name", field name: {@link Person#name}
*/
public static final String NAME = "name";
/**
* Column name: "age", field name: {@link Person#age}
*/
public static final String AGE = "age";
/**
* Column name: "address", field name: {@link Person#address}
*/
public static final String ADDRESS = "address";
/**
* Column name: "birth", field name: {@link Person#birth}
*/
public static final String BIRTH = "birth";
/**
* Column name: "student", field name: {@link Person#student}
*/
public static final String STUDENT = "student";
/**
* Column name: "is_succeed", field name: {@link Person#isSucceed}
*/
public static final String IS_SUCCEED = "is_succeed";
public Person_RORM() {
super(Person.class);
}
@Override
protected void parseAllConfigs() {
tableName = "Person";
ColumnConfig idColumnConfig = buildColumnConfig("id"/*column name*/, false/*autoincrement*/, false/*notNull*/, ""/*defaultValue*/, false/*index*/, false/*unique*/, false/*uniqueCombo*/, true/*primaryKey*/, "INTEGER"/*dbType*/);
allColumnConfigs.add(idColumnConfig);
allFieldColumnConfigMapper.put("id"/*field name*/, idColumnConfig);
pkColumnConfigs.add(idColumnConfig);
ColumnConfig typeIdColumnConfig = buildColumnConfig("type_id"/*column name*/, false/*autoincrement*/, false/*notNull*/, ""/*defaultValue*/, false/*index*/, false/*unique*/, false/*uniqueCombo*/, true/*primaryKey*/, "INTEGER"/*dbType*/);
allColumnConfigs.add(typeIdColumnConfig);
allFieldColumnConfigMapper.put("typeId"/*field name*/, typeIdColumnConfig);
pkColumnConfigs.add(typeIdColumnConfig);
ColumnConfig nameColumnConfig = buildColumnConfig("name"/*column name*/, false/*autoincrement*/, false/*notNull*/, ""/*defaultValue*/, false/*index*/, false/*unique*/, false/*uniqueCombo*/, false/*primaryKey*/, "TEXT"/*dbType*/);
allColumnConfigs.add(nameColumnConfig);
allFieldColumnConfigMapper.put("name"/*field name*/, nameColumnConfig);
noPkColumnConfigs.add(nameColumnConfig);
ColumnConfig ageColumnConfig = buildColumnConfig("age"/*column name*/, false/*autoincrement*/, false/*notNull*/, ""/*defaultValue*/, false/*index*/, false/*unique*/, false/*uniqueCombo*/, false/*primaryKey*/, "INTEGER"/*dbType*/);
allColumnConfigs.add(ageColumnConfig);
allFieldColumnConfigMapper.put("age"/*field name*/, ageColumnConfig);
noPkColumnConfigs.add(ageColumnConfig);
ColumnConfig addressColumnConfig = buildColumnConfig("address"/*column name*/, false/*autoincrement*/, false/*notNull*/, ""/*defaultValue*/, false/*index*/, false/*unique*/, false/*uniqueCombo*/, false/*primaryKey*/, "TEXT"/*dbType*/);
allColumnConfigs.add(addressColumnConfig);
allFieldColumnConfigMapper.put("address"/*field name*/, addressColumnConfig);
noPkColumnConfigs.add(addressColumnConfig);
ColumnConfig birthColumnConfig = buildColumnConfig("birth"/*column name*/, false/*autoincrement*/, false/*notNull*/, ""/*defaultValue*/, false/*index*/, false/*unique*/, false/*uniqueCombo*/, false/*primaryKey*/, "LONG"/*dbType*/);
allColumnConfigs.add(birthColumnConfig);
allFieldColumnConfigMapper.put("birth"/*field name*/, birthColumnConfig);
noPkColumnConfigs.add(birthColumnConfig);
ColumnConfig studentColumnConfig = buildColumnConfig("student"/*column name*/, false/*autoincrement*/, false/*notNull*/, ""/*defaultValue*/, false/*index*/, false/*unique*/, false/*uniqueCombo*/, false/*primaryKey*/, "INTEGER"/*dbType*/);
allColumnConfigs.add(studentColumnConfig);
allFieldColumnConfigMapper.put("student"/*field name*/, studentColumnConfig);
noPkColumnConfigs.add(studentColumnConfig);
ColumnConfig isSucceedColumnConfig = buildColumnConfig("is_succeed"/*column name*/, false/*autoincrement*/, false/*notNull*/, ""/*defaultValue*/, false/*index*/, false/*unique*/, false/*uniqueCombo*/, false/*primaryKey*/, "INTEGER"/*dbType*/);
allColumnConfigs.add(isSucceedColumnConfig);
allFieldColumnConfigMapper.put("isSucceed"/*field name*/, isSucceedColumnConfig);
noPkColumnConfigs.add(isSucceedColumnConfig);
}
@Override
public void bindInsertArgs(Person model, List<Object> insertArgs) {
Integer id = model.id;
insertArgs.add(null == id ? null : id );
Integer typeId = model.typeId;
insertArgs.add(null == typeId ? null : typeId );
String name = model.name;
insertArgs.add(null == name ? null : name );
int age = model.age;
insertArgs.add(age);
String address = model.address;
insertArgs.add(null == address ? null : address );
Long birth = model.birth;
insertArgs.add(null == birth ? null : birth );
Boolean student = model.student;
insertArgs.add(null == student ? null : student ? 1 : 0);
boolean isSucceed = model.isSucceed;
insertArgs.add(isSucceed ? 1 : 0);
}
@Override
public void bindUpdateArgs(Person model, List<Object> updateArgs) {
String name = model.name;
updateArgs.add(null == name ? null : name);
int age = model.age;
updateArgs.add(age);
String address = model.address;
updateArgs.add(null == address ? null : address);
Long birth = model.birth;
updateArgs.add(null == birth ? null : birth);
Boolean student = model.student;
updateArgs.add(null == student ? null : student ? 1 : 0);
boolean isSucceed = model.isSucceed;
updateArgs.add(isSucceed ? 1 : 0);
}
@Override
public void bindPkArgs(Person model, List<Object> pkArgs) {
Integer id = model.id;
pkArgs.add(null == id ? null : id);
Integer typeId = model.typeId;
pkArgs.add(null == typeId ? null : typeId);
}
@Override
public Person parseFromCursor(Cursor cursor) {
Person model = new Person();
int index;
index = cursor.getColumnIndex("id");
if(-1 != index) {
model.id = cursor.isNull(index) ? null : (cursor.getInt(index));
}
index = cursor.getColumnIndex("type_id");
if(-1 != index) {
model.typeId = cursor.isNull(index) ? null : (cursor.getInt(index));
}
index = cursor.getColumnIndex("name");
if(-1 != index) {
model.name = cursor.isNull(index) ? null : (cursor.getString(index));
}
index = cursor.getColumnIndex("age");
if(-1 != index) {
model.age = cursor.isNull(index) ? null : (cursor.getInt(index));
}
index = cursor.getColumnIndex("address");
if(-1 != index) {
model.address = cursor.isNull(index) ? null : (cursor.getString(index));
}
index = cursor.getColumnIndex("birth");
if(-1 != index) {
model.birth = cursor.isNull(index) ? null : (cursor.getLong(index));
}
index = cursor.getColumnIndex("student");
if(-1 != index) {
model.student = cursor.isNull(index) ? null : (cursor.getInt(index) == 1);
}
index = cursor.getColumnIndex("is_succeed");
if(-1 != index) {
model.isSucceed = cursor.isNull(index) ? null : (cursor.getInt(index) == 1);
}
return model;
}
}
2.1.3 注册持久类
与v1.0一样,新建类DatabaseFactory,继承RapidORMConnection(可参考http://www.cnblogs.com/tiantianbyconan/p/4748077.html)。
需要注意的是需要实现的并不是原来的registerAllTableClass()方法,而是registerTableConfigMapper(HashMap<Class, TableConfig> tableConfigMapper)方法:
// ...
@Override
protected void registerTableConfigMapper(HashMap<Class, TableConfig> tableConfigMapper) {
tableConfigMapper.put(Person.class, new Person_RORM());
// register all table config here...
}
// ...
注意:注册Person时,需要连带生成的Person_RORM同时注册。
2.1.4 构建Builder
构建Builder时与v1.0的方式一致,但是可以直接使用Person_RORM中的static变量来作为column name:
PersonDaoImpl:
public List<Person> findPersonsByWhere() throws Exception {
return queryBuilder()
.addSelectColumn(Person_RORM.ID, Person_RORM.TYPE_ID, Person_RORM.NAME,
Person_RORM.AGE, Person_RORM.BIRTH, Person_RORM.ADDRESS)
.setWhere(
Where.and(
Where.like(Person_RORM.NAME, "%wangjie%"),
Where.lt(Person_RORM.ID, 200),
Where.or(
Where.between(Person_RORM.AGE, 19, 39),
Where.isNull(Person_RORM.ADDRESS)
),
Where.eq(Person_RORM.TYPE_ID, 1)
)
)
.addOrder(Person_RORM.ID, false)
.addOrder(Person_RORM.NAME, true)
.setLimit(10)
.query(this);
}
2.1.5 兼容SqlCipher
与v1.0一致.
[Android]Android端ORM框架——RapidORM(v2.0)的更多相关文章
- [Android]Android端ORM框架——RapidORM(v2.1)
以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/6020412.html [Android]Android端ORM ...
- [Android]Android端ORM框架——RapidORM(v1.0)
以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/4748077.html Android上主流的ORM框架有很多 ...
- ORM框架--GreenDao 3.0基本使用指南
0. ORM框架--GreenDao 3.0基本使用指南 1. Gradle 的配置 这里可以参照官方的文档进行最新的配置(本示例的版本等你看到可能就不是最新的了),但是值得注意的一点是,GreenD ...
- Android 常用的ORM框架详解
1. OrmLite OrmLite 不是 Android 平台专用的ORM框架,它是Java ORM.支持JDBC连接,Spring以及Android平台.语法中广泛使用了注解(Annotation ...
- .NET 开源SqlServer ORM框架 SqlSugar 3.0 API
3.1.x ,将作为3.X系统的最后一个版本,下面将会开发 全新的功能 更新列表:https://github.com/sunkaixuan/SqlSugar/releases 优点: SqlSuga ...
- android中的http框架,使其更加简单易用
Afinal 是一个android的sqlite orm 和 ioc 框架. Afinal 是一个android的sqlite orm 和 ioc 框架.同时封装了android中的http框架,使其 ...
- ThinkAndroid是简洁,快速的进行Android应用程序的框架
ThinkAndroid简介ThinkAndroid是一个免费的开源的.简易的.遵循Apache2开源协议发布的Android开发框架,其开发宗旨是简单.快速的进行Android应用程序的开发,包含A ...
- .NET ORM框架 SqlSuagr4.0 功能详解与实践【开源】
SqlSugar 4.0 ORM框架的优势 为了未来能够更好的支持多库分布式的存储,并行计算等功能,将SqlSugar3.x全部重写,现有的架构可以轻松扩展多库. 源码下载: https://gith ...
- .NET ORM框架 SqlSugar4.0 功能快速预览【开源】
SqlSugar 4.0 ORM框架的优势 为了未来能够更好的支持多库分布式的存储,并行计算等功能,将SqlSugar3.x全部重写,现有的架构可以轻松扩展多库. 源码下载: https://gith ...
随机推荐
- ola.hallengren的SQL Server维护脚本
ola.hallengren的SQL Server维护脚本 下载地址 http://files.cnblogs.com/files/lyhabc/ola.hallengrenMaintenanceSo ...
- 全面解析ASP.NET MVC模块化架构方案
什么叫架构?揭开架构神秘的面纱,无非就是:分层+模块化.任意复杂的架构,你也会发现架构师也就做了这两件事. 本文将会全面的介绍我们团队在模块化设计方面取得的经验.之所以加了“全面”二字,是因为本文的内 ...
- .NET基础拾遗(7)Web Service的开发与应用基础
Index : (1)类型语法.内存管理和垃圾回收基础 (2)面向对象的实现和异常的处理 (3)字符串.集合与流 (4)委托.事件.反射与特性 (5)多线程开发基础 (6)ADO.NET与数据库开发基 ...
- 2000条你应知的WPF小姿势 基础篇<51-56 依赖属性>
前一阵子由于个人生活原因,具体见上一篇,耽搁了一阵子,在这里也十分感谢大家支持和鼓励.现在开始继续做WPF2000系列. 在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件 ...
- python scikit-learn 环境搭建问题解决记录
之前一直用pycharm 里内置的pip进行python 包的安装,今天装scikit-learn时没报错,但是报scipy包不识别,pip下载也报错下载anaconda 集成插件,最终问题解决:参考 ...
- Apache thrift RPC 双向通信
在上一篇介绍Apache thrift 安装和使用,写了一个简单的demo,讲解thrift服务的发布和客户端调用,但只是单向的客户端发送消息,服务端接收消息.而客户端却得不到服务器的响应. 在不涉及 ...
- T-sql语句查询执行顺序
前言 数据库的查询执行,毋庸置疑是程序员必备技能之一,然而数据库查询执行的过程绚烂多彩,却是很少被人了解,今天哥哥要带你装逼带你飞,深入一下这sql查询的来龙去脉,为查询的性能优化处理打个基础,或许面 ...
- c#官方推荐md5通用加密类
/// <summary> /// MD5加密 /// </summary> /// <param name="input">需要加密的字符串& ...
- 记2016腾讯 TST 校招面试经历,电面、笔试写代码、技术面、hr面,共5轮
(出处:http://www.cnblogs.com/linguanh/) 前序: 距离 2016 腾讯 TST 校招面试结束已经5天了,3月27日至今,目前还在等待消息.从投简历到两轮电面,再到被 ...
- 微信支付:H5吊起支付API,不显示“确认支付、输入密码”界面
使用公众号进行支付,官方开发帮助文档: https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_1 其业务流程如下: 按照业务流程进行开发 ...