//${tableName} <#--包名 --> package ${packages} <#-- 所有的引入--> <#list imports as ilist> <#if ilist?exists>${ilist}<#else><#rt> import java.math.BigDecimal; /** <#if version?exists> * @version ${version} */ <#--类, 还是接口 名称, 继承的类, 实现的接口 --> public class ${beanName}Dao { <#--变量 --> <#if propertys?exists> <#list propertys as prolist> <#if prolist?exists> ${prolist}<#rt> /** * 初始化 */ public ${beanName}Dao() { } /** * 初始化 */ public ${beanName}Dao(String uuid) { this.uuid = uuid; } /** * 初始化 */ public ${beanName}Dao(String uuid, Logger logger) { this.uuid = uuid; // if(logger != null){ // this.logger = logger; // } } /** * 提交 */ public void commit() { JdbcUtil.commit(writeConnectionName); } /** * 回滚 */ public void rollback() { JdbcUtil.rollback(writeConnectionName); } /** * 初始化cacheColums */ public static void initCache() { cacheColums = new ArrayList(); } /** * 线程修改cacheColums */ public static void threadResetCache(ArrayList list) { if (cacheColums == null) initCache(); cacheColums.clear(); for (int i = 0; i < list.size(); i++) cacheColums.add((String) list.get(i)); } /** * 重置cache */ public void resetCache(final List<${beanName}PO> list) { if (null == list || list.size() <= 0) return; Thread thread = new Thread() { public void run() { List haveResetList = new ArrayList(); for (${beanName}PO po : list) { Memcached.delete("1_${tableName}_PK_" + po.get${pkBeanName}()); Log.info("cacheinfo:[UUID:"+uuid+"]", logger,"一级缓存删除 key[1_${tableName}_PK_" + po.get${pkBeanName}() + "]"); Long keyTime = System.currentTimeMillis(); for (int i = 0; i < cacheColums.size(); i++) { String key = (String) cacheColums.get(i); String cache_key = "2_${tableName}_" + key + "_" + StringUtil.getValueFromObjectByName(po, key); if (!haveResetList.contains(cache_key)) { Memcached.add(2, cache_key, keyTime); Log.info("cacheinfo:[UUID:"+uuid+"]", logger,"二级缓存重置 key[" + cache_key + "]"); haveResetList.add(cache_key); } } } Memcached.add(4, "4_${tableName}", System.currentTimeMillis()); Log.info("cacheinfo:[UUID:"+uuid+"]", logger,"四级缓存重置 key[4_${tableName}]"); } }; thread.start(); } /** * 从缓存中获取缓存列 */ public Object[] getCacheColFromCache(List condition) { Object[] obj = { null, null }; for (Object[] arg : condition) { if (cacheColums.contains((String) arg[0])) { if(!"=".equals((String)arg[1])){ //操作符为"="允许使用缓存列 continue; } obj[0] = arg[0] + "_" + arg[2]; obj[1] = Memcached.get("2_${tableName}_" + obj[0]); Log.info("cacheinfo:[UUID:"+uuid+"]", logger,"二级缓存获取 key["+obj[0]+"],value["+obj[1]+"]"); return obj; } } return null; } /** * 根据Request Map 生成PO对象 */ public ${beanName}PO get${beanName}POFromRequest(Map map) throws Exception { ${beanName}PO po = new ${beanName}PO(); <#if reqParameterToPOMap?exists> <#list reqParameterToPOMap?keys as poKey> po.set${reqParameterToPOMap[poKey]}; return po; } /** * 按主键查询 */ public ${beanName}PO find${beanName}POByID(${pkType} id) { return find${beanName}POByID(id, false, true, true, false); } /** * 按主键查询 * isLock 是否锁定, 默认不锁 * isUseCache 是否用缓存, 默认用 * isAddCache 查询结果是否添加到缓存, 默认添加 */ public ${beanName}PO find${beanName}POByID(${pkType} id, boolean isLock, boolean isUseCache, boolean isAddCache, boolean fromWriteDB) { Date starttime = null; if (isLogInfo()) { starttime = new Date(); } if (isUseCache) {// 从缓存中查询 Log.info("cacheinfo:[UUID:"+uuid+"]", logger,"从一级缓存中查询,查询条件"+id); Object obj = Memcached.get("1_${tableName}_PK_" + id); if (obj != null) { if (isLogInfo()) { Date endtime = new Date(); Log.info("cacheinfo:[UUID:"+uuid+"]", logger, "${beanName}Dao.find${beanName}POByID(" + id + ", " + isLock + ", " + isUseCache + ", " + isAddCache + ", " + fromWriteDB + ")\n"+uuid + "一级缓存命中 key[1_${tableName}_PK_" + id +"]\n"+uuid + "一级缓存查询结果:" + ((${beanName}PO) obj).toString() +"\n"+uuid + "一级缓存查询时长:" + (endtime.getTime()-starttime.getTime()) + "毫秒(" + StringUtil.dateToFormatStr(starttime, "yyyy-MM-dd HH:mm:ss.SSS") + "*-*" + StringUtil.dateToFormatStr(endtime, "yyyy-MM-dd HH:mm:ss.SSS") + ")"); } return (${beanName}PO) obj; } } StringBuffer sql = new StringBuffer("SELECT ${column_list_str} FROM ${tableName}"); StringBuffer pql = new StringBuffer(sql.toString()); sql.append(" WHERE ${pkColumName} = ?"); pql.append(" WHERE ${pkColumName} = " + id); if (isLock) { sql.append(" FOR UPDATE"); pql.append(" FOR UPDATE"); } List<${beanName}PO> resultList = null; try { if (isLogInfo()) { Log.info("cacheinfo:[UUID:"+uuid+"]", logger, "${beanName}Dao.find${beanName}POByID(" + id + ", " + isLock + ", " + isUseCache + ", " + isAddCache + ", " + fromWriteDB + ")\n"+uuid + "连接名:" + (isLock ? writeConnectionName : (fromWriteDB ? writeConnectionName : readConnectionName)) + "\n"+uuid + "准备执行SQL:" + pql.toString()); } resultList = JdbcUtil.executeQuery(uuid, (isLock ? writeConnectionName : (fromWriteDB ? writeConnectionName : readConnectionName)), sql.toString(), new DAORowMapper<${beanName}PO>(${beanName}PO.class), id); if (resultList == null || resultList.size() == 0) { Memcached.delete("1_${tableName}_PK_" + id); Log.info("cacheinfo:[UUID:"+uuid+"]", logger,"一级缓存删除 key[1_${tableName}_PK_" + id + "]"); return null; } if (isAddCache) {// 把结果添加到缓存 Memcached.add(1, "1_${tableName}_PK_" + id, resultList.get(0)); Log.info("cacheinfo:[UUID:"+uuid+"]", logger,"一级缓存添加 key[1_${tableName}_PK_" + id + "]"); } return resultList.get(0); } catch (Exception e) { Log.error("error:[UUID:"+uuid+"]", logger, e, pql.toString(), id); } finally { if (isLogInfo()) { Date endtime = new Date(); Log.info("cacheinfo:[UUID:"+uuid+"]", logger, "${beanName}Dao.find${beanName}POByID(" + id + ", " + isLock + ", " + isUseCache + ", " + isAddCache + ", " + fromWriteDB + ")\n"+uuid + "连接名:" + (isLock ? writeConnectionName : (fromWriteDB ? writeConnectionName : readConnectionName)) + "\n"+uuid + "执行SQL:" + pql.toString() + "\n"+uuid + "返回数据条数:" + (resultList==null?0:resultList.size()) + "\n"+uuid + "方法执行时长:" + (endtime.getTime()-starttime.getTime()) + "毫秒(" + StringUtil.dateToFormatStr(starttime, "yyyy-MM-dd HH:mm:ss.SSS") + "*-*" + StringUtil.dateToFormatStr(endtime, "yyyy-MM-dd HH:mm:ss.SSS") + ")"); } } return null; } /** * 根据条件List查询 * Object[]数组长度是3 * Object[], 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 */ public List<${beanName}PO> find${beanName}POListByCondition(List condition) { return find${beanName}POListByCondition(condition, true, true, false); } /** * 根据条件List查询 * Object[]数组长度是3 * Object[], 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 * boolean isUseCache, 是否用缓存,默认用。 * boolean isAddCache, 是否添加缓存,默认添加。 */ public List<${beanName}PO> find${beanName}POListByCondition(List condition, boolean isUseCache, boolean isAddCache, boolean fromWriteDB) { if (StringUtil.isAutowired(condition)) { Log.error("error:[UUID:"+uuid+"]", logger, new Exception("请求条件异常,无效请求!"), null, null); return null; } Date starttime = null; if (isLogInfo()) { starttime = new Date(); } StringBuffer sql = new StringBuffer("SELECT ${column_list_str} FROM ${tableName}"); StringBuffer pql = new StringBuffer(sql.toString()); List list = new ArrayList(); JdbcUtil.appendWhereCondition(sql, pql, list, condition); List<${beanName}PO> resultList = null; Object oColumAndTimeKey[] = null; Object oTime = null; if (isUseCache) {// 从缓存中查询 Log.info(logger,uuid,"从二级缓存中查询","查询条件",StringUtil.transferObjectList(condition)); oColumAndTimeKey = getCacheColFromCache(condition); oTime = (oColumAndTimeKey == null) ? null : oColumAndTimeKey[1]; if (null != oTime) { Object object = Memcached.get("3_" + MD5.md5(pql.toString() + oTime.toString())); if (object != null) { List<${beanName}PO> r = (List<${beanName}PO>) object; if (isLogInfo()) { Date endtime = new Date(); Log.info("cacheinfo:[UUID:"+uuid+"]", logger, "${beanName}Dao.find${beanName}POListByCondition(" + condition.toString() + ", " + isUseCache + ", " + isAddCache + ", " + fromWriteDB + ")\n"+uuid + "二级三级缓存命中 key[3_" + MD5.md5(pql.toString() + oTime.toString()) + "]\n"+uuid + "三级缓存查询返回条数:" + (r==null?0:r.size()) + "\n"+uuid + "三级缓存查询时长:" + (endtime.getTime()-starttime.getTime()) + "毫秒(" + StringUtil.dateToFormatStr(starttime, "yyyy-MM-dd HH:mm:ss.SSS") + "*-*" + StringUtil.dateToFormatStr(endtime, "yyyy-MM-dd HH:mm:ss.SSS") + ")"); } return r; } } } try { if (isLogInfo()) { Log.info("cacheinfo:[UUID:"+uuid+"]", logger, "${beanName}Dao.find${beanName}POListByCondition(" + condition.toString() + ", " + isUseCache + ", " + isAddCache + ", " + fromWriteDB + ")\n"+uuid + "连接名:" + (fromWriteDB ? writeConnectionName : readConnectionName) + "\n"+uuid + "准备执行SQL:" + pql.toString() ); } resultList = JdbcUtil.executeQuery(uuid, fromWriteDB ? writeConnectionName : readConnectionName, sql.toString(), new DAORowMapper<${beanName}PO>(${beanName}PO.class), list.toArray()); if (isAddCache) {// 把结果添加到缓存 if (oColumAndTimeKey == null && !isUseCache) oColumAndTimeKey = getCacheColFromCache(condition); if (oColumAndTimeKey != null) { if (oColumAndTimeKey[1] == null) { oColumAndTimeKey[1] = System.currentTimeMillis(); Memcached.add(2, "2_${tableName}_" + oColumAndTimeKey[0], oColumAndTimeKey[1]); Log.info("cacheinfo:[UUID:"+uuid+"]", logger,"二级缓存添加 key[2_${tableName}_" + oColumAndTimeKey[0] + "]" ); } Memcached.add(3, "3_" + MD5.md5(pql.toString() + oColumAndTimeKey[1]), resultList); Log.info("cacheinfo:[UUID:"+uuid+"]", logger,"三级缓存添加 key[3_" + MD5.md5(pql.toString() + oColumAndTimeKey[1]) + "]" ); } } return resultList; } catch (Exception e) { Log.error("error:[UUID:"+uuid+"]", logger, e, pql.toString(), null); } finally { if (isLogInfo()) { Date endtime = new Date(); Log.info("cacheinfo:[UUID:"+uuid+"]", logger, "${beanName}Dao.find${beanName}POListByCondition(" + condition.toString() + ", " + isUseCache + ", " + isAddCache + ", " + fromWriteDB + ")\n"+uuid + "连接名:" + (fromWriteDB ? writeConnectionName : readConnectionName) + "\n"+uuid + "执行SQL:" + pql.toString() + "\n"+uuid + "返回条数:" + (resultList==null?0:resultList.size()) + "\n"+uuid + "方法执行时长:" + (endtime.getTime()-starttime.getTime()) + "毫秒(" + StringUtil.dateToFormatStr(starttime, "yyyy-MM-dd HH:mm:ss.SSS") + "*-*" + StringUtil.dateToFormatStr(endtime, "yyyy-MM-dd HH:mm:ss.SSS") + ")"); } } return null; } /** * 根据条件sql查询 * sqlCondition 为where 后面的条件。 */ public List<${beanName}PO> find${beanName}POListBySql(String sqlCondition) { return find${beanName}POListBySql(sqlCondition, true, true, false); } /** * 根据条件sql查询 * sqlCondition 为where 后面的条件。 */ public List<${beanName}PO> find${beanName}POListBySql(String sqlCondition, boolean isUseCache, boolean isAddCache, boolean fromWriteDB) { if (StringUtil.isAutowired(sqlCondition)) { Log.error("error:[UUID:"+uuid+"]", logger, new Exception("请求条件异常,无效请求!"), null, null); return null; } Date starttime = null; if (isLogInfo()) { starttime = new Date(); } StringBuffer sql = new StringBuffer("SELECT ${column_list_str} FROM ${tableName} WHERE " + sqlCondition); List<${beanName}PO> resultList = null; Object oTime = null; if (isUseCache) {// 从缓存中查询 oTime = Memcached.get("4_${tableName}"); Log.info("cacheinfo:[UUID:"+uuid+"]", logger,"四级缓存中获取 key[4_${tableName}],value[" + oTime + "]"); if (null != oTime) { Object object = Memcached.get("3_" + MD5.md5(sql.toString() + oTime.toString())); if (null != object) { List<${beanName}PO> r = (List<${beanName}PO>) object; if (isLogInfo()) { Date endtime = new Date(); Log.info("cacheinfo:[UUID:"+uuid+"]", logger, "${beanName}Dao.find${beanName}POListBySql(" + sqlCondition + ", " + isUseCache + ", " + isAddCache + ", " + fromWriteDB + ")\n"+uuid + "四级三级缓存命中 key[3_" + MD5.md5(sql.toString() + oTime.toString()) + "]\n"+uuid + "三级缓存返回条数:" + (r==null?0:r.size()) + "\n"+uuid + "三级缓存查询时长:" + (endtime.getTime()-starttime.getTime()) + "毫秒(" + StringUtil.dateToFormatStr(starttime, "yyyy-MM-dd HH:mm:ss.SSS") + "*-*" + StringUtil.dateToFormatStr(endtime, "yyyy-MM-dd HH:mm:ss.SSS") + ")"); } return r; } } } try { if (isLogInfo()) { Log.info("cacheinfo:[UUID:"+uuid+"]", logger, "${beanName}Dao.find${beanName}POListBySql(" + sqlCondition + ", " + isUseCache + ", " + isAddCache + ", " + fromWriteDB + ")\n"+uuid + "连接名:" + (fromWriteDB ? writeConnectionName : readConnectionName) + "\n" + "准备执行SQL:" + sql.toString() ); } resultList = JdbcUtil.executeQuery(uuid, fromWriteDB ? writeConnectionName : readConnectionName, sql.toString(), new DAORowMapper<${beanName}PO>(${beanName}PO.class)); if (isAddCache) {// 重置缓存 long keyTime = 0; if (oTime != null) { keyTime = (Long) oTime; } else { keyTime = System.currentTimeMillis(); Memcached.add(4, "4_${tableName}", keyTime); Log.info("cacheinfo:[UUID:"+uuid+"]", logger,"四级缓存重置 key[4_${tableName}]"); } Memcached.add(3, "3_" + MD5.md5(sql.toString() + keyTime), resultList); Log.info("cacheinfo:[UUID:"+uuid+"]", logger,"三级缓存添加 key[3_" + MD5.md5(sql.toString() + keyTime) + "]"); } return resultList; } catch (Exception e) { Log.error("error:[UUID:"+uuid+"]", logger, e, sql.toString(), null); } finally { if (isLogInfo()) { Date endtime = new Date(); Log.info("cacheinfo:[UUID:"+uuid+"]", logger, "${beanName}Dao.find${beanName}POListBySql(" + sqlCondition + ", " + isUseCache + ", " + isAddCache + ", " + fromWriteDB + ")\n"+uuid + "连接名:" + (fromWriteDB ? writeConnectionName : readConnectionName) + "\n"+uuid + "执行SQL:" + sql.toString() + "\n" + "返回条数:" + (resultList==null?0:resultList.size()) + "\n"+uuid + "方法执行时长:" + (endtime.getTime()-starttime.getTime()) + "毫秒(" + StringUtil.dateToFormatStr(starttime, "yyyy-MM-dd HH:mm:ss.SSS") + "*-*" + StringUtil.dateToFormatStr(endtime, "yyyy-MM-dd HH:mm:ss.SSS") + ")"); } } return null; } /** * 按条件分页查询 * Object[]数组长度是3 * Object[], 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 */ public Map findPageByCondition(List condition, int page, int pageSize, String sortCondition) { return findPageByCondition(condition, page, pageSize, true, true, sortCondition, false); } /** * 按条件分页查询 * Object[]数组长度是3 * Object[], 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 * boolean isUseCache, 是否用缓存,默认用。 * boolean isAddCache, 是否添加缓存,默认添加。 */ public Map findPageByCondition(List condition, int page, int pageSize, boolean isUseCache, boolean isAddCache, String sortCondition, boolean fromWriteDB) { if (StringUtil.isAutowired(condition)) { Log.error("error:[UUID:"+uuid+"]", logger, new Exception("请求条件异常,无效请求!"), null, null); return null; } Date starttime = null; if (isLogInfo()) { starttime = new Date(); } <#if dbType = "mysql"> StringBuffer sql = new StringBuffer("SELECT ${column_list_str} FROM ${tableName}"); StringBuffer pql = new StringBuffer(sql.toString()); <#else> StringBuffer sql = new StringBuffer("SELECT ${column_list_str}, ROWNUM RN FROM ${tableName}"); StringBuffer pql = new StringBuffer(sql.toString()); StringBuffer sqlCount = new StringBuffer("SELECT COUNT(1) FROM ${tableName}"); List count_list = new ArrayList(); List page_list = new ArrayList(); JdbcUtil.appendWhereConditionForCount(sqlCount, condition); JdbcUtil.appendWhereCondition(sql, pql, count_list, condition); for (int i = 0; i < count_list.size(); i++) page_list.add(count_list.get(i)); <#if dbType == "mysql"> page_list.add((page - 1) * pageSize); page_list.add(page * pageSize); if(StringUtil.isEmpty(sortCondition)){ String pageSql = sql.toString() + " limit ?, ?"; String pagePql = pql.toString() + " limit " + (page -1) * pageSize + ", " + page * pageSize; }else{ String pageSql = sql.toString() + sortCondition + " limit ?, ?"; String pagePql = pql.toString() + sortCondition + " limit " + (page -1) * pageSize + ", " + page * pageSize; } <#else> String pageSql = null; String pagePql = null; if (page_list.size() == 0) { if(StringUtil.isEmpty(sortCondition)){ pageSql = "SELECT * FROM (" + sql.toString() + " AND ROWNUM <= ?" + ") A WHERE A.RN >= ?"; pagePql = "SELECT * FROM (" + pql.toString() + " AND ROWNUM <= " + page * pageSize + ") A WHERE A.RN >= " + ((page -1) * pageSize + 1); }else{ pageSql = "SELECT * FROM (SELECT B.*,ROWNUM RNN FROM (" + sql.toString() + " " + sortCondition + ") B WHERE ROWNUM <= ?"+" ) A WHERE A.RNN >= ?"; pagePql = "SELECT * FROM (SELECT B.*,ROWNUM RNN FROM (" + pql.toString() + " " + sortCondition + ") B WHERE ROWNUM <= " + page * pageSize + ") A WHERE A.RNN >= " + ((page -1) * pageSize + 1); } } else { if(StringUtil.isEmpty(sortCondition)){ pageSql = "SELECT * FROM (" + sql.toString() + " AND ROWNUM <= ?" + ") A WHERE A.RN >= ?"; pagePql = "SELECT * FROM (" + pql.toString() + " AND ROWNUM <= " + page * pageSize + ") A WHERE A.RN >= " + ((page -1) * pageSize + 1); }else{ pageSql = "SELECT * FROM (SELECT B.*,ROWNUM RNN FROM (" + sql.toString() + " " + sortCondition + ") B WHERE ROWNUM <= ?"+" ) A WHERE A.RNN >= ?"; pagePql = "SELECT * FROM (SELECT B.*,ROWNUM RNN FROM (" + pql.toString() + " " + sortCondition + ") B WHERE ROWNUM <= " + page * pageSize + ") A WHERE A.RNN >= " + ((page -1) * pageSize + 1); } } page_list.add(page * pageSize); page_list.add((page - 1) * pageSize + 1); Object oColumAndTimeKey[] = null; Object oTime = null; if (isUseCache) {// 从缓存中查询 Log.info(logger,uuid,"从二级缓存中查询","查询条件",StringUtil.transferObjectList(condition)); oColumAndTimeKey = getCacheColFromCache(condition); oTime = (oColumAndTimeKey == null) ? null : oColumAndTimeKey[1]; if (null != oTime) { Object object = Memcached.get("3_" + MD5.md5(pagePql.toString() + oTime.toString())); if (null != object) { if (isLogInfo()) { Date endtime = new Date(); Log.info("cacheinfo:[UUID:"+uuid+"]", logger, "${beanName}Dao.findPageByCondition(" + condition.toString() + ", " + page + ", " + pageSize + ", " + isUseCache + ", " + isAddCache + ", " + fromWriteDB + ")\n"+uuid + "二级三级缓存命中 key[3_" + MD5.md5(pagePql.toString() + oTime.toString()) + "]\n"+uuid + "三级缓存返回条数:" + (((Map) object).get("list") == null?0:((List<${beanName}PO>)((Map) object).get("list")).size()) + "\n"+uuid + "三级缓存查询时长:" + (endtime.getTime()-starttime.getTime()) + "毫秒(" + StringUtil.dateToFormatStr(starttime, "yyyy-MM-dd HH:mm:ss.SSS") + "*-*" + StringUtil.dateToFormatStr(endtime, "yyyy-MM-dd HH:mm:ss.SSS") + ")"); } return (Map) object; } } } List<${beanName}PO> resultList = null; try { if (isLogInfo()) { Log.info("cacheinfo:[UUID:"+uuid+"]", logger, "${beanName}Dao.findPageByCondition(" + condition.toString() + ", " + page + ", " + pageSize + ", " + isUseCache + ", " + isAddCache + ", " + fromWriteDB + ")\n"+uuid + "连接名:" + (fromWriteDB ? writeConnectionName : readConnectionName) + "\n"+uuid + "准备执行SQL:" + pagePql.toString() ); } int totalPages = 0; int totalRows = JdbcUtil.queryForInt(uuid, fromWriteDB ? writeConnectionName : readConnectionName, sqlCount.toString(), count_list.toArray()) ; if (totalRows % pageSize == 0) { totalPages = totalRows / pageSize; } else { totalPages = (totalRows / pageSize) + 1; } Map resultMap = new HashMap(); resultMap.put("rows", totalRows); resultMap.put("page", page); resultMap.put("records", totalRows); resultMap.put("total", totalPages); resultList = JdbcUtil.executeQuery(uuid, fromWriteDB ? writeConnectionName : readConnectionName, pageSql.toString(), new DAORowMapper<${beanName}PO>(${beanName}PO.class), page_list.toArray()); resultMap.put("list", resultList); if (isAddCache) {// 把结果添加到缓存 if (oColumAndTimeKey == null && !isUseCache) oColumAndTimeKey = getCacheColFromCache(condition); if (oColumAndTimeKey != null) { if (oColumAndTimeKey[1] == null) { oColumAndTimeKey[1] = System.currentTimeMillis(); Memcached.add(2, "2_${tableName}_" + oColumAndTimeKey[0], oColumAndTimeKey[1]); Log.info("cacheinfo:[UUID:"+uuid+"]", logger,"二级缓存重置 key[2_${tableName}_" + oColumAndTimeKey[0] + "]"); } Memcached.add(3, "3_" + MD5.md5(pagePql.toString() + oColumAndTimeKey[1]), resultMap); Log.info("cacheinfo:[UUID:"+uuid+"]", logger,"三级缓存添加 key[3_" + MD5.md5(pagePql.toString() + oColumAndTimeKey[1]) + "]"); } } return resultMap; } catch (Exception e) { Log.error("error:[UUID:"+uuid+"]", logger, e, pagePql.toString(), page_list.toArray()); } finally { if (isLogInfo()) { Date endtime = new Date(); Log.info("cacheinfo:[UUID:"+uuid+"]", logger, "${beanName}Dao.findPageByCondition(" + condition.toString() + ", " + page + ", " + pageSize + ", " + isUseCache + ", " + isAddCache + ", " + fromWriteDB + ")\n"+uuid + "连接名:" + (fromWriteDB ? writeConnectionName : readConnectionName) + "\n"+uuid + "执行SQL:" + pagePql.toString() + "\n"+uuid + "返回条数:" + (resultList==null?0:resultList.size()) + "\n"+uuid + "方法执行时长:" + (endtime.getTime()-starttime.getTime()) + "毫秒(" + StringUtil.dateToFormatStr(starttime, "yyyy-MM-dd HH:mm:ss.SSS") + "*-*" + StringUtil.dateToFormatStr(endtime, "yyyy-MM-dd HH:mm:ss.SSS") + ")"); } } return null; } /** * 按sql分页查询, sqlCondition为where 后条件sql */ public Map findPageBySql(String sqlCondition, int page, int pageSize, String sortCondition) { return findPageBySql(sqlCondition, page, pageSize, true, true, sortCondition, false); } /** * 按sql分页查询, sqlCondition为where 后条件sql */ public Map findPageBySql(String sqlCondition, int page, int pageSize, boolean isUseCache, boolean isAddCache, String sortCondition, boolean fromWriteDB) { if (StringUtil.isAutowired(sqlCondition)) { Log.error("error:[UUID:"+uuid+"]", logger, new Exception("请求条件异常,无效请求!"), null, null); return null; } Date starttime = null; if (isLogInfo()) { starttime = new Date(); } <#if dbType = "mysql"> StringBuffer sql = new StringBuffer("SELECT ${column_list_str} FROM ${tableName} WHERE " + sqlCondition + sortCondition); StringBuffer sqlCount = new StringBuffer("SELECT count(1) from ${tableName} WHERE " + sqlCondition); String pageSql = sql.toString() + " limit ?, ?"; String pagePql = sql.toString() + " limit " + ((page -1) * pageSize) + ", " + (page * pageSize); List page_list = new ArrayList(); page_list.add((page - 1) * pageSize); page_list.add(page * pageSize); <#else> StringBuffer sql = new StringBuffer("SELECT ${column_list_str}, ROWNUM RN FROM ${tableName} WHERE " + sqlCondition); StringBuffer sqlCount = new StringBuffer("SELECT count(1) from ${tableName} WHERE " + sqlCondition); String pageSql = null; String pagePql = null; if(StringUtil.isEmpty(sortCondition)){ pageSql = "SELECT * FROM (" + sql.toString() + " AND ROWNUM <= ?" + ") A WHERE A.RN >= ?"; pagePql = "SELECT * FROM (" + sql.toString() + " AND ROWNUM <= " + page * pageSize + ") A WHERE A.RN >= " + ((page -1) * pageSize + 1); }else{ pageSql = "SELECT * FROM (SELECT B.*,ROWNUM RNN FROM (" + sql.toString() + " " + sortCondition + ") B WHERE ROWNUM <= ?"+" ) A WHERE A.RNN >= ?"; pagePql = "SELECT * FROM (SELECT B.*,ROWNUM RNN FROM (" + sql.toString() + " " + sortCondition + ") B WHERE ROWNUM <= " + page * pageSize + ") A WHERE A.RNN >= " + ((page -1) * pageSize + 1); } List page_list = new ArrayList(); page_list.add(page * pageSize); page_list.add((page - 1) * pageSize + 1); Object oTime = null; if (isUseCache) {// 从缓存中查询 oTime = Memcached.get("4_${tableName}"); Log.info("cacheinfo:[UUID:"+uuid+"]", logger,"四级缓存中获取 key[4_${tableName}],value[" + oTime + "]"); if (null != oTime) { Object object = Memcached.get("3_" + MD5.md5(pagePql.toString() + oTime.toString())); if (null != object) { if (isLogInfo()) { Date endtime = new Date(); Log.info("cacheinfo:[UUID:"+uuid+"]", logger, "${beanName}Dao.findPageBySql(" + sqlCondition + ", " + page + ", " + pageSize + ", " + isUseCache + ", " + isAddCache + ", " + fromWriteDB + ")\n" + "四级三级缓存命中 key[3_" + MD5.md5(pagePql.toString() + oTime.toString()) + "]\n"+uuid + "三级缓存返回条数:" + (((Map) object).get("list") == null?0:((List<${beanName}PO>)((Map) object).get("list")).size()) + "\n"+uuid + "三级缓存查询时长:" + (endtime.getTime()-starttime.getTime()) + "毫秒(" + StringUtil.dateToFormatStr(starttime, "yyyy-MM-dd HH:mm:ss.SSS") + "*-*" + StringUtil.dateToFormatStr(endtime, "yyyy-MM-dd HH:mm:ss.SSS") + ")"); } return (Map) object; } } } List<${beanName}PO> resultList = null; try { if (isLogInfo()) { Log.info("cacheinfo:[UUID:"+uuid+"]", logger, "${beanName}Dao.findPageBySql(" + sqlCondition + ", " + page + ", " + pageSize + ", " + isUseCache + ", " + isAddCache + ", " + fromWriteDB + ")\n" + "连接名:" + (fromWriteDB ? writeConnectionName : readConnectionName) + "\n"+uuid + "准备执行SQL:" + pagePql.toString() ); } int totalPages = 0; int totalRows = JdbcUtil.queryForInt(uuid, fromWriteDB ? writeConnectionName : readConnectionName, sqlCount.toString()); if (totalRows % pageSize == 0) { totalPages = totalRows / pageSize; } else { totalPages = (totalRows / pageSize) + 1; } Map resultMap = new HashMap(); resultMap.put("rows", totalRows); resultMap.put("page", page); resultMap.put("records", totalRows); resultMap.put("total", totalPages); resultList = JdbcUtil.executeQuery(uuid, fromWriteDB ? writeConnectionName : readConnectionName, pageSql.toString(), new DAORowMapper<${beanName}PO>(${beanName}PO.class), page_list.toArray()); resultMap.put("list", resultList); if (isAddCache) {// 重置缓存 long keyTime = 0; if (oTime != null) { keyTime = (Long) oTime; } else { keyTime = System.currentTimeMillis(); Memcached.add(4, "4_${tableName}", keyTime); Log.info("cacheinfo:[UUID:"+uuid+"]", logger,"四级缓存重置 key[4_${tableName}]"); } Memcached.add(3, "3_" + MD5.md5(pagePql.toString() + keyTime), resultMap); Log.info("cacheinfo:[UUID:"+uuid+"]", logger,"三级缓存添加 key[3_" + MD5.md5(pagePql.toString() + keyTime) + "]"); } return resultMap; } catch (Exception e) { Log.error("error:[UUID:"+uuid+"]", logger, e, pagePql.toString(), null); } finally { if (isLogInfo()) { Date endtime = new Date(); Log.info("cacheinfo:[UUID:"+uuid+"]", logger, "${beanName}Dao.findPageBySql(" + sqlCondition + ", " + page + ", " + pageSize + ", " + isUseCache + ", " + isAddCache + ", " + fromWriteDB + ")\n"+uuid + "连接名:" + (fromWriteDB ? writeConnectionName : readConnectionName) + "\n"+uuid + "执行SQL:" + pagePql.toString() + "\n"+uuid + "返回条数:" + (resultList==null?0:resultList.size()) + "\n"+uuid + "方法执行时长:" + (endtime.getTime()-starttime.getTime()) + "毫秒(" + StringUtil.dateToFormatStr(starttime, "yyyy-MM-dd HH:mm:ss.SSS") + "*-*" + StringUtil.dateToFormatStr(endtime, "yyyy-MM-dd HH:mm:ss.SSS") + ")"); } } return null; } /** *添加 */ public int directAdd${beanName}PO(${beanName}PO ${beanName}) { if (null == ${beanName}) { Log.error("error:[UUID:"+uuid+"]", logger, new Exception("请求条件异常,${beanName}不能为空!"), null, null); return -1; } Date starttime = null; if (isLogInfo()) { starttime = new Date(); } <#-- if (${beanName}.get${pkBeanName}() == null) { ${beanName}.set${pkBeanName}(this.find${beanName}POSeqNextVal()); } --> <#if columnList?exists> <#list columnList as clist> <#if clist.seqName?exists> if (${beanName}.get${pkBeanName}() == null) { ${beanName}.set${clist.columnUName}(this.findSeqNextVal("${clist.seqName}")); } StringBuffer sql = new StringBuffer("INSERT INTO ${tableName}(" + "${column_list_str}" + ") VALUES ("); StringBuffer pql = new StringBuffer(sql.toString()); List list = new ArrayList(); <#if columnList?exists> <#list columnList as clist> sql.append(" ?, "); pql.append(${beanName}.get${clist.columnUName}() + ", "); list.add(${beanName}.get${clist.columnUName}()); int count = 0; try { if (isLogInfo()) { Log.info("cacheinfo:[UUID:"+uuid+"]", logger, "${beanName}Dao.directAdd${beanName}PO (${beanName}PO ${beanName})\n"+uuid + "连接名:" + writeConnectionName + "\n"+uuid + "准备执行SQL:" + StringUtil.removeLast(pql.toString()) + ")" ); } count = JdbcUtil.executeUpdate(uuid, writeConnectionName, StringUtil.removeLast(sql.toString()) + ")", list.toArray()); return count; } catch (Exception e) { Log.error("error:[UUID:"+uuid+"]", logger, e, StringUtil.removeLast(pql.toString()) + ")", null); } finally { if (isLogInfo()) { Date endtime = new Date(); Log.info("cacheinfo:[UUID:"+uuid+"]", logger, "${beanName}Dao.directAdd${beanName}PO (${beanName}PO ${beanName})\n"+uuid + "连接名:" + writeConnectionName + "\n"+uuid + "执行SQL:" + StringUtil.removeLast(pql.toString()) + ")" + "\n"+uuid + "方法执行时长:" + (endtime.getTime()-starttime.getTime()) + "毫秒, 影响记录数:" + count + "(" + StringUtil.dateToFormatStr(starttime, "yyyy-MM-dd HH:mm:ss.SSS") + "*-*" + StringUtil.dateToFormatStr(endtime, "yyyy-MM-dd HH:mm:ss.SSS") + ")"); } } return -1; } /** * 添加 */ public int add${beanName}PO(${beanName}PO ${beanName}) { if (null == ${beanName}) { Log.error("error:[UUID:"+uuid+"]", logger, new Exception("请求条件异常,${beanName}不能为空!"), null, null); return -1; } Date starttime = null; if (isLogInfo()) { starttime = new Date(); } <#-- if (${beanName}.get${pkBeanName}() == null) { ${beanName}.set${pkBeanName}(this.find${beanName}POSeqNextVal()); } --> <#if columnList?exists> <#list columnList as clist> <#if clist.seqName?exists> if (${beanName}.get${pkBeanName}() == null) { ${beanName}.set${clist.columnUName}(this.findSeqNextVal("${clist.seqName}")); } StringBuffer sql = new StringBuffer("INSERT INTO ${tableName}(" + "${column_list_str}" + ") VALUES ("); StringBuffer sqlI = new StringBuffer("INSERT INTO ${tableName}("); StringBuffer sqlC = new StringBuffer(") VALUES ("); StringBuffer pql = new StringBuffer(sql.toString()); List list = new ArrayList(); <#if columnList?exists> <#list columnList as clist> if(${beanName}.get${clist.columnUName}() != null){ sqlI.append("${clist.columnName},"); sqlC.append(" ?, "); pql.append(${beanName}.get${clist.columnUName}() + ", "); list.add(${beanName}.get${clist.columnUName}()); } int count = 0; try { if (isLogInfo()) { Log.info("cacheinfo:[UUID:"+uuid+"]", logger, "${beanName}Dao.add${beanName}PO (${beanName}PO ${beanName})\n"+uuid + "连接名:" + writeConnectionName + "\n"+uuid + "准备执行SQL:" + StringUtil.removeLast(pql.toString()) + ")" ); } count = JdbcUtil.executeUpdate(uuid, writeConnectionName, StringUtil.removeLast(StringUtil.removeLast(sqlI.toString())+sqlC.toString()) + ")", list.toArray()); if (count > 0) { long keyTime = System.currentTimeMillis(); for (int i = 0; i < cacheColums.size(); i++) { String key = cacheColums.get(i); String cache_key = "2_${tableName}_" + key + "_" + StringUtil.getValueFromObjectByName(${beanName}, key); Memcached.add(2, cache_key, keyTime); Log.info("cacheinfo:[UUID:"+uuid+"]", logger,"二级缓存重置 key[" + cache_key + "]"); } Memcached.add(4, "4_${tableName}", keyTime); Log.info("cacheinfo:[UUID:"+uuid+"]", logger,"四级缓存重置 key[4_${tableName}]"); } return count; } catch (Exception e) { Log.error("error:[UUID:"+uuid+"]", logger, e, StringUtil.removeLast(pql.toString()) + ")", null); } finally { if (isLogInfo()) { Date endtime = new Date(); Log.info("cacheinfo:[UUID:"+uuid+"]", logger, "${beanName}Dao.add${beanName}PO (${beanName}PO ${beanName})\n"+uuid + "连接名:" + writeConnectionName + "\n"+uuid + "执行SQL:" + StringUtil.removeLast(pql.toString()) + ")" + "\n"+uuid + "方法执行时长:" + (endtime.getTime()-starttime.getTime()) + "毫秒, 影响记录数:" + count + "(" + StringUtil.dateToFormatStr(starttime, "yyyy-MM-dd HH:mm:ss.SSS") + "*-*" + StringUtil.dateToFormatStr(endtime, "yyyy-MM-dd HH:mm:ss.SSS") + ")"); } } return -1; } /** * 批量添加 */ public int add${beanName}POList(List<${beanName}PO> ${beanName}list) { Date starttime = null; if (isLogInfo()) { starttime = new Date(); } int count = 0; for (${beanName}PO ${beanName} : ${beanName}list) { count += add${beanName}PO(${beanName}); } if (isLogInfo()) { Date endtime = new Date(); Log.info("cacheinfo:[UUID:"+uuid+"]", logger, "${beanName}Dao.add${beanName}POList(List<${beanName}PO> ${beanName}list)\n"+uuid + "方法执行时长:" + (endtime.getTime()-starttime.getTime()) + "毫秒, 影响记录数:" + count + "(" + StringUtil.dateToFormatStr(starttime, "yyyy-MM-dd HH:mm:ss.SSS") + "*-*" + StringUtil.dateToFormatStr(endtime, "yyyy-MM-dd HH:mm:ss.SSS") + ")"); } return count; } /** * 按ID删除 */ public int remove${beanName}POByID(${pkType} id) { Date starttime = null; if (isLogInfo()) { starttime = new Date(); } StringBuffer sql = new StringBuffer("DELETE FROM ${tableName} WHERE"); StringBuffer pql = new StringBuffer(sql.toString()); pql.append(" ${pkColumName} = " + id); sql.append(" ${pkColumName} = ?"); int count = 0; try { ${beanName}PO oldPO = find${beanName}POByID(id, false, true, false, false); if (isLogInfo()) { Log.info("cacheinfo:[UUID:"+uuid+"]", logger, "${beanName}Dao.remove${beanName}POByID(${pkType} id)\n"+uuid + "连接名:" + writeConnectionName + "\n"+uuid + "准备执行SQL:" + pql.toString() ); } count = JdbcUtil.executeUpdate(uuid, writeConnectionName, sql.toString(), id); if (count > 0) { long keyTime = System.currentTimeMillis(); Memcached.delete("1_${tableName}_PK_" + id); Log.info("cacheinfo:[UUID:"+uuid+"]", logger,"一级缓存删除 key[1_${tableName}_PK_" + id + "]"); for (int i = 0; i < cacheColums.size(); i++) { String key = cacheColums.get(i); String cache_key = "2_${tableName}_" + key + "_" + StringUtil.getValueFromObjectByName(oldPO, key); Memcached.add(2, cache_key, keyTime); Log.info("cacheinfo:[UUID:"+uuid+"]", logger,"二级缓存重置 key[" + cache_key + "]"); } Memcached.add(4, "4_${tableName}", keyTime); Log.info("cacheinfo:[UUID:"+uuid+"]", logger,"四级缓存重置 key[4_${tableName}]"); } return count; } catch (Exception e) { Log.error("error:[UUID:"+uuid+"]", logger, e, pql.toString(), null); } finally { if (isLogInfo()) { Date endtime = new Date(); Log.info("cacheinfo:[UUID:"+uuid+"]", logger, "${beanName}Dao.remove${beanName}POByID(${pkType} id)\n"+uuid + "连接名:" + writeConnectionName + "\n"+uuid + "执行SQL:" + pql.toString() + "\n"+uuid + "方法执行时长:" + (endtime.getTime()-starttime.getTime()) + "毫秒, 影响记录数:" + count + "(" + StringUtil.dateToFormatStr(starttime, "yyyy-MM-dd HH:mm:ss.SSS") + "*-*" + StringUtil.dateToFormatStr(endtime, "yyyy-MM-dd HH:mm:ss.SSS") + ")"); } } return -1; } /** *根据ID删除,不走缓存 */ public int directRemove${beanName}POByID(${pkType} id) { Date starttime = null; if (isLogInfo()) { starttime = new Date(); } StringBuffer sql = new StringBuffer("DELETE FROM ${tableName} WHERE"); StringBuffer pql = new StringBuffer(sql.toString()); pql.append(" ${pkColumName} = " + id); sql.append(" ${pkColumName} = ?"); int count = 0; try { count = JdbcUtil.executeUpdate(uuid, writeConnectionName, sql.toString(), id); return count; } catch (Exception e) { Log.error("error:[UUID:"+uuid+"]", logger, e, pql.toString(), null); } finally { if (isLogInfo()) { Date endtime = new Date(); Log.info("cacheinfo:[UUID:"+uuid+"]", logger, "${beanName}Dao.directRemove${beanName}POByID(${pkType} id)\n"+uuid + "连接名:" + writeConnectionName + "\n"+uuid + "执行SQL:" + pql.toString() + "\n"+uuid + "方法执行时长:" + (endtime.getTime()-starttime.getTime()) + "毫秒, 影响记录数:" + count + "(" + StringUtil.dateToFormatStr(starttime, "yyyy-MM-dd HH:mm:ss.SSS") + "*-*" + StringUtil.dateToFormatStr(endtime, "yyyy-MM-dd HH:mm:ss.SSS") + ")"); } } return -1; } /** * 删除按List条件 * Object[]数组长度是3 * Object[], 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 */ public int remove${beanName}POByCondition(List condition) { if (null == condition || condition.size() == 0) { Log.error("error:[UUID:"+uuid+"]", logger, new Exception("请求条件异常,请求条件List condition不能为空!"), null, null); return -1; } if (StringUtil.isAutowired(condition)) { Log.error("error:[UUID:"+uuid+"]", logger, new Exception("请求条件异常,无效请求!"), null, null); return -1; } Date starttime = null; if (isLogInfo()) { starttime = new Date(); } List list = new ArrayList(); StringBuffer sql = new StringBuffer("DELETE FROM ${tableName}"); StringBuffer pql = new StringBuffer(sql.toString()); JdbcUtil.appendWhereCondition(sql, pql, list, condition); int count = 0; try { List<${beanName}PO> oldList = find${beanName}POListByCondition(condition, true, false, false); if (isLogInfo()) { Log.info("cacheinfo:[UUID:"+uuid+"]", logger, "${beanName}Dao.remove${beanName}POByCondition(List condition)\n"+uuid + "连接名:" + writeConnectionName + "\n"+uuid + "准备执行SQL:" + pql.toString() ); } count = JdbcUtil.executeUpdate(uuid, writeConnectionName, sql.toString(), list.toArray()); if (count > 0) {// 重置缓存 resetCache(oldList); } return count; } catch (Exception e) { Log.error("error:[UUID:"+uuid+"]", logger, e, pql.toString(), null); } finally { if (isLogInfo()) { Date endtime = new Date(); Log.info("cacheinfo:[UUID:"+uuid+"]", logger, "${beanName}Dao.remove${beanName}POByCondition(List condition)\n" + "连接名:" + writeConnectionName + "\n" + "执行SQL:" + pql.toString() + "\n" + "方法执行时长:" + (endtime.getTime()-starttime.getTime()) + "毫秒, 影响记录数:" + count + "(" + StringUtil.dateToFormatStr(starttime, "yyyy-MM-dd HH:mm:ss.SSS") + "*-*" + StringUtil.dateToFormatStr(endtime, "yyyy-MM-dd HH:mm:ss.SSS") + ")"); } } return -1; } /** * 删除按condition条件 * 建议使用remove${beanName}POByCondition(List condition), 如果remove${beanName}POByCondition(List condition)满足不了where条件可以使用此方法。 * condition为where后面的条件,condition不能为空。 */ public int remove${beanName}POBySql(String sqlCondition) { if (StringUtil.isEmpty(sqlCondition)) { Log.error("error:[UUID:"+uuid+"]", logger, new Exception("请求条件异常,请求条件String sqlCondition不能为空!"), null, null); return -1; } if (StringUtil.isAutowired(sqlCondition)) { Log.error("error:[UUID:"+uuid+"]", logger, new Exception("请求条件异常,无效请求!"), null, null); return -1; } Date starttime = null; if (isLogInfo()) { starttime = new Date(); } StringBuffer sql = new StringBuffer("DELETE FROM ${tableName} WHERE "); int count = 0; try { List<${beanName}PO> oldList = find${beanName}POListBySql(sqlCondition, true, false, false); if (isLogInfo()) { Log.info("cacheinfo:[UUID:"+uuid+"]", logger, "${beanName}Dao.remove${beanName}POBySql(String sqlCondition)\n"+uuid + "连接名:" + writeConnectionName + "\n"+uuid + "准备执行SQL:" + sql.toString() + sqlCondition ); } count = JdbcUtil.executeUpdate(uuid, writeConnectionName, sql.toString() + sqlCondition); if (count > 0) {// 重置缓存 resetCache(oldList); } return count; } catch (Exception e) { Log.error("error:[UUID:"+uuid+"]", logger, e, sql.toString() + sqlCondition, null); } finally { if (isLogInfo()) { Date endtime = new Date(); Log.info("cacheinfo:[UUID:"+uuid+"]", logger, "${beanName}Dao.remove${beanName}POBySql(String sqlCondition)\n"+uuid + "连接名:" + writeConnectionName + "\n"+uuid + "执行SQL:" + sql.toString() + sqlCondition + "\n"+uuid + "方法执行时长:" + (endtime.getTime()-starttime.getTime()) + "毫秒, 影响记录数:" + count + "(" + StringUtil.dateToFormatStr(starttime, "yyyy-MM-dd HH:mm:ss.SSS") + "*-*" + StringUtil.dateToFormatStr(endtime, "yyyy-MM-dd HH:mm:ss.SSS") + ")"); } } return -1; } /** * 根据list对象逐个删除。 */ public int remove${beanName}POList(List<${beanName}PO> ${beanName}list) { Date starttime = null; if (isLogInfo()) { starttime = new Date(); } int count = 0; for (${beanName}PO ${beanName} : ${beanName}list) { count += remove${beanName}POByID(${beanName}.get${pkBeanName}()); } if (isLogInfo()) { Date endtime = new Date(); Log.info("cacheinfo:[UUID:"+uuid+"]", logger, "${beanName}Dao.remove${beanName}POList(List<${beanName}PO> ${beanName}list)\n"+uuid + "方法执行时长:" + (endtime.getTime()-starttime.getTime()) + "毫秒, 影响记录数:" + count + "(" + StringUtil.dateToFormatStr(starttime, "yyyy-MM-dd HH:mm:ss.SSS") + "*-*" + StringUtil.dateToFormatStr(endtime, "yyyy-MM-dd HH:mm:ss.SSS") + ")"); } return count; } /** * 根据对象ID修改。 */ public int update${beanName}PO(${beanName}PO ${beanName}) { if (null == ${beanName} || null == ${beanName}.get${pkBeanName}()) { Log.error("error:[UUID:"+uuid+"]", logger, new Exception("请求条件异常,请求对象和对象ID不能为空!"), null, null); return -1; } Date starttime = null; if (isLogInfo()) { starttime = new Date(); } StringBuffer sql = new StringBuffer("UPDATE ${tableName} SET"); StringBuffer pql = new StringBuffer(sql.toString()); List list = new ArrayList(); <#if columnList?exists> <#list columnList as clist> <#if clist.seqName?exists> <#else> <#if clist.columnJAVAType = "Long" || clist.columnJAVAType = "Integer" || clist.columnJAVAType = "Short" || clist.columnJAVAType = "Float" || clist.columnJAVAType = "Double"> boolean ${clist.columnUName}IsZero = false; Object variation${clist.columnUName} = ${beanName}.getVariation${clist.columnUName}(); if (variation${clist.columnUName} instanceof BigDecimal) { ${clist.columnUName}IsZero = (((BigDecimal)variation${clist.columnUName}).compareTo(BigDecimal.ZERO) == 0); } else { Long l${clist.columnUName} = Long.parseLong(String.valueOf(variation${clist.columnUName})); ${clist.columnUName}IsZero = (l${clist.columnUName} == 0); } if (!${clist.columnUName}IsZero) { sql.append(" ${clist.columnName} = ${clist.columnName} + ?, "); pql.append(" ${clist.columnName} = ${clist.columnName} + " + ${beanName}.getVariation${clist.columnUName}() + ","); list.add(${beanName}.getVariation${clist.columnUName}()); } else { sql.append(" ${clist.columnName} = ?, "); pql.append(" ${clist.columnName} = " + ${beanName}.get${clist.columnUName}() + ","); list.add(${beanName}.get${clist.columnUName}()); } <#elseif clist.columnJAVAType = "java.math.BigDecimal"> if (${beanName}.getVariation${clist.columnUName}().compareTo(BigDecimal.ZERO) != 0) { sql.append(" ${clist.columnName} = ${clist.columnName} + ?, "); pql.append(" ${clist.columnName} = ${clist.columnName} + " + ${beanName}.getVariation${clist.columnUName}() + ","); list.add(${beanName}.getVariation${clist.columnUName}()); } else { sql.append(" ${clist.columnName} = ?, "); pql.append(" ${clist.columnName} = " + ${beanName}.get${clist.columnUName}() + ","); list.add(${beanName}.get${clist.columnUName}()); } <#else> sql.append(" ${clist.columnName} = ?, "); pql.append(" ${clist.columnName} = " + ${beanName}.get${clist.columnUName}() + ","); list.add(${beanName}.get${clist.columnUName}()); String where = " WHERE ${pkColumName} = ?"; String pwhere = " WHERE ${pkColumName} = " + ${beanName}.get${pkBeanName}(); list.add(${beanName}.get${pkBeanName}()); int count = 0; try { List<${beanName}PO> oldList = find${beanName}POListBySql("${pkColumName} = " + ${beanName}.get${pkBeanName}(), true, false, false); if (isLogInfo()) { Log.info("cacheinfo:[UUID:"+uuid+"]", logger, "${beanName}Dao.update${beanName}PO(${beanName}PO ${beanName})\n"+uuid + "连接名:" + writeConnectionName + "\n"+uuid + "准备执行SQL:" + StringUtil.removeLast(pql.toString()) + pwhere ); } count = JdbcUtil.executeUpdate(uuid, writeConnectionName, StringUtil.removeLast(sql.toString()) + where, list.toArray()); if (count > 0) { long keyTime = System.currentTimeMillis(); //重置旧数据缓存 for (${beanName}PO po : oldList) { for (int i = 0; i < cacheColums.size(); i++) { String key = (String) cacheColums.get(i); String cache_key = "2_${tableName}_" + key + "_" + StringUtil.getValueFromObjectByName(po, key); Memcached.add(2, cache_key, keyTime); Log.info("cacheinfo:[UUID:"+uuid+"]", logger,"二级缓存重置 key[" + cache_key + "]"); } } //重置新数据缓存 Memcached.add(1, "1_${tableName}_PK_" + ${beanName}.get${pkBeanName}(), ${beanName}); Log.info("cacheinfo:[UUID:"+uuid+"]", logger,"一级缓存添加 key[1_${tableName}_PK_" + ${beanName}.get${pkBeanName}() + "]"); for (int i = 0; i < cacheColums.size(); i++) { String key = cacheColums.get(i); String cache_key = "2_${tableName}_" + key + "_" + StringUtil.getValueFromObjectByName(${beanName}, key); Memcached.add(2, cache_key, keyTime); Log.info("cacheinfo:[UUID:"+uuid+"]", logger,"二级缓存重置 key[" + cache_key + "]"); } Memcached.add(4, "4_${tableName}", keyTime); Log.info("cacheinfo:[UUID:"+uuid+"]", logger,"四级缓存重置 key[4_${tableName}]"); } return count; } catch (Exception e) { Log.error("error:[UUID:"+uuid+"]", logger, e, StringUtil.removeLast(pql.toString()) + pwhere, null); } finally { if (isLogInfo()) { Date endtime = new Date(); Log.info("cacheinfo:[UUID:"+uuid+"]", logger, "${beanName}Dao.update${beanName}PO(${beanName}PO ${beanName})\n"+uuid + "连接名:" + writeConnectionName + "\n"+uuid + "执行SQL:" + StringUtil.removeLast(pql.toString()) + pwhere + "\n"+uuid + "方法执行时长:" + (endtime.getTime()-starttime.getTime()) + "毫秒, 影响记录数:" + count + "(" + StringUtil.dateToFormatStr(starttime, "yyyy-MM-dd HH:mm:ss.SSS") + "*-*" + StringUtil.dateToFormatStr(endtime, "yyyy-MM-dd HH:mm:ss.SSS") + ")"); } } return -1; } /** * 根据ID修改指定的值 */ public int update${beanName}FieldById(List changeList, ${pkType} id) { if (null == id) { Log.error("error:[UUID:"+uuid+"]", logger, new Exception("请求条件异常,请求对象和对象ID不能为空!"), null, null); return -1; } Date starttime = null; if (isLogInfo()) { starttime = new Date(); } StringBuffer sql = new StringBuffer("UPDATE ${tableName} SET"); StringBuffer pql = new StringBuffer(sql.toString()); List list = new ArrayList(); Map map = new HashMap(); for (int i = 0, iLen = changeList.size(); i < iLen; i++) { String name = (String) changeList.get(i)[0]; Object value = changeList.get(i)[1]; String variationOpt = "="; if (changeList.get(i).length > 2) variationOpt = (String)changeList.get(i)[2]; if (variationOpt.equals("=")) { sql.append(" " + name + " = ?,"); list.add(value); if (value == null) { pql.append(" " + name + "=null,"); } else { pql.append(" " + name + "=\"" + value.toString() + "\","); } map.put(name, value); } else { sql.append(" " + name + " = " + name + " + ?,"); list.add(value); pql.append(" " + name + " = " + name + " + " + value.toString() + ","); <#--会有问题 暂留 map.put(name, value);--> } } String where = " WHERE ${pkColumName}=?"; String pwhere = " WHERE ${pkColumName}=\"" + id + "\""; list.add(id); int count = 0; try { List<${beanName}PO> oldList = find${beanName}POListBySql("${pkColumName} = " + id, true, false, false); if (isLogInfo()) { Log.info("cacheinfo:[UUID:"+uuid+"]", logger, "${beanName}Dao.update${beanName}PO(List changeList, ${pkType} id)\n"+uuid + "连接名:" + writeConnectionName + "\n"+uuid + "准备执行SQL:" + StringUtil.removeLast(pql.toString()) + pwhere ); } count = JdbcUtil.executeUpdate(uuid, writeConnectionName, StringUtil.removeLast(sql.toString()) + where, list.toArray()); if (count > 0) { resetCache(oldList);// 重置缓存 //重置新数据缓存 long keyTime = System.currentTimeMillis(); Memcached.delete("1_${tableName}_PK_" + id); Log.info("cacheinfo:[UUID:"+uuid+"]", logger,"一级缓存删除 key[1_${tableName}_PK_" + id + "]"); for (int i = 0; i < cacheColums.size(); i++) { String key = cacheColums.get(i); if(map.containsKey(key)){ String cache_key = "2_${tableName}_" + key + "_" + map.get(key); Memcached.add(2, cache_key, keyTime); Log.info("cacheinfo:[UUID:"+uuid+"]", logger,"二级缓存重置 key[" + cache_key + "]"); } } Memcached.add(4, "4_${tableName}", keyTime); Log.info("cacheinfo:[UUID:"+uuid+"]", logger,"四级缓存重置 key[4_${tableName}]"); } return count; } catch (Exception e) { Log.error("error:[UUID:"+uuid+"]", logger, e, StringUtil.removeLast(pql.toString()) + pwhere, null); } finally { if (isLogInfo()) { Date endtime = new Date(); Log.info("cacheinfo:[UUID:"+uuid+"]", logger, "${beanName}Dao.update${beanName}PO(List changeList, ${pkType} id)\n"+uuid + "连接名:" + writeConnectionName + "\n"+uuid + "执行SQL:" + StringUtil.removeLast(pql.toString()) + pwhere + "\n"+uuid + "方法执行时长:" + (endtime.getTime()-starttime.getTime()) + "毫秒, 影响记录数:" + count + "(" + StringUtil.dateToFormatStr(starttime, "yyyy-MM-dd HH:mm:ss.SSS") + "*-*" + StringUtil.dateToFormatStr(endtime, "yyyy-MM-dd HH:mm:ss.SSS") + ")"); } } return -1; } /** * 批量修改。 */ public int update${beanName}POList(List<${beanName}PO> ${beanName}list) { Date starttime = null; if (isLogInfo()) { starttime = new Date(); } int count = 0; for (${beanName}PO ${beanName} : ${beanName}list) { count += update${beanName}PO(${beanName}); } if (isLogInfo()) { Date endtime = new Date(); Log.info("cacheinfo:[UUID:"+uuid+"]", logger, "${beanName}Dao.update${beanName}POList(List<${beanName}PO> ${beanName}list)\n" + "方法执行时长:" + (endtime.getTime()-starttime.getTime()) + "毫秒, 影响记录数:" + count + "(" + StringUtil.dateToFormatStr(starttime, "yyyy-MM-dd HH:mm:ss.SSS") + "*-*" + StringUtil.dateToFormatStr(endtime, "yyyy-MM-dd HH:mm:ss.SSS") + ")"); } return count; } /** * List updateObj 要修改成的值,数组长度为2,第一个值为列名,第二个值是要改成的值。 * List condition 修改的条件, 数组长度是3, 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 */ public int update${beanName}POByCondition(List updateObj, List condition) { if (null == updateObj || updateObj.size() == 0) { Log.error("error:[UUID:"+uuid+"]", logger, new Exception("请求条件异常,请求条件List updateObj不能为空!"), null, null); return -1; } if (null == condition || condition.size() == 0) { Log.error("error:[UUID:"+uuid+"]", logger, new Exception("请求条件异常,请求条件List condition不能为空!"), null, null); return -1; } if (StringUtil.isAutowired(condition)) { Log.error("error:[UUID:"+uuid+"]", logger, new Exception("请求条件异常,无效请求!"), null, null); return -1; } Date starttime = null; if (isLogInfo()) { starttime = new Date(); } StringBuffer sql = new StringBuffer("UPDATE ${tableName} SET"); StringBuffer pql = new StringBuffer(sql.toString()); List list = new ArrayList(); JdbcUtil.appendSql(sql, pql, list, updateObj); StringBuffer where = new StringBuffer(""); StringBuffer pwhere = new StringBuffer(""); JdbcUtil.appendWhereCondition(where, pwhere, list, condition); int count = 0; try { List<${beanName}PO> oldList = find${beanName}POListByCondition(condition, true, false, false); if (isLogInfo()) { Log.info("cacheinfo:[UUID:"+uuid+"]", logger, "${beanName}Dao.update${beanName}POByCondition(List updateObj, List condition)\n"+uuid + "连接名:" + writeConnectionName + "\n"+uuid + "准备执行SQL:" + StringUtil.removeLast(pql.toString())+pwhere.toString() ); } count = JdbcUtil.executeUpdate(uuid, writeConnectionName, StringUtil.removeLast(sql.toString()) + where.toString(), list.toArray()); if (count > 0) { resetCache(oldList);// 重置缓存 //重置新数据缓存 Map map = new HashMap(); for(int i = 0; i < updateObj.size(); i++){ map.put(updateObj.get(i)[0], updateObj.get(i)[1]); } long keyTime = System.currentTimeMillis(); for (int i = 0; i < cacheColums.size(); i++) { String key = cacheColums.get(i); if(map.containsKey(key)){ String cache_key = "2_${tableName}_" + key + "_" + map.get(key); Memcached.add(2, cache_key, keyTime); Log.info("cacheinfo:[UUID:"+uuid+"]", logger,"二级缓存重置 key[" + cache_key + "]"); } } Memcached.add(4, "4_${tableName}", keyTime); Log.info("cacheinfo:[UUID:"+uuid+"]", logger,"四级缓存重置 key[4_${tableName}]"); } return count; } catch (Exception e) { Log.error("error:[UUID:"+uuid+"]", logger, e, StringUtil.removeLast(pql.toString())+pwhere.toString(), null, null); } finally { if (isLogInfo()) { Date endtime = new Date(); Log.info("cacheinfo:[UUID:"+uuid+"]", logger, "${beanName}Dao.update${beanName}POByCondition(List updateObj, List condition)\n" + "连接名:" + writeConnectionName + "\n"+uuid + "执行SQL:" + StringUtil.removeLast(pql.toString())+pwhere.toString() + "\n"+uuid + "方法执行时长:" + (endtime.getTime()-starttime.getTime()) + "毫秒, 影响记录数:" + count + "(" + StringUtil.dateToFormatStr(starttime, "yyyy-MM-dd HH:mm:ss.SSS") + "*-*" + StringUtil.dateToFormatStr(endtime, "yyyy-MM-dd HH:mm:ss.SSS") + ")"); } } return -1; } /** * List updateObj 要修改成的值,数组长度为2,第一个值为列名,第二个值是要改成的值。 * String sqlCondition 修改的条件。 */ public int update${beanName}POBySql(List updateObj, String sqlCondition) { if (null == updateObj || updateObj.size() == 0) { Log.error("error:[UUID:"+uuid+"]", logger, new Exception("请求条件异常,请求条件List updateObj不能为空!"), null, null); return -1; } if (StringUtil.isEmpty(sqlCondition)) { Log.error("error:[UUID:"+uuid+"]", logger, new Exception("请求条件异常,请求条件sqlCondition不能为空!"), null, null); return -1; } if (StringUtil.isAutowired(sqlCondition)) { Log.error("error:[UUID:"+uuid+"]", logger, new Exception("请求条件异常,无效请求!"), null, null); return -1; } Date starttime = null; if (isLogInfo()) { starttime = new Date(); } StringBuffer sql = new StringBuffer("UPDATE ${tableName} SET"); StringBuffer pql = new StringBuffer(sql.toString()); List list = new ArrayList(); JdbcUtil.appendSql(sql, pql, list, updateObj); int count = 0; try { List<${beanName}PO> oldList = find${beanName}POListBySql(sqlCondition, true, false, false); if (isLogInfo()) { Log.info("cacheinfo:[UUID:"+uuid+"]", logger, "${beanName}Dao.update${beanName}POBySql(List updateObj, String sqlCondition)\n"+uuid + "连接名:" + writeConnectionName + "\n"+uuid + "准备执行SQL:" + StringUtil.removeLast(pql.toString()) + " WHERE " + sqlCondition ); } count = JdbcUtil.executeUpdate(uuid, writeConnectionName, StringUtil.removeLast(sql.toString()) + " WHERE "+sqlCondition, list.toArray()); if (count > 0) { resetCache(oldList);// 重置缓存 //重置新数据缓存 Map map = new HashMap(); for(int i = 0; i < updateObj.size(); i++){ map.put(updateObj.get(i)[0], updateObj.get(i)[1]); } long keyTime = System.currentTimeMillis(); for (int i = 0; i < cacheColums.size(); i++) { String key = cacheColums.get(i); if(map.containsKey(key)){ String cache_key = "2_${tableName}_" + key + "_" + map.get(key); Memcached.add(2, cache_key, keyTime); Log.info("cacheinfo:[UUID:"+uuid+"]", logger,"二级缓存重置 key[" + cache_key + "]"); } } Memcached.add(4, "4_${tableName}", keyTime); Log.info("cacheinfo:[UUID:"+uuid+"]", logger,"四级缓存重置 key[4_${tableName}]"); } return count; } catch (Exception e) { Log.error("error:[UUID:"+uuid+"]", logger, e, StringUtil.removeLast(pql.toString()) + " WHERE " + sqlCondition, null, null); } finally { if (isLogInfo()) { Date endtime = new Date(); Log.info("cacheinfo:[UUID:"+uuid+"]", logger, "${beanName}Dao.update${beanName}POBySql(List updateObj, String sqlCondition)\n"+uuid + "连接名:" + writeConnectionName + "\n"+uuid + "执行SQL:" + StringUtil.removeLast(pql.toString()) + " WHERE " + sqlCondition + "\n"+uuid + "方法执行时长:" + (endtime.getTime()-starttime.getTime()) + "毫秒, 影响记录数:" + count + "(" + StringUtil.dateToFormatStr(starttime, "yyyy-MM-dd HH:mm:ss.SSS") + "*-*" + StringUtil.dateToFormatStr(endtime, "yyyy-MM-dd HH:mm:ss.SSS") + ")"); } } return -1; } <#-- /** * 获取自增长ID */ public Integer find${beanName}POSeqNextVal() { String sql = "SELECT SEQ_${tableName}_PK.NEXTVAL FROM DUAL"; try { return JdbcUtil.queryForInt(uuid, writeConnectionName, sql); } catch (Exception e) { Log.error("error:[UUID:"+uuid+"]", logger, e, sql.toString()); } return -1; } --> /** * 获取自增长列的值(仅支持数值类型) */ <#-- public ${pkType} --> public Long findSeqNextVal(String seq_name) { String sql = "SELECT " + seq_name + ".NEXTVAL FROM DUAL"; try { return Long.parseLong(JdbcUtil.queryForInt(uuid, writeConnectionName, sql)+""); } catch (Exception e) { Log.error("error:[UUID:"+uuid+"]", logger, e, sql.toString()); } return -1l; } /** * 获取多个自增长ID */ public List<${pkType}> find${beanName}POSeqNextVal(Integer step, String seqName) { String sql = "SELECT " + seqName + ".NEXTVAL FROM (SELECT 1 FROM ALL_OBJECTS WHERE ROWNUM <= "+step+" ) "; try { List<${pkType}> re = JdbcUtil.executeQuery(uuid, readConnectionName, sql.toString(), new DAORowMapper<${pkType}>(${pkType}.class)); return re; } catch (Exception e) { Log.error("error:[UUID:"+uuid+"]", logger, e, sql.toString()); } return null; } <#--get, set方法集合 --> <#if funlist?exists> <#list funlist as flist> <#--方法注释 --> <#if flist.function_param?exists> /** <#list flist.function_param as param> <#if param?exists>*@param ${param}<#else><#rt> <#list flist.function_exception as exception> <#if exception?exists>*@throws ${exception}<#else><#rt> <#if flist.function_description?exists>*@Description:${flist.function_description}<#else><#rt> */ <#--是否是override的 --> <#if flist.over?exists>@Override<#else><#rt> <#--类或者接口的方法不同所有有两个方法体 --> <#-- 方法的定义--> public ${flist.function_return} ${flist.function_name}(<#if flist.function_params?exists>${flist.function_params}<#else>)<#if flist.function_exceptions?exists> throws ${flist.function_exceptions}<#else> { <#--方法体--> ${flist.function_body} } }