对JFianl的Model 部分进一步的扩展,对QueryParams增加一个方法。今天增加的东西也不是很多。这几天有点懒了吧。
接下来就直接开始讲吧。
public Long countSqlResult(String sqlExceptSelect, Object... params) { List result = Db.query("SELECT COUNT(*) " + DbKit.replaceFormatSqlOrderBy(sqlExceptSelect), params); int size = result.size(); if (size == 1) { return ((Number) result.get(0)).longValue(); } return Long.valueOf(0); } public Long countSqlResult(String shql, Map<String, Object> attrs) { List<Object> params = new ArrayList<Object>(); String sqlExceptSelect = QueryParams.toFormatSQL(shql, attrs, params); return countSqlResult(sqlExceptSelect, params.toArray()); }
以上这两个方法主要用来查询记录集的,第一个方法就不用介绍了吧。
我们讲讲 第二个方法,shql这个字符串差不多就是类似与hibernate里面有的一种sql语句, 就是条件参数不是用"?"保留,而是用对映的名称标识这样比较通俗易懂。
例子:
shql="select * from zz z where z.name = :name" attrs.put("name", "张三") countSqlResult(shql,attrs);
这个方法里面有用到这个方法:QueryParams.toFormatSQL(),之前在QueryParams有提到过这个方法,但之前的是只有第一个参数的。我将其扩展了一下吧。
代码如下:
/** * * @param hsql * @param attrs * @param values * @return */ public static String toFormatSQL(String hsql, Map<String, Object> attrs, List<Object> values) { Matcher matcher = Pattern.compile(":(\\w+)").matcher(hsql); while ( matcher.find()){ String rexp = null; String group = matcher.group(1); Object ov = attrs.get(group); if (ov instanceof List) { StringBuilder sb = new StringBuilder(); List vs = (List) ov; for (Object v : vs) { sb.append("?,"); values.add(v); } sb.deleteCharAt(sb.length() - 1); rexp = sb.toString(); }else { values.add(ov); rexp = "?"; } hsql = hsql.replace(String.format(":%s", group), rexp); } return hsql; }
接下来这两个方法就不用讲了吧,参数跟上面一样的意思
/** * * 例子: * queryOrNamedQuery="select * from zz z where z.name = :name" * attrs.put("name", "张三") * findFirstBySQLQuery(queryOrNamedQuery, attrs) * @param queryOrNamedQuery * @param attrs * @return List */ public List<Map<String, Object>> findBySQLQuery(String queryOrNamedQuery, Map<String, Object> attrs) { List<Object> params = new ArrayList<Object>(); String sql = QueryParams.toFormatSQL(queryOrNamedQuery, attrs, params); List<Record> records = Db.find(sql, params); List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); for (Record record : records) { list.add(record.getColumns()); } return list; } /** * 获得一条记录 * 例子: * queryOrNamedQuery="select * from zz z where z.name = :name" * attrs.put("name", "张三") * findFirstBySQLQuery(queryOrNamedQuery, attrs) * * @param queryOrNamedQuery * @param attrs * @return */ public Map<String, Object> findFirstBySQLQuery(String queryOrNamedQuery, Map<String, Object> attrs) { List<Object> params = new ArrayList<Object>(); String sql = QueryParams.toFormatSQL(queryOrNamedQuery, attrs, params); List<Record> records = Db.find(sql, params); if (records.size() >= 1) { return records.get(0).getColumns(); } return null; }
接下来几个就比较简单了。批量操作
public boolean saveOrUpdate() { if (null == this.get(getPrimaryKey())) return save(); return update(); } public boolean saveAll(List<M> ms) { /** * 这样写确定好? */ for (M m : ms) { if (!m.saveOrUpdate()) { // throw new ActiveRecordException("保存失败!"); throw new ActiveRecordException("Save failed :" + m); } } return true; /* int size = 0; if ((size = ms.size()) <= 0) { throw new ActiveRecordException("(List<M> is null ?"); } Config config = this.getConfig(); Table table = this.getTable(); Connection conn = null; PreparedStatement pst = null; boolean result = false; StringBuilder sql = new StringBuilder(); ArrayList paras = new ArrayList(); config.getDialect().forModelSave(table, ms.get(0).getAttrs(), sql, paras); boolean e; try { conn = config.getConnection(); if(config.getDialect().isOracle()) { pst = conn.prepareStatement(sql.toString(), new String[]{table.getPrimaryKey()}); } else { pst = conn.prepareStatement(sql.toString(), 1); } config.getDialect().fillStatement(pst, paras); if (size >= 2 ){ for (int i = 1; i < size; i++ ) { config.getDialect().forModelSave(table, ms.get(i).getAttrs(), sql, paras); pst.addBatch(); } } int result1 = pst.executeUpdate(); this.getGeneratedKey(pst, table); clear(); e = result1 >= 1; } catch (Exception var12) { throw new ActiveRecordException(var12); } finally { config.close(pst, conn); } return e;*/ } public boolean deleteAll(List<M> ms) { for (M m : ms) { if (!m.delete()) throw new ActiveRecordException("Delete failed :" + m); } return true; } public boolean deleteAllById(List<PK> ids) { for (PK id : ids) { if (!deleteById(id)) throw new ActiveRecordException("Delete failed :" + id); } return true; } /** * 按PK列表获取对象列表. * * @param ids 主键ID集合 * @return List */ public List<M> get(Collection<Object> ids) { if (ids.size() <= 0) { return Collections.emptyList(); } QueryParams params = new QueryParams(); params.addIn(getPrimaryKey(), ids); String sqlExceptSelect = params.toSqlExceptSelect(getTableName(), "m"); return find(String.format("SELECT * %s", sqlExceptSelect), params.getParas().toArray()); }
差不多就这样吧,今天没有写多少。
需要具体源码可以看我的
github
, github地址:https:
//github.com/cnzzs/zjf
本文出自 “7031393” 博客,请务必保留此出处http://7041393.blog.51cto.com/7031393/1681440
原文地址:http://7041393.blog.51cto.com/7031393/1681440