> 特点 1.支持多数据源管理和配置 2.自动封装Entity 3.支持事务 4.支持存储过程的方便调用 5.支持lazy加载 6.支持分页查询 7.支持多种数据库H2,MySQL,Oracle,PostgrSQL,SQLServer 8.支持多种主流数据库连接池,如c3p0,proxool,dbcp,bonecp等 9.自动管理数据库连接的生命周期
> 要求 1.JDK 1.5 or later 2.如需要lazy加载,需要引入cglib或javaassit,具体可配置
**> Maven dependency net.sf.javaclub gerald-jorm 1.0.7.GA
> 示例 1.添加 Session session = Jorm.getSession(); User u = new User(“Gerald.Chen”, “男”, 21, “job”);; session.save(u);
2.删除 session.clean(User.class);// 清空表 session.delete(User.class, “id > 100”);// 指定条件删除
session.delete(user);
3.查询 User user = session.read(User.class, 1);// 根据主键加载
// 加载第一个 String sql = “SELECT * FROM t_user WHERE id > ?”; User user = session.loadFirst(User.class, sql, 88);
// 分页查询 String sql = “SELECT * FROM t_user WHERE id > ?”; SqlParams params = new SqlParams(sql, new Object[] { 6 }); params.setObjectClass(User.class); params.setFirstResult(3); params.setMaxResults(10); List users = session.list(params);
// 查询单个属性 String sql = “SELECT name FROM t_user WHERE id = 28”; String name = session.queryUniqueObject(sql);
// 查询属性列表 String sql = “SELECT name FROM t_user WHERE id > ?”; List names = session.list(String.class, sql, 200);
String sql = “SELECT age FROM t_user WHERE age > 18”; List ages = session.list(int.class, sql);
4.存储过程 final String pro = “{? = call hello_proc(?)}”; String r = session.call(new ProcedureCaller() {
public CallableStatement prepare() throws SQLException { CallableStatement cs = getSession().getConnection().prepareCall(pro); cs.setString(2, “World”); cs.registerOutParameter(1, Types.CHAR); return cs; }
public String callback(CallableStatement cs) throws SQLException { cs.execute(); return cs.getString(1); } });
5.事务处理 session.clean(User.class); session.beginTransaction(); try { User u = null; for(int i = 0; i < 1000; i++) { String sex = (i % 2 == 0 ? “男” : “女”); u = new User(fixedStr(6), sex, randNumber(100), randString(16)); session.save(u); if(i == 886) { Integer.parseInt(“kkk”); } } session.commit(); } catch (Exception e) { session.rollback(); } finally { session.endTransaction(); }
这是一个完全基于JDBC的轻量java orm framework, 目标定位于使用方便,简单,后续会增加许多新的特性
下载地址:http://sourceforge.net/projects/javaclub/files/jorm/
Simple CRUD http://www.oschina.net/code/snippet_151926_6316
处理特殊字段 http://www.oschina.net/code/snippet_151926_6355
处理多个Oracle存储过程返回的游标 http://www.oschina.net/code/snippet_151926_6356
批量操作 http://www.oschina.net/code/snippet_151926_6528[
](http://www.oschina.net/code/snippet_151926_6528)主键生成策略 http://www.oschina.net/code/snippet_151926_6531