迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:rexdb开源软件地址:https://gitee.com/rexsoft/rexdb开源软件介绍:
<dependency> <groupId>org.rex-soft</groupId> <artifactId>rexdb</artifactId> <version>1.0.4</version></dependency> 使用Maven添加依赖后,不要忘记将rexdb配置文件rexdb.xml拷贝到classpath路径中
概述Rexdb是一款使用Java语言编写的,开放源代码的持久层框架。提供了查询、更新、批处理、调用、(JTA)事务、数据源管理等功能,可以取代Mybatis、Hibernate作为系统的核心ORM框架。 Rexdb提供了工具类风格的接口,不需要编写映射配置,使用简便;同时,它还具备同类框架中最高效的执行效率。 性能功能
特点
技术支持Rexdb的更新十分缓慢,但发布的每一个版本均已在多个生产环境中稳定运行,包括企业、政务、金融系统等,因此稳定性有所保障。但由于Rexdb是免费的开源软件,因此除文档、源代码和示例外,无法提供日常的技术支持。 用户协议Rexdb基于Apache 2.0协议,可以免费用于个人或商业用途。 协议详情请见:Apache Lisence, Version 2.0 快速入门本文档用于快速了解Rexdb的使用方法,适合大部分的Java编程人员阅读。 开发/运行运行环境Rexdb需要如下运行环境:
在开始前,请检查环境变量中的如下jar包:
全局配置 rexdb.xmlRexdb依赖全局配置文件rexdb.xml,用于配置数据源、日志、异常信息语言等。该文件默认存放于classpath环境变量中(例如,在Java EE应用中,应将其放置于WEB-INF/classes目录中)。 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//rex-soft.org//REXDB DTD 1.0//EN" "http://www.rex-soft.org/dtd/rexdb-1-config.dtd"><configuration> <!-- 默认数据源,Oracle数据库,使用框架内置的连接池 --> <dataSource> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:rexdb" /> <property name="username" value="rexdb" /> <property name="password" value="12345678" /> </dataSource> <!-- id为“student”的数据源,Mysql数据库,使用了Apache DBCP连接池 --> <dataSource id="student" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/rexdb?characterEncoding=utf8" /> <property name="username" value="root" /> <property name="password" value="12345678" /> </dataSource> <!-- id为“school”的数据源,使用Tomcat JNDI --> <dataSource id="school" jndi="java:comp/env/rexdbJNDI"></dataSource></configuration> 如果希望从其它位置加载该配置文件,或是启用更多的功能,例如设置异常信息语言(中/英)、设置跟踪SQL的监听、设置超时时间等,请查询Rexdb用户手册。 查询单条记录 DB.get()
例1:执行SQL,并获取结果 Student stu = DB.get("select * from t_student where class='3年1班' and num=1", Student.class); 例2:执行带有预编译参数的SQL,当parameter参数为 Student stu = DB.get("select * from t_student where class=? and num=?", new Object[]{"3年1班", 1}, Student.class);Student stu = DB.get("select * from t_student where class=? and num=?", new Ps("3年1班", 1), Student.class); 例3:执行带有预编译参数的SQL,当parameter参数为 //map为java.util.Map类型的实例,包含名为“clazz”和“num”的键;obj为普通的java对象,包含名为“clazz”和“num”的成员变量Student stu = DB.get("select * from t_student where class=#{clazz} and num=#{num}", map, Student.class);Student stu = DB.get("select * from t_student where class=#{clazz} and num=#{num}", obj, Student.class); 例4:在指定数据源中执行SQL //配置文件rexdb.xml中有id为student的数据源Student stu = DB.get("student", "select * from t_student where class='3年1班' and num=1", Student.class); 查询单条Map记录 DB.getMap()
例1:执行SQL,并获取结果 RMap stu = DB.getMap("select * from t_student where class='3年1班' and num=1"); 例2:执行带有预编译参数的SQL,当parameter参数为 RMap stu = DB.getMap("select * from t_student where class=? and num=?", new Object[]{"3年1班", 1});RMap stu = DB.getMap("select * from t_student where class=? and num=?", new Ps("3年1班", 1)); 例3:执行带有预编译参数的SQL,当parameter参数为 //map为java.util.Map类型的实例,包含名为“class”和“num”的键;obj为普通的java对象,包含名为“clazz”和“num”的成员变量RMap stu = DB.getMap("select * from t_student where class=#{clazz} and num=#{num}", map);RMap stu = DB.getMap("select * from t_student where class=#{clazz} and num=#{num}", obj); 例4:在指定数据源中执行SQL //配置文件rexdb.xml中有id为student的数据源RMap stu = DB.getMap("student", "select * from t_student where class='3年1班' and num=1"); 查询多条记录 DB.getList()
例1:执行SQL,并获取结果 List<Student> list = DB.getList("select * from t_student where class='3年1班'", Student.class); 例2:执行带有预编译参数的SQL,当parameter参数为 List<Student> list = DB.getList("select * from t_student where class=?", new Object[]{"3年1班"}, Student.class);List<Student> list = DB.getList("select * from t_student where class=?", new Ps("3年1班"), Student.class); 例3:执行带有预编译参数的SQL,当parameter参数为 //map为java.util.Map类型的实例,包含名为“class”的键;obj为普通的java对象,包含名为“clazz”的成员变量List<Student> list = DB.getList("select * from t_student where class=#{clazz}", map, Student.class);List<Student> list = DB.getList("select * from t_student where class=#{clazz}", obj, Student.class); 例4:执行分页查询,查询第100~110条记录 List<Student> list = DB.getList("select * from t_student where class='3年1班'", Student.class, 100, 10); 例5:在指定数据源中执行SQL //配置文件rexdb.xml中有id为student的数据源List<Student> list = DB.getList("student", "select * from t_student where class='3年1班'", Student.class); 查询多条Map记录 DB.getMapList()
例1:执行SQL,并获取结果 List<RMap> list = DB.getMapList("select * from t_student where class='3年1班'"); 例2:执行带有预编译参数的SQL,当parameter参数为 List<RMap> list = DB.getMapList("select * from t_student where class=?", new Object[]{"3年1班"});List<RMap> list = DB.getMapList("select * from t_student where class=?", new Ps("3年1班")); 例3:执行带有预编译参数的SQL,当parameter参数为 //map为java.util.Map类型的实例,包含名为“class”的键;obj为普通的java对象,包含名为“clazz”的成员变量List<RMap> list = DB.getMapList("select * from t_student where class=#{clazz}", map);List<RMap> list = DB.getMapList("select * from t_student where class=#{clazz}", obj); 例4:执行分页查询,查询第100~110条记录 List<RMap> list = DB.getMapList("select * from t_student where class='3年1班'", 100, 10); 例5:在指定数据源中执行SQL //配置文件rexdb.xml中有id为student的数据源List<RMap> list = DB.getMapList("student", "select * from t_student where class='3年1班'"); 插入/更新/删除 DB.update()
例1:执行SQL DB.update("delete from t_student where num = 1"); 例2:执行带有预编译参数的SQL,当parameter参数为 string sql = "insert into t_student(num, student_name, student_class,create_time) values (?, ?, ?, ?)";DB.update(sql, new Object[]{1, "钟小强","3年1班", new Date()});DB.update(sql, new Ps(2, "王小五", "3年1班",new Date())); 例3:执行带有预编译参数的SQL,当parameter参数为 String sql = "update t_student set student_name = #{studentName} where num = #{num}";DB.update(sql,map);//map为java.util.Map类型的实例,包含名为“studentName”和“num”的键DB.update(sql,new Students(1, "钟小强", null, null));//obj为普通的java对象,包含名为“studentName”和“num”的成员变量 例4:在指定数据源中执行SQL //配置文件rexdb.xml中有id为student的数据源List<RMap> list = DB.getMapList("student", "delete from t_student where num = 1"); 批量更新 DB.batchUpdate()
例1:执行多个SQL String[] sqls = new String[]{"delete from t_student where num=1", "delete from t_student where num=2"};DB.batchUpdate(sqls); 例2:执行带有预编译参数的SQL,当parameter参数元素类型为 string sql = "insert into t_student(num, student_name, student_class,create_time) values (?, ?, ?, ?)";DB.batchUpdate(sql, new Object[][]{{1, "钟小强","3年1班", new Date()}, {2, "王小五","3年1班", new Date()}});DB.batchUpdate(sql, new Ps[]{new Ps(3, "李小华", "3年1班", new Date()), new Ps(4, "赵小明", "3年1班", new Date())}); 例3:执行带有预编译参数的SQL,当parameter参数元素类型为 String sql = "update t_student set student_name = #{studentName} where num = #{num}";DB.batchUpdate(sql, maps);//maps为java.util.Map数组实例,数组中每个元素都包含名为“studentName”和“num”的键DB.batchUpdate(sql, objs);//objs为Student类型的java对象实例数组,Student对象包含名为“studentName”和“num”的成员变量 例4:在指定数据源中执行SQL String[] sqls = new String[]{"delete from t_student where num=1", "delete from t_student where num=2"};DB.batchUpdate("student", sqls); 事务Rexdb使用编程的方式处理事务,以下接口用于事务处理:
JTA事物接口如下:
例: DB.beginTransaction();try{ DB.update("delete from t_student where num = 1"); DB.update("delete from t_student where num = 2"); UserDao.update();//Rexdb的事物是线程级别的,同一线程且同一数据源中的事物对象共享,所以可以把原子操作分散在多个DAO中 DB.commit();}catch(Exception e){ DB.rollback();} 调用 DB.call()
例1:调用存储过程/函数 DB.call("test_proc()"); 例2:调用有输入参数的存储过程/函数 DB.call("{call test_proc_in(?)", new Ps(200)); 例3:调用有输出参数的存储过程/函数时,必须使用org.rex.db.Ps对象声明输出参数 Ps ps = new Ps();ps.addOutInt("age");RMap result = DB.call("{call test_proc_out(?)}", ps);int age = result.getInt("age") 例4:调用同时有输入输出参数的存储过程/函数,必须使用org.rex.db.Ps对象,并按照SQL中标记的顺序声明 Ps ps = new Ps();ps.add(200);ps.addOutInt("major");RMap result = DB.call("{call test_proc_in_out(?, ?)}", ps);int major = result.getInt("major"); 例5:调用即是输入参数也是输出参数的存储过程/函数,必须使用org.rex.db.Ps对象 Ps ps = new Ps();ps.addInOut("count", 10);RMap result = DB.call("{call test_proc_inout(?)}", ps);int count = result.getInt("count"); 例6:调用带有返回值的存储过程/函数,返回值将按照return_1、return_2的顺序命名 RMap result = DB.call("{call exdb_test_proc_return()}");List<RMap> return1 = result.getList("return_1"); 关于调用的其它用法请参见用户手册。 更多Rexdb还有更多功能,例如:
详情请参见Rexdb用户手册。 附1:类 org.rex.RMap类 例1:查询记录数 int c = DB.getMap("select count(*) as c from t_student").getInt("c"); 例2:查询数据库中的当前时间(oracle) Date now = DB.getMap("select sysdate as now from dual").getDate("now"); 例3:获取某日期字段(SQL查询出的类型为String,RMap自动将其转换为Date类型) Date date = DB.getMap("select '2016-01-01' as date from dual").getDate("date"); 有关该类的更多使用方法请参见Rexdb用户手册。 附2:类 org.rex.db.Ps类 例1:使用构造函数按顺序声明预编译参数 RMap stu = DB.get("select * from t_student where class=? and num=?", new Ps("3年1班", 1)); 例2:逐个声明预编译参数 Ps ps = new Ps();ps.add("3年1班");ps.add(1);RMap stu = DB.get("select * from t_student where class=? and num=?", ps); 例3:声明存储过程的输出参数 Ps ps = new Ps();ps.addOutInt("age");RMap result = DB.call("{call test_proc_out(?)}", ps);int age = result.getInt("age") 有关该类的更多使用方法请参见Rexdb用户手册。 附3:作者的一些建议
![]() |
请发表评论