//${tableName} <#--包名 --> package ${packages} <#-- 所有的引入--> <#list imports as ilist> <#if ilist?exists>${ilist}<#else><#rt> import java.math.BigDecimal; /** <#if version?exists> * @version ${version} */ @Repository <#--类, 还是接口 名称, 继承的类, 实现的接口 --> public class ${beanName}Dao { <#--变量 --> @Resource private JdbcTemplate jdbcegroceryRead; @Resource private JdbcTemplate jdbcegroceryWrite; Log logger = LogFactory.getLog(this.getClass()); /** * 根据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 findPOByID(${pkType} id) { return findPOByID(id, true, false); } /** * 按主键查询 * isLock 是否锁定, 默认不锁 * fromWriteDB 是否从写库读写,默认从读库查询 */ public ${beanName}PO findPOByID(${pkType} id, boolean isLock, boolean fromWriteDB) { Date starttime = new Date(); 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 { resultList = (fromWriteDB ? jdbcegroceryWrite : jdbcegroceryRead).query(sql.toString(), new DAORowMapper<${beanName}PO>(${beanName}PO.class), id); if (resultList == null || resultList.size() == 0) { return null; } return resultList.get(0); } catch (Exception e) { LogUtils.error("error:findPOByID", logger, e, pql.toString(), id); } finally { Date endtime = new Date(); if(false)LogUtils.info("info:", logger, "${beanName}Dao.findPOByID(" + id + ", " + isLock + ", " + ", " + fromWriteDB + ")\n" + "返回数据条数:" + (resultList==null?0:resultList.size()) + "方法执行时长:" + (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 ${beanName}PO findPOByCondition(List condition) { List<${beanName}PO> tempList = findListByCondition(condition,null, false); if(tempList == null || tempList.size() == 0){ return null; }else{ return tempList.get(0); } } /** * 根据条件sql查询 * sqlCondition 为where 后面的条件。 */ public ${beanName}PO findPOBySql(String sqlCondition) { List<${beanName}PO> tempList = findListBySql(sqlCondition, false); if(tempList == null || tempList.size() == 0){ return null; }else{ return tempList.get(0); } } /** * 根据条件List查询 * Object[]数组长度是3 * Object[], 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 */ public List<${beanName}PO> findListByCondition(List condition) { return findListByCondition(condition,null, false); } /** * 根据条件List查询 * Object[]数组长度是3 * Object[], 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 */ public List<${beanName}PO> findListByCondition(List condition,String sortCondition, boolean fromWriteDB) { if (StringUtil.isAutowired(condition)) { LogUtils.error("error:", logger, new Exception("请求条件异常,无效请求!"), null, null); return null; } Date 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); if(!StringUtils.isEmpty(sortCondition)){ sql.append(" " + sortCondition + " "); pql.append(" " + sortCondition + " "); } List<${beanName}PO> resultList = null; try { resultList = (fromWriteDB ? jdbcegroceryWrite : jdbcegroceryRead).query(sql.toString(), new DAORowMapper<${beanName}PO>(${beanName}PO.class), list.toArray()); return resultList; } catch (Exception e) { LogUtils.error("error:", logger, e, pql.toString(), null); } finally { Date endtime = new Date(); if(false)LogUtils.info("info", logger, "${beanName}Dao.findListByCondition(" + condition.toString() + ", " + fromWriteDB + ")\n" + "fromWriteDB:" + fromWriteDB + "执行SQL:" + pql.toString() + "\n" + "返回条数:" + (resultList==null?0:resultList.size()) + "\n" + "方法执行时长:" + (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> findListBySql(String sqlCondition) { return findListBySql(sqlCondition, false); } /** * 根据条件sql查询 * sqlCondition 为where 后面的条件。 */ public List<${beanName}PO> findListBySql(String sqlCondition,boolean fromWriteDB) { if (StringUtil.isAutowired(sqlCondition)) { LogUtils.error("error", logger, new Exception("请求条件异常,无效请求!"), null, null); return null; } Date starttime = new Date(); StringBuffer sql = new StringBuffer("SELECT ${column_list_str} FROM ${tableName} WHERE " + sqlCondition); List<${beanName}PO> resultList = null; try { resultList = (fromWriteDB ? jdbcegroceryWrite : jdbcegroceryRead).query(sql.toString(), new DAORowMapper<${beanName}PO>(${beanName}PO.class)); return resultList; } catch (Exception e) { LogUtils.error("error:", logger, e, sql.toString(), null); } finally { Date endtime = new Date(); if(false)LogUtils.info("info", logger, "${beanName}Dao.findListBySql(" + sqlCondition + ", " + fromWriteDB + ")\n" + "fromWriteDB:" + fromWriteDB + "\n" + "执行SQL:" + sql.toString() + "\n" + "返回条数:" + (resultList==null?0:resultList.size()) + "\n" + "方法执行时长:" + (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) { return findPageByCondition(condition, null , page, pageSize, false); } /** * 按条件分页查询 * Object[]数组长度是3 * Object[]第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 * boolean isUseCache, 是否用缓存,默认用。 * boolean isAddCache, 是否添加缓存,默认添加。 */ public Map findPageByCondition(List condition,String sortCondition, int page, int pageSize, boolean fromWriteDB) { if (StringUtil.isAutowired(condition)) { LogUtils.error("error:", logger, new Exception("请求条件异常,无效请求!"), null, null); return null; } Date 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(pageSize); if(!StringUtils.isEmpty(sortCondition)){ sql.append(" " + sortCondition + " "); pql.append(" " + sortCondition + " "); } String pageSql = sql.toString() + " limit ?, ?"; String pagePql = pql.toString() + " limit " + (page -1) * pageSize + ", " + pageSize; <#else> String pageSql = null; String pagePql = null; if (page_list.size() == 0) { 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 (" + 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); } page_list.add(page * pageSize); page_list.add((page - 1) * pageSize + 1); List<${beanName}PO> resultList = null; try { int totalPages = 0; int totalRows = (fromWriteDB ? jdbcegroceryWrite : jdbcegroceryRead).queryForInt(sqlCount.toString(), count_list.toArray()) ; if (totalRows % pageSize == 0) { totalPages = totalRows / pageSize; } else { totalPages = (totalRows / pageSize) + 1; } Map resultMap = new HashMap(); resultMap.put("page", page); resultMap.put("total", totalRows); resultList = (fromWriteDB ? jdbcegroceryWrite : jdbcegroceryRead).query( pageSql.toString(), new DAORowMapper<${beanName}PO>(${beanName}PO.class), page_list.toArray()); resultMap.put("rows", resultList); return resultMap; } catch (Exception e) { LogUtils.error("error:", logger, e, pagePql.toString(), page_list.toArray()); } finally { Date endtime = new Date(); if(false)LogUtils.info("info:", logger, "${beanName}Dao.findPageByCondition(" + condition.toString() + ", " + page + ", " + pageSize + ", " + fromWriteDB + ")\n" + "执行SQL:" + pagePql.toString() + "\n" + "返回条数:" + (resultList==null?0:resultList.size()) + "\n" + "方法执行时长:" + (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) { return findPageBySql(sqlCondition, page, pageSize, false); } /** * 按sql分页查询, sqlCondition为where 后条件sql */ public Map findPageBySql(String sqlCondition, int page, int pageSize,boolean fromWriteDB) { if (StringUtil.isAutowired(sqlCondition)) { LogUtils.error("error:", logger, new Exception("请求条件异常,无效请求!"), null, null); return null; } Date starttime = new Date(); <#if dbType = "mysql"> StringBuffer sql = new StringBuffer("SELECT ${column_list_str} FROM ${tableName} WHERE " + sqlCondition ); 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; 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); List page_list = new ArrayList(); page_list.add(page * pageSize); page_list.add((page - 1) * pageSize + 1); List<${beanName}PO> resultList = null; try { int totalPages = 0; int totalRows = (fromWriteDB ? jdbcegroceryWrite : jdbcegroceryRead).queryForInt(sqlCount.toString()); if (totalRows % pageSize == 0) { totalPages = totalRows / pageSize; } else { totalPages = (totalRows / pageSize) + 1; } Map resultMap = new HashMap(); resultMap.put("page", page); resultMap.put("total", totalRows); resultList = (fromWriteDB ? jdbcegroceryWrite : jdbcegroceryRead).query(pageSql.toString(), new DAORowMapper<${beanName}PO>(${beanName}PO.class), page_list.toArray()); resultMap.put("rows", resultList); return resultMap; } catch (Exception e) { LogUtils.error("error:", logger, e, pagePql.toString(), null); } finally { Date endtime = new Date(); if(false)LogUtils.info("info:", logger, "${beanName}Dao.findPageBySql(" + sqlCondition + ", " + page + ", " + pageSize + ", " + fromWriteDB + ")\n" + "fromWriteDB:" +fromWriteDB + "\n" + "执行SQL:" + pagePql.toString() + "\n" + "返回条数:" + (resultList==null?0:resultList.size()) + "\n" + "方法执行时长:" + (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 add${beanName}PO(${beanName}PO ${beanName}) { if (null == ${beanName}) { LogUtils.error("error:", logger, new Exception("请求条件异常,${beanName}不能为空!"), null, null); return -1; } Date 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.find${beanName}POSeqNextVal()); } 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 { jdbcegroceryWrite.update(StringUtil.removeLast(StringUtil.removeLast(sqlI.toString())+sqlC.toString()) + ")", list.toArray()); count = jdbcegroceryWrite.queryForInt("SELECT LAST_INSERT_ID()"); return count; } catch (Exception e) { LogUtils.error("error:", logger, e, StringUtil.removeLast(pql.toString()) + ")", null); } finally { Date endtime = new Date(); LogUtils.info("info:", logger, "${beanName}Dao.add${beanName}PO (${beanName}PO ${beanName})\n" + "执行SQL:" + StringUtil.removeLast(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; } /** * 批量添加 */ public int add${beanName}POList(List<${beanName}PO> ${beanName}list) { Date starttime = new Date(); int count = 0; for (${beanName}PO ${beanName} : ${beanName}list) { count += add${beanName}PO(${beanName}); } Date endtime = new Date(); LogUtils.info("info:", logger, "${beanName}Dao.add${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; } /** * 按ID删除 */ public int delete${beanName}POByID(${pkType} id) { Date 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 = jdbcegroceryWrite.update(sql.toString(), id); return count; } catch (Exception e) { LogUtils.error("error:", logger, e, pql.toString(), null); } finally { Date endtime = new Date(); LogUtils.info("info:", logger, "${beanName}Dao.delete${beanName}POByID(${pkType} id)\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; } /** * 删除按List条件 * Object[]数组长度是3 * Object[], 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 */ public int delete${beanName}POByCondition(List condition) { if (null == condition || condition.size() == 0) { LogUtils.error("error:", logger, new Exception("请求条件异常,请求条件List condition不能为空!"), null, null); return -1; } if (StringUtil.isAutowired(condition)) { LogUtils.error("error:", logger, new Exception("请求条件异常,无效请求!"), null, null); return -1; } Date 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 { count = jdbcegroceryWrite.update( sql.toString(), list.toArray()); return count; } catch (Exception e) { LogUtils.error("error:", logger, e, pql.toString(), null); } finally { Date endtime = new Date(); LogUtils.info("cacheinfo:", logger, "${beanName}Dao.delete${beanName}POByCondition(List condition)\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条件 * 建议使用delete${beanName}POByCondition(List condition), 如果remove${beanName}POByCondition(List condition)满足不了where条件可以使用此方法。 * condition为where后面的条件,condition不能为空。 */ public int delete${beanName}POBySql(String sqlCondition) { if (StringUtil.isEmpty(sqlCondition)) { LogUtils.error("error:", logger, new Exception("请求条件异常,请求条件String sqlCondition不能为空!"), null, null); return -1; } if (StringUtil.isAutowired(sqlCondition)) { LogUtils.error("error:", logger, new Exception("请求条件异常,无效请求!"), null, null); return -1; } Date starttime = new Date(); StringBuffer sql = new StringBuffer("DELETE FROM ${tableName} WHERE "); int count = 0; try { count = jdbcegroceryWrite.update( sql.toString() + sqlCondition); return count; } catch (Exception e) { LogUtils.error("error:", logger, e, sql.toString() + sqlCondition, null); } finally { Date endtime = new Date(); LogUtils.info("info", logger, "${beanName}Dao.delete${beanName}POBySql(String sqlCondition)\n" + "执行SQL:" + sql.toString() + sqlCondition + "\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; } /** * 根据list对象逐个删除。 */ public int delete${beanName}POList(List<${beanName}PO> ${beanName}list) { int count = 0; for (${beanName}PO ${beanName} : ${beanName}list) { count += delete${beanName}POByID(${beanName}.get${pkBeanName}()); } return count; } /** * 根据ID修改指定的值 */ public int update${beanName}FieldById(List changeList, ${pkType} id) { if (null == id) { LogUtils.error("error:", logger, new Exception("请求条件异常,请求对象和对象ID不能为空!"), null, null); return -1; } Date 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 { count = jdbcegroceryWrite.update(StringUtil.removeLast(sql.toString()) + where, list.toArray()); return count; } catch (Exception e) { LogUtils.error("error:", logger, e, StringUtil.removeLast(pql.toString()) + pwhere, null); } finally { Date endtime = new Date(); LogUtils.info("info", logger, "${beanName}Dao.update${beanName}PO(List changeList, ${pkType} id)\n" + "执行SQL:" + StringUtil.removeLast(pql.toString()) + pwhere + "\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; } /** * List updateObj 要修改成的值,数组长度为2,第一个值为列名,第二个值是要改成的值。 * List condition 修改的条件, 数组长度是3, 第一个参数是列名,第二个参数是操作符,第三个参数是查询条件的值。 */ public int update${beanName}POByCondition(List updateObj, List condition) { if (null == updateObj || updateObj.size() == 0) { LogUtils.error("error:", logger, new Exception("请求条件异常,请求条件List updateObj不能为空!"), null, null); return -1; } if (null == condition || condition.size() == 0) { LogUtils.error("error:", logger, new Exception("请求条件异常,请求条件List condition不能为空!"), null, null); return -1; } if (StringUtil.isAutowired(condition)) { LogUtils.error("error:", logger, new Exception("请求条件异常,无效请求!"), null, null); return -1; } Date 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 { count = jdbcegroceryWrite.update(StringUtil.removeLast(sql.toString()) + where.toString(), list.toArray()); return count; } catch (Exception e) { LogUtils.error("error:", logger, e, StringUtil.removeLast(pql.toString())+pwhere.toString(), null, null); } finally { Date endtime = new Date(); LogUtils.info("info", logger, "${beanName}Dao.update${beanName}POByCondition(List updateObj, List condition)\n" + "执行SQL:" + StringUtil.removeLast(pql.toString())+pwhere.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; } /** * List updateObj 要修改成的值,数组长度为2,第一个值为列名,第二个值是要改成的值。 * String sqlCondition 修改的条件。 */ public int update${beanName}POBySql(List updateObj, String sqlCondition) { if (null == updateObj || updateObj.size() == 0) { LogUtils.error("error:", logger, new Exception("请求条件异常,请求条件List updateObj不能为空!"), null, null); return -1; } if (StringUtil.isEmpty(sqlCondition)) { LogUtils.error("error:", logger, new Exception("请求条件异常,请求条件sqlCondition不能为空!"), null, null); return -1; } if (StringUtil.isAutowired(sqlCondition)) { LogUtils.error("error:", logger, new Exception("请求条件异常,无效请求!"), null, null); return -1; } Date 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 { count = jdbcegroceryWrite.update(StringUtil.removeLast(sql.toString()) + " WHERE "+sqlCondition, list.toArray()); return count; } catch (Exception e) { LogUtils.error("error:", logger, e, StringUtil.removeLast(pql.toString()) + " WHERE " + sqlCondition, null, null); } finally { Date endtime = new Date(); LogUtils.info("info", logger, "${beanName}Dao.update${beanName}POBySql(List updateObj, String sqlCondition)\n" + "执行SQL:" + StringUtil.removeLast(pql.toString()) + " WHERE " + sqlCondition + "\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; } }