@Override public List<String> queryForListString(String sql, Object... args) { try { EmptyUtils.isEmptyException(sql, "sql为空!"); List<String> resultList = null; List<Map<String, Object>> list = null; long startTime = System.currentTimeMillis(); if (!EmptyUtils.isEmpty(args)) { list = this.jdbcTemplate.queryForList(sql, args); } else { list = this.jdbcTemplate.queryForList(sql); } if (EmptyUtils.isNotEmpty(list)) { resultList = new ArrayList<String>(); for (Map<String, Object> map : list) { Iterator<String> keys = map.keySet().iterator(); if (keys.hasNext()) { String key = keys.next(); resultList.add((String) map.get(key)); } } } long endTime = System.currentTimeMillis(); LOG.debug("processTime:{}", endTime - startTime); return resultList; } catch (Exception e) { LOG.error("queryForListString error:", e); throw new DaoException(e); } }
@Override public <T> T queryForObject(String sql, Class<T> requestType, Object... args) { try { EmptyUtils.isEmptyException(sql, "sql为空!"); EmptyUtils.isEmptyException(requestType, "requestType类型对象为空!"); long startTime = System.currentTimeMillis(); T t = null; List<T> list = null; if (EmptyUtils.isNotEmpty(args)) { list = (List<T>) this.jdbcTemplate.query(sql, args, new BeanPropertyRowMapper<T>(requestType)); } else { list = (List<T>) this.jdbcTemplate.query(sql, new BeanPropertyRowMapper<T>(requestType)); } if (EmptyUtils.isNotEmpty(list)) { t = list.get(0); } long endTime = System.currentTimeMillis(); LOG.debug("processTime:{}", endTime - startTime); return t; } catch (Exception e) { LOG.error("queryForObject error:", e); throw new DaoException(e); } }
@Override public <T> int update(String sql, T t) { try { EmptyUtils.isEmptyException(sql, "sql语句不能为空!"); EmptyUtils.isEmptyException(t, "保存的对象是空值!"); long startTime = System.currentTimeMillis(); SqlParameterSource ps = new BeanPropertySqlParameterSource(t); int result = namedParameterJdbcTemplate.update(sql, ps); long endTime = System.currentTimeMillis(); LOG.debug("processTime:{}", endTime - startTime); return result; } catch (Exception e) { LOG.error("updateObject error:", e); throw new DaoException(e); } }
@Override public int update(String sql, Object... params) { try { EmptyUtils.isEmptyException(sql, "sql语句不能为空!"); return this.jdbcTemplate.update(sql, params); } catch (Exception e) { LOG.error("update error:", e); throw new DaoException(e); } }
@Override public int[] batchDelete(String[] sql) { EmptyUtils.isEmptyException(sql, "sql为空!"); try { return this.jdbcTemplate.batchUpdate(sql); } catch (Exception e) { LOG.error("batchDelete error:", e); throw new DaoException(e); } }
@Override public int[] batchDelete(String sql, List<Object[]> batchArgs) { EmptyUtils.isEmptyException(sql, "sql为空!"); try { return this.jdbcTemplate.batchUpdate(sql, batchArgs); } catch (Exception e) { LOG.error("batchDeleteObject error:", e); throw new DaoException(e); } }
@Override public int queryForInt(String sql, Object... params) { try { EmptyUtils.isEmptyException(sql, "sql"); int result = 0; long startTime = System.currentTimeMillis(); if (EmptyUtils.isEmpty(params)) { result = this.jdbcTemplate.queryForInt(sql); } else { result = this.jdbcTemplate.queryForInt(sql, params); } long endTime = System.currentTimeMillis(); LOG.debug("processTime:{}", endTime - startTime); return result; } catch (Exception e) { LOG.error("queryForInt error:", e); throw new DaoException(e); } }
@Override public String queryForString(String sql, Object... args) { try { EmptyUtils.isEmptyException(sql, "sql为空!"); String result = ""; List<Map<String, Object>> list = null; long startTime = System.currentTimeMillis(); list = this.jdbcTemplate.queryForList(sql, args); if (CollectionUtils.isNotEmpty(list)) { Map<String, Object> map = (Map<String, Object>) list.get(0); Iterator<String> keys = map.keySet().iterator(); if (keys.hasNext()) { String key = keys.next(); result = (String) map.get(key); } } long endTime = System.currentTimeMillis(); LOG.debug("processTime:{}", endTime - startTime); return result; } catch (Exception e) { LOG.error("queryForString error:", e); throw new DaoException(e); } }