Commit ab3b0c973a42bd9fc900a8826a84e7c6c0c7907e
1 parent
1adcb6de
Exists in
master
and in
2 other branches
1.repository add方法增加事务、更新参数构造优化、exception info优化
Showing
2 changed files
with
30 additions
and
44 deletions
Show diff stats
build.gradle
src/main/java/com/taover/repository/CustomJdbcTemplate.java
... | ... | @@ -16,6 +16,7 @@ import javax.persistence.Id; |
16 | 16 | import javax.persistence.Table; |
17 | 17 | |
18 | 18 | import org.springframework.jdbc.core.JdbcTemplate; |
19 | +import org.springframework.transaction.annotation.Transactional; | |
19 | 20 | |
20 | 21 | /** |
21 | 22 | * |
... | ... | @@ -209,20 +210,20 @@ public class CustomJdbcTemplate<T, ID extends Serializable> { |
209 | 210 | } |
210 | 211 | } |
211 | 212 | |
212 | - private void appendSql(StringBuffer sql, StringBuffer pql, List<Object> list, List<Object[]> obj) { | |
213 | + private void appendSetSql(StringBuffer sql, StringBuffer pql, List<Object> list, List<Object[]> obj) { | |
213 | 214 | for (Object[] arg : obj) { |
214 | - String opt = "="; | |
215 | 215 | if (arg.length > 2) { |
216 | - opt = (String)arg[2]; | |
217 | - } | |
218 | - if (opt.equals("=")) { | |
219 | - sql.append(" " + arg[0] + " = ?,"); | |
216 | + sql.append(" " + arg[0] + " = " + arg[0] + arg[2] + " ?, "); | |
217 | + pql.append(" " + arg[0] + " = " + arg[0] + arg[2] + arg[1] +", "); | |
218 | + list.add(arg[1]); | |
219 | + }else if(arg.length == 2) { | |
220 | + sql.append(" " + arg[0] + " = ?,"); | |
220 | 221 | pql.append(" " + arg[0] + " = " + arg[1] + ","); |
221 | - } else { | |
222 | - sql.append(" " + arg[0] + " = " + arg[0] + " + ?,"); | |
223 | - pql.append(" " + arg[0] + " = " + arg[0] + " + " + arg[1] + ","); | |
222 | + list.add(arg[1]); | |
223 | + }else if(arg.length == 1) { | |
224 | + sql.append(" " + arg[0] + ", "); | |
225 | + pql.append(" " + arg[0] + ", "); | |
224 | 226 | } |
225 | - list.add(arg[1]); | |
226 | 227 | } |
227 | 228 | } |
228 | 229 | |
... | ... | @@ -269,7 +270,7 @@ public class CustomJdbcTemplate<T, ID extends Serializable> { |
269 | 270 | if(tempList != null && tempList.size() == 1){ |
270 | 271 | return tempList.get(0); |
271 | 272 | }else{ |
272 | - throw new Exception("数据库存在多条记录满足条件"); | |
273 | + throw new Exception("found multi rows with condition,but this func expected one row"); | |
273 | 274 | } |
274 | 275 | } |
275 | 276 | |
... | ... | @@ -285,7 +286,7 @@ public class CustomJdbcTemplate<T, ID extends Serializable> { |
285 | 286 | if(tempList != null && tempList.size() == 1){ |
286 | 287 | return tempList.get(0); |
287 | 288 | }else{ |
288 | - throw new Exception("数据库存在多条记录满足条件"); | |
289 | + throw new Exception("found multi rows with condition,but this func expected one row"); | |
289 | 290 | } |
290 | 291 | } |
291 | 292 | |
... | ... | @@ -402,6 +403,7 @@ public class CustomJdbcTemplate<T, ID extends Serializable> { |
402 | 403 | /** |
403 | 404 | * 添加 |
404 | 405 | */ |
406 | + @Transactional(rollbackFor = {Exception.class, Error.class}) | |
405 | 407 | public BigInteger addEntityForAutoincrementId(T entity) { |
406 | 408 | StringBuffer sqlForLog = new StringBuffer("INSERT INTO "+this.getTableSql()+"("); |
407 | 409 | StringBuffer sqlValueForLog = new StringBuffer(") VALUES ("); |
... | ... | @@ -474,7 +476,7 @@ public class CustomJdbcTemplate<T, ID extends Serializable> { |
474 | 476 | */ |
475 | 477 | public int deleteEntityByCondition(List<Object[]> condition) throws Exception{ |
476 | 478 | if (null == condition || condition.size() == 0) { |
477 | - throw new Exception("没有传入条件,请至少传入一个筛选条件"); | |
479 | + throw new Exception("params[condition] is empty"); | |
478 | 480 | } |
479 | 481 | |
480 | 482 | List<Object> list = new ArrayList<Object>(); |
... | ... | @@ -491,7 +493,7 @@ public class CustomJdbcTemplate<T, ID extends Serializable> { |
491 | 493 | */ |
492 | 494 | public int deleteEntityBySql(String sqlCondition) throws Exception{ |
493 | 495 | if("".equals(sqlCondition)) { |
494 | - throw new Exception("没有传入条件,请至少传入一个筛选条件"); | |
496 | + throw new Exception("params[sqlCondition] is empty"); | |
495 | 497 | } |
496 | 498 | return jdbcTemplateWrite.update( "DELETE FROM "+this.getTableSql()+" WHERE " + sqlCondition); |
497 | 499 | } |
... | ... | @@ -521,32 +523,16 @@ public class CustomJdbcTemplate<T, ID extends Serializable> { |
521 | 523 | */ |
522 | 524 | public int updateEntityById(List<Object[]> changeList, ID id) throws Exception{ |
523 | 525 | if(null == id){ |
524 | - throw new Exception("请求条件异常,没有传入主键ID!"); | |
526 | + throw new Exception("params[id] is null"); | |
527 | + } | |
528 | + if (null == changeList || changeList.size() == 0) { | |
529 | + throw new Exception("params[changeList] is empty"); | |
525 | 530 | } |
526 | 531 | |
527 | 532 | StringBuffer sql = new StringBuffer("UPDATE "+this.getTableSql()+" SET"); |
528 | 533 | StringBuffer pql = new StringBuffer(sql.toString()); |
529 | 534 | List<Object> list = new ArrayList<Object>(); |
530 | - for (int i = 0, iLen = changeList.size(); i < iLen; i++) { | |
531 | - String name = (String) changeList.get(i)[0]; | |
532 | - Object value = changeList.get(i)[1]; | |
533 | - String variationOpt = "="; | |
534 | - if (changeList.get(i).length > 2) | |
535 | - variationOpt = (String)changeList.get(i)[2]; | |
536 | - if (variationOpt.equals("=")) { | |
537 | - sql.append(" " + name + " = ?,"); | |
538 | - list.add(value); | |
539 | - if (value == null) { | |
540 | - pql.append(" " + name + "=null,"); | |
541 | - } else { | |
542 | - pql.append(" " + name + "=\"" + value.toString() + "\","); | |
543 | - } | |
544 | - } else { | |
545 | - sql.append(" " + name + " = " + name + " + ?,"); | |
546 | - list.add(value); | |
547 | - pql.append(" " + name + " = " + name + " + " + value.toString() + ","); | |
548 | - } | |
549 | - } | |
535 | + this.appendSetSql(sql, pql, list, changeList); | |
550 | 536 | |
551 | 537 | String where = " WHERE "+this.idTableFieldName+"=?"; |
552 | 538 | String updateSql = sql.substring(0, sql.length()-1)+where; |
... | ... | @@ -560,16 +546,16 @@ public class CustomJdbcTemplate<T, ID extends Serializable> { |
560 | 546 | */ |
561 | 547 | public int updateEntityByCondition(List<Object[]> updateObj, List<Object[]> condition) throws Exception{ |
562 | 548 | if (null == updateObj || updateObj.size() == 0) { |
563 | - throw new Exception("请求条件异常,请求条件List<Object[]> updateObj不能为空!"); | |
549 | + throw new Exception("params[updateObj] is empty"); | |
564 | 550 | } |
565 | 551 | if (null == condition || condition.size() == 0) { |
566 | - throw new Exception("请求条件异常,请求条件List<Object[]> condition不能为空!"); | |
552 | + throw new Exception("params[condition] is empty"); | |
567 | 553 | } |
568 | 554 | |
569 | 555 | StringBuffer sql = new StringBuffer("UPDATE "+this.getTableSql()+" SET"); |
570 | 556 | StringBuffer pql = new StringBuffer(sql.toString()); |
571 | 557 | List<Object> list = new ArrayList<Object>(); |
572 | - this.appendSql(sql, pql, list, updateObj); | |
558 | + this.appendSetSql(sql, pql, list, updateObj); | |
573 | 559 | |
574 | 560 | StringBuffer where = new StringBuffer(""); |
575 | 561 | StringBuffer pwhere = new StringBuffer(""); |
... | ... | @@ -585,16 +571,16 @@ public class CustomJdbcTemplate<T, ID extends Serializable> { |
585 | 571 | */ |
586 | 572 | public int updateEntityBySql(List<Object[]> updateObj, String sqlCondition) throws Exception{ |
587 | 573 | if (null == updateObj || updateObj.size() == 0) { |
588 | - throw new Exception("请求条件异常,请求条件List<Object[]> updateObj不能为空!"); | |
574 | + throw new Exception("params[updateObj] is empty"); | |
589 | 575 | } |
590 | 576 | if ("".equals(sqlCondition)) { |
591 | - throw new Exception("请求条件异常,请求条件sqlCondition不能为空!"); | |
577 | + throw new Exception("params[sqlCondition] is empty"); | |
592 | 578 | } |
593 | 579 | |
594 | 580 | StringBuffer sql = new StringBuffer("UPDATE "+this.getTableSql()+" SET"); |
595 | 581 | StringBuffer pql = new StringBuffer(sql.toString()); |
596 | 582 | List<Object> list = new ArrayList<Object>(); |
597 | - this.appendSql(sql, pql, list, updateObj); | |
583 | + this.appendSetSql(sql, pql, list, updateObj); | |
598 | 584 | |
599 | 585 | String updateSql = sql.toString().substring(0, sql.length()-1) + " WHERE "+sqlCondition; |
600 | 586 | return jdbcTemplateWrite.update(updateSql, list.toArray()); |
... | ... | @@ -685,7 +671,7 @@ public class CustomJdbcTemplate<T, ID extends Serializable> { |
685 | 671 | if(tempList != null && tempList.size() == 1){ |
686 | 672 | return tempList.get(0); |
687 | 673 | }else{ |
688 | - throw new Exception("数据库存在多条记录满足条件"); | |
674 | + throw new Exception("found multi rows with condition,but this func expected one row"); | |
689 | 675 | } |
690 | 676 | } |
691 | 677 | |
... | ... | @@ -701,7 +687,7 @@ public class CustomJdbcTemplate<T, ID extends Serializable> { |
701 | 687 | if(tempList != null && tempList.size() == 1){ |
702 | 688 | return tempList.get(0); |
703 | 689 | }else{ |
704 | - throw new Exception("数据库存在多条记录满足条件"); | |
690 | + throw new Exception("found multi rows with condition,but this func expected one row"); | |
705 | 691 | } |
706 | 692 | } |
707 | 693 | ... | ... |