diff --git a/build.gradle b/build.gradle index 6b14ffe..d7c0cb4 100644 --- a/build.gradle +++ b/build.gradle @@ -55,7 +55,7 @@ uploadArchives { authentication(userName: NEXUS_USERNAME, password: NEXUS_PASSWORD) } pom.project { - version '2.1.8' + version '2.1.9' artifactId ARTIFACT_Id groupId GROUP_ID packaging TYPE diff --git a/src/main/java/com/taover/repository/CustomJdbcTemplate.java b/src/main/java/com/taover/repository/CustomJdbcTemplate.java index e8d4040..c8680c9 100644 --- a/src/main/java/com/taover/repository/CustomJdbcTemplate.java +++ b/src/main/java/com/taover/repository/CustomJdbcTemplate.java @@ -16,6 +16,7 @@ import javax.persistence.Id; import javax.persistence.Table; import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.transaction.annotation.Transactional; /** * @@ -209,20 +210,20 @@ public class CustomJdbcTemplate { } } - private void appendSql(StringBuffer sql, StringBuffer pql, List list, List obj) { + private void appendSetSql(StringBuffer sql, StringBuffer pql, List list, List obj) { for (Object[] arg : obj) { - String opt = "="; if (arg.length > 2) { - opt = (String)arg[2]; - } - if (opt.equals("=")) { - sql.append(" " + arg[0] + " = ?,"); + sql.append(" " + arg[0] + " = " + arg[0] + arg[2] + " ?, "); + pql.append(" " + arg[0] + " = " + arg[0] + arg[2] + arg[1] +", "); + list.add(arg[1]); + }else if(arg.length == 2) { + sql.append(" " + arg[0] + " = ?,"); pql.append(" " + arg[0] + " = " + arg[1] + ","); - } else { - sql.append(" " + arg[0] + " = " + arg[0] + " + ?,"); - pql.append(" " + arg[0] + " = " + arg[0] + " + " + arg[1] + ","); + list.add(arg[1]); + }else if(arg.length == 1) { + sql.append(" " + arg[0] + ", "); + pql.append(" " + arg[0] + ", "); } - list.add(arg[1]); } } @@ -269,7 +270,7 @@ public class CustomJdbcTemplate { if(tempList != null && tempList.size() == 1){ return tempList.get(0); }else{ - throw new Exception("数据库存在多条记录满足条件"); + throw new Exception("found multi rows with condition,but this func expected one row"); } } @@ -285,7 +286,7 @@ public class CustomJdbcTemplate { if(tempList != null && tempList.size() == 1){ return tempList.get(0); }else{ - throw new Exception("数据库存在多条记录满足条件"); + throw new Exception("found multi rows with condition,but this func expected one row"); } } @@ -402,6 +403,7 @@ public class CustomJdbcTemplate { /** * 添加 */ + @Transactional(rollbackFor = {Exception.class, Error.class}) public BigInteger addEntityForAutoincrementId(T entity) { StringBuffer sqlForLog = new StringBuffer("INSERT INTO "+this.getTableSql()+"("); StringBuffer sqlValueForLog = new StringBuffer(") VALUES ("); @@ -474,7 +476,7 @@ public class CustomJdbcTemplate { */ public int deleteEntityByCondition(List condition) throws Exception{ if (null == condition || condition.size() == 0) { - throw new Exception("没有传入条件,请至少传入一个筛选条件"); + throw new Exception("params[condition] is empty"); } List list = new ArrayList(); @@ -491,7 +493,7 @@ public class CustomJdbcTemplate { */ public int deleteEntityBySql(String sqlCondition) throws Exception{ if("".equals(sqlCondition)) { - throw new Exception("没有传入条件,请至少传入一个筛选条件"); + throw new Exception("params[sqlCondition] is empty"); } return jdbcTemplateWrite.update( "DELETE FROM "+this.getTableSql()+" WHERE " + sqlCondition); } @@ -521,32 +523,16 @@ public class CustomJdbcTemplate { */ public int updateEntityById(List changeList, ID id) throws Exception{ if(null == id){ - throw new Exception("请求条件异常,没有传入主键ID!"); + throw new Exception("params[id] is null"); + } + if (null == changeList || changeList.size() == 0) { + throw new Exception("params[changeList] is empty"); } StringBuffer sql = new StringBuffer("UPDATE "+this.getTableSql()+" SET"); StringBuffer pql = new StringBuffer(sql.toString()); List list = new ArrayList(); - 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() + "\","); - } - } else { - sql.append(" " + name + " = " + name + " + ?,"); - list.add(value); - pql.append(" " + name + " = " + name + " + " + value.toString() + ","); - } - } + this.appendSetSql(sql, pql, list, changeList); String where = " WHERE "+this.idTableFieldName+"=?"; String updateSql = sql.substring(0, sql.length()-1)+where; @@ -560,16 +546,16 @@ public class CustomJdbcTemplate { */ public int updateEntityByCondition(List updateObj, List condition) throws Exception{ if (null == updateObj || updateObj.size() == 0) { - throw new Exception("请求条件异常,请求条件List updateObj不能为空!"); + throw new Exception("params[updateObj] is empty"); } if (null == condition || condition.size() == 0) { - throw new Exception("请求条件异常,请求条件List condition不能为空!"); + throw new Exception("params[condition] is empty"); } StringBuffer sql = new StringBuffer("UPDATE "+this.getTableSql()+" SET"); StringBuffer pql = new StringBuffer(sql.toString()); List list = new ArrayList(); - this.appendSql(sql, pql, list, updateObj); + this.appendSetSql(sql, pql, list, updateObj); StringBuffer where = new StringBuffer(""); StringBuffer pwhere = new StringBuffer(""); @@ -585,16 +571,16 @@ public class CustomJdbcTemplate { */ public int updateEntityBySql(List updateObj, String sqlCondition) throws Exception{ if (null == updateObj || updateObj.size() == 0) { - throw new Exception("请求条件异常,请求条件List updateObj不能为空!"); + throw new Exception("params[updateObj] is empty"); } if ("".equals(sqlCondition)) { - throw new Exception("请求条件异常,请求条件sqlCondition不能为空!"); + throw new Exception("params[sqlCondition] is empty"); } StringBuffer sql = new StringBuffer("UPDATE "+this.getTableSql()+" SET"); StringBuffer pql = new StringBuffer(sql.toString()); List list = new ArrayList(); - this.appendSql(sql, pql, list, updateObj); + this.appendSetSql(sql, pql, list, updateObj); String updateSql = sql.toString().substring(0, sql.length()-1) + " WHERE "+sqlCondition; return jdbcTemplateWrite.update(updateSql, list.toArray()); @@ -685,7 +671,7 @@ public class CustomJdbcTemplate { if(tempList != null && tempList.size() == 1){ return tempList.get(0); }else{ - throw new Exception("数据库存在多条记录满足条件"); + throw new Exception("found multi rows with condition,but this func expected one row"); } } @@ -701,7 +687,7 @@ public class CustomJdbcTemplate { if(tempList != null && tempList.size() == 1){ return tempList.get(0); }else{ - throw new Exception("数据库存在多条记录满足条件"); + throw new Exception("found multi rows with condition,but this func expected one row"); } } -- libgit2 0.21.2