#13 spring db: jpa - basic
This commit is contained in:
@@ -17,7 +17,8 @@ import javax.sql.DataSource;
|
||||
//@Import(JdbcTemplateV1Config.class)
|
||||
//@Import(JdbcTemplateV2Config.class)
|
||||
//@Import(JdbcTemplateV3Config.class)
|
||||
@Import(MyBatisConfig.class)
|
||||
//@Import(MyBatisConfig.class)
|
||||
@Import(JpaConfig.class)
|
||||
@SpringBootApplication(scanBasePackages = "hello.itemservice.web")
|
||||
public class ItemServiceApplication {
|
||||
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
package hello.itemservice.config;
|
||||
|
||||
import hello.itemservice.repository.ItemRepository;
|
||||
import hello.itemservice.repository.jpa.JpaItemRepository;
|
||||
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;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
|
||||
@Configuration
|
||||
@RequiredArgsConstructor
|
||||
public class JpaConfig {
|
||||
|
||||
private final EntityManager em;
|
||||
|
||||
@Bean
|
||||
public ItemService itemService() {
|
||||
return new ItemServiceV1(itemRepository());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ItemRepository itemRepository() {
|
||||
return new JpaItemRepository(em);
|
||||
}
|
||||
}
|
||||
@@ -2,11 +2,16 @@ package hello.itemservice.domain;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@Data
|
||||
@Entity
|
||||
public class Item {
|
||||
|
||||
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(name = "item_name", length = 20)
|
||||
private String itemName;
|
||||
private Integer price;
|
||||
private Integer quantity;
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
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.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.TypedQuery;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Slf4j
|
||||
@Repository
|
||||
@Transactional
|
||||
public class JpaItemRepository implements ItemRepository {
|
||||
|
||||
private final EntityManager em;
|
||||
|
||||
public JpaItemRepository(EntityManager em) {
|
||||
this.em = em;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Item save(Item item) {
|
||||
|
||||
em.persist(item);
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Long itemId, ItemUpdateDto updateParam) {
|
||||
Item findItem = em.find(Item.class, itemId);
|
||||
|
||||
findItem.setItemName(updateParam.getItemName());
|
||||
findItem.setPrice(updateParam.getPrice());
|
||||
findItem.setQuantity(updateParam.getQuantity());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Item> findById(Long id) {
|
||||
|
||||
Item item = em.find(Item.class, id);
|
||||
|
||||
return Optional.ofNullable(item);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Item> findAll(ItemSearchCond cond) {
|
||||
String jpql = "select i from Item i";
|
||||
Integer maxPrice = cond.getMaxPrice();
|
||||
String itemName = cond.getItemName();
|
||||
|
||||
if (StringUtils.hasText(itemName) || maxPrice != null) {
|
||||
jpql += " where";
|
||||
}
|
||||
|
||||
boolean andFlag = false;
|
||||
List<Object> param = new ArrayList<>();
|
||||
|
||||
if (StringUtils.hasText(itemName)) {
|
||||
jpql += " i.itemName like concat('%',:itemName,'%')";
|
||||
param.add(itemName);
|
||||
andFlag = true;
|
||||
}
|
||||
|
||||
if (maxPrice != null) {
|
||||
if (andFlag) {
|
||||
jpql += " and";
|
||||
}
|
||||
jpql += " i.price <= :maxPrice";
|
||||
param.add(maxPrice);
|
||||
}
|
||||
|
||||
log.info("jpql={}", jpql);
|
||||
|
||||
TypedQuery<Item> query = em.createQuery(jpql, Item.class);
|
||||
if (StringUtils.hasText(itemName)) {
|
||||
query.setParameter("itemName", itemName);
|
||||
}
|
||||
if (maxPrice != null) {
|
||||
query.setParameter("maxPrice", maxPrice);
|
||||
}
|
||||
return query.getResultList();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user