diff --git a/src/main/java/com/taover/repository/CustomJdbcTemplate.java b/src/main/java/com/taover/repository/CustomJdbcTemplate.java index ad38da6..33751cf 100644 --- a/src/main/java/com/taover/repository/CustomJdbcTemplate.java +++ b/src/main/java/com/taover/repository/CustomJdbcTemplate.java @@ -250,13 +250,7 @@ public class CustomJdbcTemplate { sql.append(" FOR UPDATE"); pql.append(" FOR UPDATE"); } - T result = null; - try { - result = (T) (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).queryForObject(sql.toString(), this.customJdbcTemplateRowMapper, id); - }catch(Exception e) { - UtilsLog.errorForException(e, this.getClass()); - } - return result; + return (T) (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).queryForObject(sql.toString(), this.customJdbcTemplateRowMapper, id); } /** @@ -315,14 +309,7 @@ public class CustomJdbcTemplate { sql.append(" " + sortCondition + " "); pql.append(" " + sortCondition + " "); } - List resultList = null; - try { - resultList = (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).query(sql.toString(), this.customJdbcTemplateRowMapper, list.toArray()); - return resultList; - } catch (Exception e) { - UtilsLog.errorForException(e, this.getClass()); - } - return null; + return (List)(fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).query(sql.toString(), this.customJdbcTemplateRowMapper, list.toArray()); } /** @@ -339,14 +326,7 @@ public class CustomJdbcTemplate { */ public List findListBySql(String sqlCondition, boolean fromWriteDB) { StringBuffer sql = new StringBuffer("SELECT "+this.tableFieldNameListGapWithComma+" FROM "+this.getTableSql()+" WHERE " + sqlCondition); - List resultList = null; - try { - resultList = (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).query(sql.toString(), this.customJdbcTemplateRowMapper); - return resultList; - } catch (Exception e) { - UtilsLog.errorForException(e, this.getClass()); - } - return null; + return (List)(fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).query(sql.toString(), this.customJdbcTemplateRowMapper); } /** @@ -386,21 +366,16 @@ public class CustomJdbcTemplate { } String pageSql = sql.toString() + " limit ?, ?"; - + + Map totalRowsMap = (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).queryForMap(sqlCount.toString(), count_list.toArray()) ; + + Map resultMap = new HashMap(); + resultMap.put("page", page); + resultMap.put("total", totalRowsMap.get("rowCount")); List resultList = null; - try { - Map totalRowsMap = (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).queryForMap(sqlCount.toString(), count_list.toArray()) ; - - Map resultMap = new HashMap(); - resultMap.put("page", page); - resultMap.put("total", totalRowsMap.get("rowCount")); - resultList = (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).query(pageSql.toString(), this.customJdbcTemplateRowMapper, page_list.toArray()); - resultMap.put("rows", resultList); - return resultMap; - } catch (Exception e) { - UtilsLog.errorForException(e, this.getClass()); - } - return null; + resultList = (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).query(pageSql.toString(), this.customJdbcTemplateRowMapper, page_list.toArray()); + resultMap.put("rows", resultList); + return resultMap; } /** @@ -422,22 +397,15 @@ public class CustomJdbcTemplate { page_list.add((page - 1) * pageSize); page_list.add(page * pageSize); + Map totalRowsMap = (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).queryForMap(sqlCount.toString()); + Map resultMap = new HashMap(); + resultMap.put("page", page); + resultMap.put("total", totalRowsMap.get("rowCount")); List resultList = null; - try { - - Map totalRowsMap = (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).queryForMap(sqlCount.toString()); - - Map resultMap = new HashMap(); - resultMap.put("page", page); - resultMap.put("total", totalRowsMap.get("rowCount")); - resultList = (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).query(pageSql.toString(), this.customJdbcTemplateRowMapper, page_list.toArray()); - resultMap.put("rows", resultList); - return resultMap; - } catch (Exception e) { - UtilsLog.errorForException(e, this.getClass()); - } - return null; + resultList = (fromWriteDB ? jdbcTemplateWrite : jdbcTemplateRead).query(pageSql.toString(), this.customJdbcTemplateRowMapper, page_list.toArray()); + resultMap.put("rows", resultList); + return resultMap; } /** @@ -482,14 +450,9 @@ public class CustomJdbcTemplate { //执行SQL String exeSql = sqlInsertPart.substring(0, sqlInsertPart.length()-1)+sqlColumnPart.substring(0, sqlColumnPart.length()-1)+")"; - try { - jdbcTemplateWrite.update(exeSql, paramList.toArray()); - Map lastInsertIdMap = jdbcTemplateWrite.queryForMap("SELECT LAST_INSERT_ID() lastInsertId"); - return (BigInteger)lastInsertIdMap.get("lastInsertId"); - } catch (Exception e) { - UtilsLog.errorForException(e, this.getClass()); - } - return null; + jdbcTemplateWrite.update(exeSql, paramList.toArray()); + Map lastInsertIdMap = jdbcTemplateWrite.queryForMap("SELECT LAST_INSERT_ID() lastInsertId"); + return (BigInteger)lastInsertIdMap.get("lastInsertId"); } /** @@ -511,7 +474,6 @@ public class CustomJdbcTemplate { StringBuffer pql = new StringBuffer(sql.toString()); pql.append(" "+this.idTableFieldName+" = " + id); sql.append(" "+this.idTableFieldName+" = ?"); - //UtilsLog.infoForMessage(pql.toString(), this.getClass()); return jdbcTemplateWrite.update(sql.toString(), id); } /** @@ -528,12 +490,7 @@ public class CustomJdbcTemplate { StringBuffer sql = new StringBuffer("DELETE FROM "+this.getTableSql()+""); StringBuffer pql = new StringBuffer(sql.toString()); this.appendWhereCondition(sql, pql, list, condition); - try { - return jdbcTemplateWrite.update( sql.toString(), list.toArray()); - } catch (Exception e) { - UtilsLog.errorForException(e, this.getClass()); - } - return 0; + return jdbcTemplateWrite.update( sql.toString(), list.toArray()); } /** @@ -544,14 +501,8 @@ public class CustomJdbcTemplate { public int deleteEntityBySql(String sqlCondition) throws Exception{ if("".equals(sqlCondition)) { throw new Exception("没有传入条件,请至少传入一个筛选条件"); - } - StringBuffer sql = new StringBuffer("DELETE FROM "+this.getTableSql()+" WHERE "); - try { - return jdbcTemplateWrite.update( sql.toString() + sqlCondition); - } catch (Exception e) { - UtilsLog.errorForException(e, this.getClass()); } - return 0; + return jdbcTemplateWrite.update( "DELETE FROM "+this.getTableSql()+" WHERE " + sqlCondition); } /** @@ -606,19 +557,10 @@ public class CustomJdbcTemplate { } } - //记录SQL - String pwhere = " WHERE "+this.idTableFieldName+"=\"" + id + "\""; - //UtilsLog.infoForMessage(pql.toString()+pwhere, this.getClass()); - - try { - String where = " WHERE "+this.idTableFieldName+"=?"; - String updateSql = sql.substring(0, sql.length()-1)+where; - list.add(id); - return jdbcTemplateWrite.update(updateSql, list.toArray()); - } catch (Exception e) { - UtilsLog.errorForException(e, this.getClass()); - } - return 0; + String where = " WHERE "+this.idTableFieldName+"=?"; + String updateSql = sql.substring(0, sql.length()-1)+where; + list.add(id); + return jdbcTemplateWrite.update(updateSql, list.toArray()); } /** @@ -642,15 +584,8 @@ public class CustomJdbcTemplate { StringBuffer pwhere = new StringBuffer(""); this.appendWhereCondition(where, pwhere, list, condition); - //UtilsLog.infoForMessage(pql.toString()+pwhere.toString(), this.getClass()); - String updateSql = sql.substring(0, sql.length()-1)+where.toString(); - try { - return jdbcTemplateWrite.update(updateSql, list.toArray()); - } catch (Exception e) { - UtilsLog.errorForException(e, this.getClass()); - } - return 0; + return jdbcTemplateWrite.update(updateSql, list.toArray()); } /** @@ -670,13 +605,8 @@ public class CustomJdbcTemplate { List list = new ArrayList(); this.appendSql(sql, pql, list, updateObj); - try { - String updateSql = sql.toString().substring(0, sql.length()-1) + " WHERE "+sqlCondition; - return jdbcTemplateWrite.update(updateSql, list.toArray()); - } catch (Exception e) { - UtilsLog.errorForException(e, this.getClass()); - } - return 0; + String updateSql = sql.toString().substring(0, sql.length()-1) + " WHERE "+sqlCondition; + return jdbcTemplateWrite.update(updateSql, list.toArray()); } public Map getPageData(String coreSql, String orderByPartSql, Integer page, Integer pageSize){ @@ -688,16 +618,9 @@ public class CustomJdbcTemplate { //执行查询 List> queryData = new ArrayList>(); - Map countData = new HashMap(); - try{ - queryData = this.jdbcTemplateRead.queryForList(querySql); - countData = this.jdbcTemplateRead.queryForMap(countSql); - }catch(Exception e){ - countData.put("rows", "0"); - - UtilsLog.errorForException(e, this.getClass()); - } - + Map countData = new HashMap(); + queryData = this.jdbcTemplateRead.queryForList(querySql); + countData = this.jdbcTemplateRead.queryForMap(countSql); return UtilsSql.createPage(page, Integer.valueOf(countData.get("rows").toString()), queryData); } } -- libgit2 0.21.2