#13 spring db: mybatis - basic
This commit is contained in:
@@ -16,7 +16,8 @@ import javax.sql.DataSource;
|
|||||||
//@Import(MemoryConfig.class)
|
//@Import(MemoryConfig.class)
|
||||||
//@Import(JdbcTemplateV1Config.class)
|
//@Import(JdbcTemplateV1Config.class)
|
||||||
//@Import(JdbcTemplateV2Config.class)
|
//@Import(JdbcTemplateV2Config.class)
|
||||||
@Import(JdbcTemplateV3Config.class)
|
//@Import(JdbcTemplateV3Config.class)
|
||||||
|
@Import(MyBatisConfig.class)
|
||||||
@SpringBootApplication(scanBasePackages = "hello.itemservice.web")
|
@SpringBootApplication(scanBasePackages = "hello.itemservice.web")
|
||||||
public class ItemServiceApplication {
|
public class ItemServiceApplication {
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package hello.itemservice.config;
|
||||||
|
|
||||||
|
import hello.itemservice.repository.ItemRepository;
|
||||||
|
import hello.itemservice.repository.mybatis.ItemMapper;
|
||||||
|
import hello.itemservice.repository.mybatis.MyBatisItemRepository;
|
||||||
|
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;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class MyBatisConfig {
|
||||||
|
|
||||||
|
private final ItemMapper itemMapper;
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ItemService itemService() {
|
||||||
|
return new ItemServiceV1(itemRepository());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ItemRepository itemRepository() {
|
||||||
|
return new MyBatisItemRepository(itemMapper);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package hello.itemservice.repository.mybatis;
|
||||||
|
|
||||||
|
import hello.itemservice.domain.Item;
|
||||||
|
import hello.itemservice.repository.ItemSearchCond;
|
||||||
|
import hello.itemservice.repository.ItemUpdateDto;
|
||||||
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
import org.apache.ibatis.annotations.Param;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@Mapper
|
||||||
|
public interface ItemMapper {
|
||||||
|
|
||||||
|
void save(Item item);
|
||||||
|
|
||||||
|
void update(@Param("id") Long id,
|
||||||
|
@Param("updateParam")ItemUpdateDto updateParam);
|
||||||
|
|
||||||
|
List<Item> findAll(ItemSearchCond itemSearchCond);
|
||||||
|
|
||||||
|
Optional<Item> findById(Long id);
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package hello.itemservice.repository.mybatis;
|
||||||
|
|
||||||
|
import hello.itemservice.domain.Item;
|
||||||
|
import hello.itemservice.repository.ItemRepository;
|
||||||
|
import hello.itemservice.repository.ItemSearchCond;
|
||||||
|
import hello.itemservice.repository.ItemUpdateDto;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class MyBatisItemRepository implements ItemRepository {
|
||||||
|
|
||||||
|
private final ItemMapper itemMapper;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Item save(Item item) {
|
||||||
|
itemMapper.save(item);
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void update(Long itemId, ItemUpdateDto updateParam) {
|
||||||
|
itemMapper.update(itemId, updateParam);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Optional<Item> findById(Long id) {
|
||||||
|
return itemMapper.findById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Item> findAll(ItemSearchCond cond) {
|
||||||
|
return itemMapper.findAll(cond);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,4 +9,6 @@ spring.datasource.username=sa
|
|||||||
#Mybatis
|
#Mybatis
|
||||||
mybatis.type-aliases-package=hello.itemservice.domain
|
mybatis.type-aliases-package=hello.itemservice.domain
|
||||||
mybatis.configuration.map-underscore-to-camel-case=true
|
mybatis.configuration.map-underscore-to-camel-case=true
|
||||||
logging.level.hello.itemservice.repository.mybatis=trace
|
logging.level.hello.itemservice.repository.mybatis=trace
|
||||||
|
|
||||||
|
mybatis.mapper-locations=classpath:mapper/**/*.xml
|
||||||
|
|||||||
36
spring-db/src/main/resources/mapper/ItemMapper.xml
Normal file
36
spring-db/src/main/resources/mapper/ItemMapper.xml
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||||
|
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||||
|
<mapper namespace="hello.itemservice.repository.mybatis.ItemMapper">
|
||||||
|
|
||||||
|
<insert id="save" useGeneratedKeys="true" keyProperty="id">
|
||||||
|
insert into item (item_name, price, quantity)
|
||||||
|
values (#{itemName}, #{price}, #{quantity})
|
||||||
|
</insert>
|
||||||
|
<update id="update">
|
||||||
|
update item
|
||||||
|
set item_name=#{updateParam.itemName},
|
||||||
|
price=#{updateParam.price},
|
||||||
|
quantity=#{updateParam.quantity}
|
||||||
|
where id = #{id}
|
||||||
|
</update>
|
||||||
|
<select id="findById" resultType="Item">
|
||||||
|
select id, item_name, price, quantity
|
||||||
|
from item
|
||||||
|
where id = #{id}
|
||||||
|
</select>
|
||||||
|
<select id="findAll" resultType="Item">
|
||||||
|
select id, item_name, price, quantity
|
||||||
|
from item
|
||||||
|
<where>
|
||||||
|
<if test="itemName != null and itemName != ''">
|
||||||
|
and item_name like concat('%',#{itemName},'%')
|
||||||
|
</if>
|
||||||
|
<if test="maxPrice != null">
|
||||||
|
and price <= #{maxPrice}
|
||||||
|
</if>
|
||||||
|
</where>
|
||||||
|
</select>
|
||||||
|
|
||||||
|
|
||||||
|
</mapper>
|
||||||
@@ -9,4 +9,6 @@ logging.level.org.springframework.jdbc=debug
|
|||||||
#Mybatis
|
#Mybatis
|
||||||
mybatis.type-aliases-package=hello.itemservice.domain
|
mybatis.type-aliases-package=hello.itemservice.domain
|
||||||
mybatis.configuration.map-underscore-to-camel-case=true
|
mybatis.configuration.map-underscore-to-camel-case=true
|
||||||
logging.level.hello.itemservice.repository.mybatis=trace
|
logging.level.hello.itemservice.repository.mybatis=trace
|
||||||
|
|
||||||
|
mybatis.mapper-locations=classpath:mapper/**/*.xml
|
||||||
Reference in New Issue
Block a user