Last active
July 17, 2018 01:59
-
-
Save SeokRae/174072ef4e097e04be4a1e054d218fb3 to your computer and use it in GitHub Desktop.
NamedParameterJdbcTemplate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Repository | |
public class ProductDao { | |
private NamedParameterJdbcTemplate jdbc; | |
private RowMapper<Product> rowMapper = BeanPropertyRowMapper.newInstance(Product.class); | |
/** | |
* 생성자를 통한 DI주입 | |
* | |
* @param dataSource | |
*/ | |
public ProductDao(DataSource dataSource) { | |
this.jdbc = new NamedParameterJdbcTemplate(dataSource); | |
this.insertAction = new SimpleJdbcInsert(dataSource).withTableName("Product").usingGeneratedKeyColumns("id"); | |
} | |
/** | |
* product 전체 개수 | |
* @return | |
*/ | |
public int selectCount() { | |
System.out.println(); | |
return jdbc.queryForObject(SELECT_TOTAL_COUNT, Collections.emptyMap(), Integer.class); | |
} | |
/** | |
* 데이터 출력 3, 4개씩 | |
* @param start | |
* @return | |
*/ | |
public List<Product> selectAll(Integer start) { | |
Map<String, Integer> paramMap = new HashMap<>(); | |
paramMap.put("start", start); | |
paramMap.put("limit", 4); | |
return jdbc.query(SELECT_PRODUCT_MORE, paramMap, rowMapper); | |
} | |
/** | |
* 데이터 하나만 출력 | |
* @param id | |
* @return | |
*/ | |
public Product selectItem(Integer id) { | |
return jdbc.queryForObject(SELECT_PRODUCT_ONE, new MapSqlParameterSource("id", id), rowMapper); | |
} | |
/** | |
* 카테고리, 상품 목록 구하기 | |
* @param category_id | |
* @param start | |
* @param limit | |
* @return | |
*/ | |
public List<Product> selectCategoryAll(Integer category_id, Integer start) { | |
Map<String, Integer> paramMap = new HashMap<>(); | |
paramMap.put("category_id", category_id); | |
paramMap.put("start", start); | |
paramMap.put("limit", 4); | |
return jdbc.query(SELECT_CATEGORY_PRODUCT_LIST, paramMap, rowMapper); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment