Commit 6811f8ba8cde56d220210246dfefc150a16ad3c8
1 parent
d6a3508b
Exists in
master
and in
2 other branches
1. repository do not catch db exception
Showing
1 changed file
with
34 additions
and
111 deletions
Show diff stats
src/main/java/com/taover/repository/CustomJdbcTemplate.java
| ... | ... | @@ -250,13 +250,7 @@ public class CustomJdbcTemplate<T, ID extends Serializable> { |
| 250 | 250 | sql.append(" FOR UPDATE"); |
| 251 | 251 | pql.append(" FOR UPDATE"); |
| 252 | 252 | } |
| 253 | - T result = null; | |
| 254 | - try { | |
| 255 | - result = (T) (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).queryForObject(sql.toString(), this.customJdbcTemplateRowMapper, id); | |
| 256 | - }catch(Exception e) { | |
| 257 | - UtilsLog.errorForException(e, this.getClass()); | |
| 258 | - } | |
| 259 | - return result; | |
| 253 | + return (T) (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).queryForObject(sql.toString(), this.customJdbcTemplateRowMapper, id); | |
| 260 | 254 | } |
| 261 | 255 | |
| 262 | 256 | /** |
| ... | ... | @@ -315,14 +309,7 @@ public class CustomJdbcTemplate<T, ID extends Serializable> { |
| 315 | 309 | sql.append(" " + sortCondition + " "); |
| 316 | 310 | pql.append(" " + sortCondition + " "); |
| 317 | 311 | } |
| 318 | - List<T> resultList = null; | |
| 319 | - try { | |
| 320 | - resultList = (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).query(sql.toString(), this.customJdbcTemplateRowMapper, list.toArray()); | |
| 321 | - return resultList; | |
| 322 | - } catch (Exception e) { | |
| 323 | - UtilsLog.errorForException(e, this.getClass()); | |
| 324 | - } | |
| 325 | - return null; | |
| 312 | + return (List<T>)(fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).query(sql.toString(), this.customJdbcTemplateRowMapper, list.toArray()); | |
| 326 | 313 | } |
| 327 | 314 | |
| 328 | 315 | /** |
| ... | ... | @@ -339,14 +326,7 @@ public class CustomJdbcTemplate<T, ID extends Serializable> { |
| 339 | 326 | */ |
| 340 | 327 | public List<T> findListBySql(String sqlCondition, boolean fromWriteDB) { |
| 341 | 328 | StringBuffer sql = new StringBuffer("SELECT "+this.tableFieldNameListGapWithComma+" FROM "+this.getTableSql()+" WHERE " + sqlCondition); |
| 342 | - List<T> resultList = null; | |
| 343 | - try { | |
| 344 | - resultList = (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).query(sql.toString(), this.customJdbcTemplateRowMapper); | |
| 345 | - return resultList; | |
| 346 | - } catch (Exception e) { | |
| 347 | - UtilsLog.errorForException(e, this.getClass()); | |
| 348 | - } | |
| 349 | - return null; | |
| 329 | + return (List<T>)(fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).query(sql.toString(), this.customJdbcTemplateRowMapper); | |
| 350 | 330 | } |
| 351 | 331 | |
| 352 | 332 | /** |
| ... | ... | @@ -386,21 +366,16 @@ public class CustomJdbcTemplate<T, ID extends Serializable> { |
| 386 | 366 | } |
| 387 | 367 | |
| 388 | 368 | String pageSql = sql.toString() + " limit ?, ?"; |
| 389 | - | |
| 369 | + | |
| 370 | + Map<String, Object> totalRowsMap = (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).queryForMap(sqlCount.toString(), count_list.toArray()) ; | |
| 371 | + | |
| 372 | + Map<String, Object> resultMap = new HashMap<String, Object>(); | |
| 373 | + resultMap.put("page", page); | |
| 374 | + resultMap.put("total", totalRowsMap.get("rowCount")); | |
| 390 | 375 | List<T> resultList = null; |
| 391 | - try { | |
| 392 | - Map<String, Object> totalRowsMap = (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).queryForMap(sqlCount.toString(), count_list.toArray()) ; | |
| 393 | - | |
| 394 | - Map<String, Object> resultMap = new HashMap<String, Object>(); | |
| 395 | - resultMap.put("page", page); | |
| 396 | - resultMap.put("total", totalRowsMap.get("rowCount")); | |
| 397 | - resultList = (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).query(pageSql.toString(), this.customJdbcTemplateRowMapper, page_list.toArray()); | |
| 398 | - resultMap.put("rows", resultList); | |
| 399 | - return resultMap; | |
| 400 | - } catch (Exception e) { | |
| 401 | - UtilsLog.errorForException(e, this.getClass()); | |
| 402 | - } | |
| 403 | - return null; | |
| 376 | + resultList = (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).query(pageSql.toString(), this.customJdbcTemplateRowMapper, page_list.toArray()); | |
| 377 | + resultMap.put("rows", resultList); | |
| 378 | + return resultMap; | |
| 404 | 379 | } |
| 405 | 380 | |
| 406 | 381 | /** |
| ... | ... | @@ -422,22 +397,15 @@ public class CustomJdbcTemplate<T, ID extends Serializable> { |
| 422 | 397 | page_list.add((page - 1) * pageSize); |
| 423 | 398 | page_list.add(page * pageSize); |
| 424 | 399 | |
| 400 | + Map<String, Object> totalRowsMap = (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).queryForMap(sqlCount.toString()); | |
| 425 | 401 | |
| 402 | + Map<String, Object> resultMap = new HashMap<String, Object>(); | |
| 403 | + resultMap.put("page", page); | |
| 404 | + resultMap.put("total", totalRowsMap.get("rowCount")); | |
| 426 | 405 | List<T> resultList = null; |
| 427 | - try { | |
| 428 | - | |
| 429 | - Map<String, Object> totalRowsMap = (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).queryForMap(sqlCount.toString()); | |
| 430 | - | |
| 431 | - Map<String, Object> resultMap = new HashMap<String, Object>(); | |
| 432 | - resultMap.put("page", page); | |
| 433 | - resultMap.put("total", totalRowsMap.get("rowCount")); | |
| 434 | - resultList = (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).query(pageSql.toString(), this.customJdbcTemplateRowMapper, page_list.toArray()); | |
| 435 | - resultMap.put("rows", resultList); | |
| 436 | - return resultMap; | |
| 437 | - } catch (Exception e) { | |
| 438 | - UtilsLog.errorForException(e, this.getClass()); | |
| 439 | - } | |
| 440 | - return null; | |
| 406 | + resultList = (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).query(pageSql.toString(), this.customJdbcTemplateRowMapper, page_list.toArray()); | |
| 407 | + resultMap.put("rows", resultList); | |
| 408 | + return resultMap; | |
| 441 | 409 | } |
| 442 | 410 | |
| 443 | 411 | /** |
| ... | ... | @@ -482,14 +450,9 @@ public class CustomJdbcTemplate<T, ID extends Serializable> { |
| 482 | 450 | |
| 483 | 451 | //执行SQL |
| 484 | 452 | String exeSql = sqlInsertPart.substring(0, sqlInsertPart.length()-1)+sqlColumnPart.substring(0, sqlColumnPart.length()-1)+")"; |
| 485 | - try { | |
| 486 | - jdbcTemplateWrite.update(exeSql, paramList.toArray()); | |
| 487 | - Map<String, Object> lastInsertIdMap = jdbcTemplateWrite.queryForMap("SELECT LAST_INSERT_ID() lastInsertId"); | |
| 488 | - return (BigInteger)lastInsertIdMap.get("lastInsertId"); | |
| 489 | - } catch (Exception e) { | |
| 490 | - UtilsLog.errorForException(e, this.getClass()); | |
| 491 | - } | |
| 492 | - return null; | |
| 453 | + jdbcTemplateWrite.update(exeSql, paramList.toArray()); | |
| 454 | + Map<String, Object> lastInsertIdMap = jdbcTemplateWrite.queryForMap("SELECT LAST_INSERT_ID() lastInsertId"); | |
| 455 | + return (BigInteger)lastInsertIdMap.get("lastInsertId"); | |
| 493 | 456 | } |
| 494 | 457 | |
| 495 | 458 | /** |
| ... | ... | @@ -511,7 +474,6 @@ public class CustomJdbcTemplate<T, ID extends Serializable> { |
| 511 | 474 | StringBuffer pql = new StringBuffer(sql.toString()); |
| 512 | 475 | pql.append(" "+this.idTableFieldName+" = " + id); |
| 513 | 476 | sql.append(" "+this.idTableFieldName+" = ?"); |
| 514 | - //UtilsLog.infoForMessage(pql.toString(), this.getClass()); | |
| 515 | 477 | return jdbcTemplateWrite.update(sql.toString(), id); |
| 516 | 478 | } |
| 517 | 479 | /** |
| ... | ... | @@ -528,12 +490,7 @@ public class CustomJdbcTemplate<T, ID extends Serializable> { |
| 528 | 490 | StringBuffer sql = new StringBuffer("DELETE FROM "+this.getTableSql()+""); |
| 529 | 491 | StringBuffer pql = new StringBuffer(sql.toString()); |
| 530 | 492 | this.appendWhereCondition(sql, pql, list, condition); |
| 531 | - try { | |
| 532 | - return jdbcTemplateWrite.update( sql.toString(), list.toArray()); | |
| 533 | - } catch (Exception e) { | |
| 534 | - UtilsLog.errorForException(e, this.getClass()); | |
| 535 | - } | |
| 536 | - return 0; | |
| 493 | + return jdbcTemplateWrite.update( sql.toString(), list.toArray()); | |
| 537 | 494 | } |
| 538 | 495 | |
| 539 | 496 | /** |
| ... | ... | @@ -544,14 +501,8 @@ public class CustomJdbcTemplate<T, ID extends Serializable> { |
| 544 | 501 | public int deleteEntityBySql(String sqlCondition) throws Exception{ |
| 545 | 502 | if("".equals(sqlCondition)) { |
| 546 | 503 | throw new Exception("没有传入条件,请至少传入一个筛选条件"); |
| 547 | - } | |
| 548 | - StringBuffer sql = new StringBuffer("DELETE FROM "+this.getTableSql()+" WHERE "); | |
| 549 | - try { | |
| 550 | - return jdbcTemplateWrite.update( sql.toString() + sqlCondition); | |
| 551 | - } catch (Exception e) { | |
| 552 | - UtilsLog.errorForException(e, this.getClass()); | |
| 553 | 504 | } |
| 554 | - return 0; | |
| 505 | + return jdbcTemplateWrite.update( "DELETE FROM "+this.getTableSql()+" WHERE " + sqlCondition); | |
| 555 | 506 | } |
| 556 | 507 | |
| 557 | 508 | /** |
| ... | ... | @@ -606,19 +557,10 @@ public class CustomJdbcTemplate<T, ID extends Serializable> { |
| 606 | 557 | } |
| 607 | 558 | } |
| 608 | 559 | |
| 609 | - //记录SQL | |
| 610 | - String pwhere = " WHERE "+this.idTableFieldName+"=\"" + id + "\""; | |
| 611 | - //UtilsLog.infoForMessage(pql.toString()+pwhere, this.getClass()); | |
| 612 | - | |
| 613 | - try { | |
| 614 | - String where = " WHERE "+this.idTableFieldName+"=?"; | |
| 615 | - String updateSql = sql.substring(0, sql.length()-1)+where; | |
| 616 | - list.add(id); | |
| 617 | - return jdbcTemplateWrite.update(updateSql, list.toArray()); | |
| 618 | - } catch (Exception e) { | |
| 619 | - UtilsLog.errorForException(e, this.getClass()); | |
| 620 | - } | |
| 621 | - return 0; | |
| 560 | + String where = " WHERE "+this.idTableFieldName+"=?"; | |
| 561 | + String updateSql = sql.substring(0, sql.length()-1)+where; | |
| 562 | + list.add(id); | |
| 563 | + return jdbcTemplateWrite.update(updateSql, list.toArray()); | |
| 622 | 564 | } |
| 623 | 565 | |
| 624 | 566 | /** |
| ... | ... | @@ -642,15 +584,8 @@ public class CustomJdbcTemplate<T, ID extends Serializable> { |
| 642 | 584 | StringBuffer pwhere = new StringBuffer(""); |
| 643 | 585 | this.appendWhereCondition(where, pwhere, list, condition); |
| 644 | 586 | |
| 645 | - //UtilsLog.infoForMessage(pql.toString()+pwhere.toString(), this.getClass()); | |
| 646 | - | |
| 647 | 587 | String updateSql = sql.substring(0, sql.length()-1)+where.toString(); |
| 648 | - try { | |
| 649 | - return jdbcTemplateWrite.update(updateSql, list.toArray()); | |
| 650 | - } catch (Exception e) { | |
| 651 | - UtilsLog.errorForException(e, this.getClass()); | |
| 652 | - } | |
| 653 | - return 0; | |
| 588 | + return jdbcTemplateWrite.update(updateSql, list.toArray()); | |
| 654 | 589 | } |
| 655 | 590 | |
| 656 | 591 | /** |
| ... | ... | @@ -670,13 +605,8 @@ public class CustomJdbcTemplate<T, ID extends Serializable> { |
| 670 | 605 | List<Object> list = new ArrayList<Object>(); |
| 671 | 606 | this.appendSql(sql, pql, list, updateObj); |
| 672 | 607 | |
| 673 | - try { | |
| 674 | - String updateSql = sql.toString().substring(0, sql.length()-1) + " WHERE "+sqlCondition; | |
| 675 | - return jdbcTemplateWrite.update(updateSql, list.toArray()); | |
| 676 | - } catch (Exception e) { | |
| 677 | - UtilsLog.errorForException(e, this.getClass()); | |
| 678 | - } | |
| 679 | - return 0; | |
| 608 | + String updateSql = sql.toString().substring(0, sql.length()-1) + " WHERE "+sqlCondition; | |
| 609 | + return jdbcTemplateWrite.update(updateSql, list.toArray()); | |
| 680 | 610 | } |
| 681 | 611 | |
| 682 | 612 | public Map<String, Object> getPageData(String coreSql, String orderByPartSql, Integer page, Integer pageSize){ |
| ... | ... | @@ -688,16 +618,9 @@ public class CustomJdbcTemplate<T, ID extends Serializable> { |
| 688 | 618 | |
| 689 | 619 | //执行查询 |
| 690 | 620 | List<Map<String, Object>> queryData = new ArrayList<Map<String, Object>>(); |
| 691 | - Map<String, Object> countData = new HashMap<String, Object>(); | |
| 692 | - try{ | |
| 693 | - queryData = this.jdbcTemplateRead.queryForList(querySql); | |
| 694 | - countData = this.jdbcTemplateRead.queryForMap(countSql); | |
| 695 | - }catch(Exception e){ | |
| 696 | - countData.put("rows", "0"); | |
| 697 | - | |
| 698 | - UtilsLog.errorForException(e, this.getClass()); | |
| 699 | - } | |
| 700 | - | |
| 621 | + Map<String, Object> countData = new HashMap<String, Object>(); | |
| 622 | + queryData = this.jdbcTemplateRead.queryForList(querySql); | |
| 623 | + countData = this.jdbcTemplateRead.queryForMap(countSql); | |
| 701 | 624 | return UtilsSql.createPage(page, Integer.valueOf(countData.get("rows").toString()), queryData); |
| 702 | 625 | } |
| 703 | 626 | } | ... | ... |