#13 spring db: JdbcTemplate - NamedParameterJdbcTemplate
This commit is contained in:
@@ -10,7 +10,8 @@ import org.springframework.context.annotation.Profile;
|
||||
|
||||
|
||||
//@Import(MemoryConfig.class)
|
||||
@Import(JdbcTemplateV1Config.class)
|
||||
//@Import(JdbcTemplateV1Config.class)
|
||||
@Import(JdbcTemplateV2Config.class)
|
||||
@SpringBootApplication(scanBasePackages = "hello.itemservice.web")
|
||||
public class ItemServiceApplication {
|
||||
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package hello.itemservice.config;
|
||||
|
||||
import hello.itemservice.repository.ItemRepository;
|
||||
import hello.itemservice.repository.jdbctemplate.JdbcTemplateItemRepositoryV2;
|
||||
import hello.itemservice.service.ItemService;
|
||||
import hello.itemservice.service.ItemServiceV1;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
@Configuration
|
||||
@RequiredArgsConstructor
|
||||
public class JdbcTemplateV2Config {
|
||||
|
||||
private final DataSource dataSource;
|
||||
|
||||
@Bean
|
||||
public ItemService itemService() {
|
||||
return new ItemServiceV1(itemRepository());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ItemRepository itemRepository() {
|
||||
return new JdbcTemplateItemRepositoryV2(dataSource);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
package hello.itemservice.repository.jdbctemplate;
|
||||
|
||||
import hello.itemservice.domain.Item;
|
||||
import hello.itemservice.repository.ItemRepository;
|
||||
import hello.itemservice.repository.ItemSearchCond;
|
||||
import hello.itemservice.repository.ItemUpdateDto;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.dao.EmptyResultDataAccessException;
|
||||
import org.springframework.jdbc.core.BeanPropertyRowMapper;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
|
||||
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
|
||||
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
|
||||
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
|
||||
import org.springframework.jdbc.support.GeneratedKeyHolder;
|
||||
import org.springframework.jdbc.support.KeyHolder;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* NamedParameterJdbcTemplate
|
||||
*
|
||||
* SqlParameterSource
|
||||
* - BeanPropertySqlParameterSource
|
||||
* - MapSqlParameterSource
|
||||
*
|
||||
* Map
|
||||
*
|
||||
* BeanPropertyRowMapper
|
||||
*/
|
||||
@Slf4j
|
||||
public class JdbcTemplateItemRepositoryV2 implements ItemRepository {
|
||||
|
||||
// private final JdbcTemplate jdbcTemplate;
|
||||
private final NamedParameterJdbcTemplate jdbcTemplate;
|
||||
|
||||
public JdbcTemplateItemRepositoryV2(DataSource dataSource) {
|
||||
this.jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Item save(Item item) {
|
||||
String sql =
|
||||
"insert into item(item_name, price, quantity) values (:itemName, :price, :quantity)";
|
||||
|
||||
SqlParameterSource param = new BeanPropertySqlParameterSource(item);
|
||||
|
||||
KeyHolder keyHolder = new GeneratedKeyHolder();
|
||||
jdbcTemplate.update(sql, param, keyHolder);
|
||||
|
||||
long key = keyHolder.getKey().longValue();
|
||||
item.setId(key);
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Long itemId, ItemUpdateDto updateParam) {
|
||||
String sql =
|
||||
"update item set item_name=:itemName, price=:price, quantity=:quantity where id=:id";
|
||||
|
||||
SqlParameterSource param = new MapSqlParameterSource()
|
||||
.addValue("itemName", updateParam.getItemName())
|
||||
.addValue("price", updateParam.getPrice())
|
||||
.addValue("quantity", updateParam.getQuantity())
|
||||
.addValue("id", itemId);
|
||||
|
||||
jdbcTemplate.update(sql, param);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Item> findById(Long id) {
|
||||
String sql =
|
||||
"select id, item_name, price, quantity from item where id = :id";
|
||||
|
||||
try {
|
||||
Map<String, Object> param = Map.of("id", id);
|
||||
Item item = jdbcTemplate.queryForObject(sql, param, itemRowMapper());
|
||||
return Optional.of(item);
|
||||
} catch (EmptyResultDataAccessException e) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private RowMapper<Item> itemRowMapper() {
|
||||
return BeanPropertyRowMapper.newInstance(Item.class); // camel 변환 지원
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Item> findAll(ItemSearchCond cond) {
|
||||
String itemName = cond.getItemName();
|
||||
Integer maxPrice = cond.getMaxPrice();
|
||||
|
||||
SqlParameterSource param = new BeanPropertySqlParameterSource(cond);
|
||||
|
||||
String sql = "select id, item_name, price, quantity from item";
|
||||
|
||||
// 동적 쿼리
|
||||
if (StringUtils.hasText(itemName) || maxPrice != null) {
|
||||
sql += " where";
|
||||
}
|
||||
boolean andFlag = false;
|
||||
if (StringUtils.hasText(itemName)) {
|
||||
sql += " item_name like concat('%',:itemName,'%')";
|
||||
andFlag = true;
|
||||
}
|
||||
if (maxPrice != null) {
|
||||
if (andFlag) {
|
||||
sql += " and";
|
||||
}
|
||||
sql += " price <= :maxPrice";
|
||||
}
|
||||
log.info("sql={}", sql);
|
||||
return jdbcTemplate.query(sql, param, itemRowMapper());
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
spring.profiles.active=test
|
||||
|
||||
#jdbcTemplate sql log
|
||||
#logging.level.org.springframework.jdbc=debug
|
||||
logging.level.org.springframework.jdbc=debug
|
||||
Reference in New Issue
Block a user