示例#1
0
  @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);
    }
  }
示例#2
0
  @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);
    }
  }