JdbcTemplateRepository.java
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ParameterizedPreparedStatementSetter;
import org.springframework.jdbc.core.SingleColumnRowMapper;
import org.springframework.stereotype.Repository;
import java.util.Collection;
import java.util.List;
import java.util.Map;
@Repository
public class JdbcTemplateRepository {
private static final Logger logger = LoggerFactory.getLogger(JdbcTemplateRepository.class);
@Autowired
private JdbcTemplate jdbcTemplate;
public <T> List<T> queryForList(String sql, Class<T> tClass){
logger.info("sql: " + sql);
List<T> resultList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(tClass));
logger.info("resultList: " + resultList);
return resultList;
}
public <T> List<T> queryForList(String sql, Class<T> tClass, Object... args){
logger.info("sql: " + sql);
List<T> resultList = jdbcTemplate.query(sql, args, new BeanPropertyRowMapper<>(tClass));
logger.info("resultList: " + resultList);
return resultList;
}
public <T> List<T> queryForListSingleColumn(String sql, Class<T> tClass){
logger.info("sql: " + sql);
List<T> resultList = jdbcTemplate.query(sql, new SingleColumnRowMapper<>(tClass));
logger.info("resultList: " + resultList);
return resultList;
}
public <T> List<T> queryForListSingleColumn(String sql, Class<T> tClass, Object... args){
logger.info("sql: " + sql);
List<T> resultList = jdbcTemplate.query(sql, args, new SingleColumnRowMapper<>(tClass));
logger.info("resultList: " + resultList);
return resultList;
}
public List<Map<String, Object>> queryForListMap(String sql){
logger.info("sql: " + sql);
List<Map<String, Object>> resultListMap = jdbcTemplate.queryForList(sql);
logger.info("resultListMap: " + resultListMap);
return resultListMap;
}
public List<Map<String, Object>> queryForListMap(String sql, Object... args){
logger.info("sql: " + sql);
List<Map<String, Object>> resultListMap = jdbcTemplate.queryForList(sql, args);
logger.info("resultListMap: " + resultListMap);
return resultListMap;
}
public Map<String, Object> queryForMap(String sql){
logger.info("sql: " + sql);
Map<String, Object> resultMap = jdbcTemplate.queryForMap(sql);
logger.info("resultMap: " + resultMap);
return resultMap;
}
public Map<String, Object> queryForMap(String sql, Object... args){
logger.info("sql: " + sql);
Map<String, Object> resultMap = jdbcTemplate.queryForMap(sql, args);
logger.info("resultMap: " + resultMap);
return resultMap;
}
public int update(String sql){
logger.info("sql: " + sql);
return jdbcTemplate.update(sql);
}
public int update(String sql, Object... args){
logger.info("sql: " + sql);
return jdbcTemplate.update(sql, args);
}
public int[] batchUpdate(String... sql){
logger.info("sql: " + sql);
return jdbcTemplate.batchUpdate(sql);
}
public int[] batchUpdate(String sql, List<Object[]> batchArgs){
logger.info("sql: " + sql);
return jdbcTemplate.batchUpdate(sql, batchArgs);
}
public int[] batchUpdate(String sql, BatchPreparedStatementSetter pss) {
logger.info("sql: " + sql);
return jdbcTemplate.batchUpdate(sql, pss);
}
public <T> int[][] batchUpdate(String sql, Collection<T> batchArgs, int batchSize, ParameterizedPreparedStatementSetter<T> pss) {
logger.info("sql: " + sql);
return jdbcTemplate.batchUpdate(sql, batchArgs, batchSize, pss);
}
}
NamedParameterJdbcTemplateRepository.java
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.SingleColumnRowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Map;
@Repository
public class NamedParameterJdbcTemplateRepository {
private static final Logger logger = LoggerFactory.getLogger(NamedParameterJdbcTemplateRepository.class);
@Autowired
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
//批量使用的时候可以使用SqlParameterSourceUtils创建批量参数
//SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(list.toArray());
public <T> List<T> queryForList(String sql, Class<T> tClass, SqlParameterSource args){
logger.info("sql: " + sql);
List<T> resultList = namedParameterJdbcTemplate.query(sql, args, new BeanPropertyRowMapper<>(tClass));
logger.info("resultList: " + resultList);
return resultList;
}
public <T> List<T> queryForListSingleColumn(String sql, Class<T> tClass, SqlParameterSource args){
logger.info("sql: " + sql);
List<T> resultList = namedParameterJdbcTemplate.query(sql, args, new SingleColumnRowMapper<>(tClass));
logger.info("resultList: " + resultList);
return resultList;
}
public List<Map<String, Object>> queryForListMap(String sql, SqlParameterSource args){
logger.info("sql: " + sql);
List<Map<String, Object>> resultListMap = namedParameterJdbcTemplate.queryForList(sql, args);
logger.info("resultListMap: " + resultListMap);
return resultListMap;
}
public Map<String, Object> queryForMap(String sql, SqlParameterSource args){
logger.info("sql: " + sql);
Map<String, Object> resultMap = namedParameterJdbcTemplate.queryForMap(sql, args);
logger.info("resultMap: " + resultMap);
return resultMap;
}
public <T> List<T> queryForList(String sql, Class<T> tClass, Map<String, ?> paramMap){
logger.info("sql: " + sql);
List<T> resultList = namedParameterJdbcTemplate.query(sql, paramMap, new BeanPropertyRowMapper<>(tClass));
logger.info("resultList: " + resultList);
return resultList;
}
public List<Map<String, Object>> queryForListMap(String sql, Map<String, ?> paramMap){
logger.info("sql: " + sql);
List<Map<String, Object>> resultListMap = namedParameterJdbcTemplate.queryForList(sql, paramMap);
logger.info("resultListMap: " + resultListMap);
return resultListMap;
}
public Map<String, Object> queryForMap(String sql, Map<String, ?> paramMap){
logger.info("sql: " + sql);
Map<String, Object> resultMap = namedParameterJdbcTemplate.queryForMap(sql, paramMap);
logger.info("resultMap: " + resultMap);
return resultMap;
}
public int update(String sql, Map<String, ?> paramMap) {
logger.info("sql: " + sql);
return namedParameterJdbcTemplate.update(sql, paramMap);
}
public int[] batchUpdate(String sql, Map<String, ?>[] batchValues) {
logger.info("sql: " + sql);
return namedParameterJdbcTemplate.batchUpdate(sql, batchValues);
}
}
标签:info,封装,简单,List,resultList,jdbctemplate,sql,logger,public
From: https://blog.51cto.com/lenglingx/7043981