對(duì)JFianl的Model?部分進(jìn)一步的擴(kuò)展,對(duì)QueryParams增加一個(gè)方法。今天增加的東西也不是很多。這幾天有點(diǎn)懶了吧。
接下來就直接開始講吧。
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());
}
以上這兩個(gè)方法主要用來查詢記錄集的,第一個(gè)方法就不用介紹了吧。
我們講講?第二個(gè)方法,shql這個(gè)字符串差不多就是類似與hibernate里面有的一種sql語句,?就是條件參數(shù)不是用"?"保留,而是用對(duì)映的名稱標(biāo)識(shí)這樣比較通俗易懂。
例子:
shql="select?*?from?zz?z?where?z.name?=?:name"attrs.put("name",?"張三")countSqlResult(shql,attrs);
這個(gè)方法里面有用到這個(gè)方法:QueryParams.toFormatSQL(),之前在QueryParams有提到過這個(gè)方法,但之前的是只有第一個(gè)參數(shù)的。我將其擴(kuò)展了一下吧。
代碼如下:
/***?*?@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;
}
接下來這兩個(gè)方法就不用講了吧,參數(shù)跟上面一樣的意思
/**
*
*??例子:
*??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;
}
接下來幾個(gè)就比較簡(jiǎn)單了。批量操作
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列表獲取對(duì)象列表.**?@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
轉(zhuǎn)載于:https://blog.51cto.com/7041393/1681440
總結(jié)
以上是生活随笔為你收集整理的JFinal model简单包装,版本2的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。