#13 spring db: spring data jpa - basic
This commit is contained in:
@@ -8,6 +8,8 @@ group = 'com.example'
|
||||
version = '0.0.1-SNAPSHOT'
|
||||
sourceCompatibility = '11'
|
||||
|
||||
ext["hibernate.version"] = "5.6.5.Final"
|
||||
|
||||
configurations {
|
||||
compileOnly {
|
||||
extendsFrom annotationProcessor
|
||||
|
||||
@@ -18,7 +18,8 @@ import javax.sql.DataSource;
|
||||
//@Import(JdbcTemplateV2Config.class)
|
||||
//@Import(JdbcTemplateV3Config.class)
|
||||
//@Import(MyBatisConfig.class)
|
||||
@Import(JpaConfig.class)
|
||||
//@Import(JpaConfig.class)
|
||||
@Import(SpringDataJpaConfig.class)
|
||||
@SpringBootApplication(scanBasePackages = "hello.itemservice.web")
|
||||
public class ItemServiceApplication {
|
||||
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package hello.itemservice.config;
|
||||
|
||||
import hello.itemservice.repository.ItemRepository;
|
||||
import hello.itemservice.repository.jpa.JpaItemRepositoryV2;
|
||||
import hello.itemservice.repository.jpa.SpringDataJpaItemRepository;
|
||||
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 SpringDataJpaConfig {
|
||||
|
||||
private final SpringDataJpaItemRepository springDataJpaItemRepository;
|
||||
|
||||
@Bean
|
||||
public ItemService itemService() {
|
||||
return new ItemServiceV1(itemRepository());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ItemRepository itemRepository() {
|
||||
return new JpaItemRepositoryV2(springDataJpaItemRepository);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package hello.itemservice.repository.jpa;
|
||||
|
||||
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 org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Repository
|
||||
@Transactional
|
||||
@RequiredArgsConstructor
|
||||
public class JpaItemRepositoryV2 implements ItemRepository {
|
||||
|
||||
private final SpringDataJpaItemRepository repository;
|
||||
|
||||
@Override
|
||||
public Item save(Item item) {
|
||||
return repository.save(item);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Long itemId, ItemUpdateDto updateParam) {
|
||||
Item findItem = repository.findById(itemId).orElseThrow();
|
||||
|
||||
findItem.setItemName(updateParam.getItemName());
|
||||
findItem.setPrice(updateParam.getPrice());
|
||||
findItem.setQuantity(updateParam.getQuantity());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Item> findById(Long id) {
|
||||
return repository.findById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Item> findAll(ItemSearchCond cond) {
|
||||
String itemName = cond.getItemName();
|
||||
Integer maxPrice = cond.getMaxPrice();
|
||||
|
||||
if (StringUtils.hasText(itemName) && maxPrice != null) {
|
||||
// return repository.findByItemNameLikeAndPriceLessThanEqual("%" + itemName + "%", maxPrice);
|
||||
return repository.findItems("%" + itemName + "%", maxPrice);
|
||||
} else if (StringUtils.hasText(itemName)) {
|
||||
return repository.findByItemNameLike("%" + itemName + "%");
|
||||
} else if (maxPrice != null) {
|
||||
return repository.findByPriceLessThanEqual(maxPrice);
|
||||
} else {
|
||||
return repository.findAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package hello.itemservice.repository.jpa;
|
||||
|
||||
import hello.itemservice.domain.Item;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface SpringDataJpaItemRepository extends JpaRepository<Item, Long> {
|
||||
|
||||
List<Item> findByItemNameLike(String itemName);
|
||||
|
||||
List<Item> findByPriceLessThanEqual(Integer price);
|
||||
|
||||
// 쿼리 메서드
|
||||
List<Item> findByItemNameLikeAndPriceLessThanEqual(String itemName, Integer price);
|
||||
|
||||
// 쿼리 직접 실행
|
||||
@Query("select i from Item i where i.itemName like :itemName and i.price <= :price")
|
||||
List<Item> findItems(@Param("itemName") String itemName,
|
||||
@Param("price") Integer price);
|
||||
}
|
||||
Reference in New Issue
Block a user