일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 |
Tags
- BFS
- Java
- appendleft
- pypy3
- 소수판별
- 백준
- 파이썬
- 합 구하기
- Python
- 1일1솔
- python3
- C#강의
- c#
- unity
- 연관관계
- 브루투포스
- 프로그래머스
- spring
- 소수찾기
- deque
- 누적합
- DP
- popleft
- mvc
- 그리디 알고리즘
- 인프런
- LCM
- 우선순위큐
- 완전탐색
- JPA
Archives
- Today
- Total
jae_coding
[Spring Project] 상품 도메인 개발 본문
반응형
목차
- 상품 엔티티 비지니스로직 추가하기
- 상품 Repository
- 상품 Service
1. 상품 엔티티 비지니스 로직 추가하기
package jpabook.jpashop.domain.Item;
import jpabook.jpashop.domain.Category;
import jpabook.jpashop.exception.NotEnoughStockException;
import lombok.Getter;
import lombok.Setter;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Entity
@Getter
@Setter
//상속관계 전략
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="dtype")
public abstract class Item {
@Id @GeneratedValue
@Column(name = "item_id")
private Long id;
private String name;
private int price;
private int stockQuantity;
@ManyToMany(mappedBy = "items")
private List<Category> categories = new ArrayList<>();
//==비지니스 로직 ==//
//재고 수량 증가
private void addStock(int Quantity){
this.stockQuantity += Quantity;
}
//재고 수량 감소
private void removeStock(int Quantity){
int restStock = this.stockQuantity - Quantity;
if(restStock < 0){
throw new NotEnoughStockException("need more stock");
}
this.stockQuantity = restStock;
}
}
Exception 추가하기
package jpabook.jpashop.exception;
public class NotEnoughStockException extends RuntimeException{
public NotEnoughStockException() {
super();
}
public NotEnoughStockException(String message) {
super(message);
}
public NotEnoughStockException(String message, Throwable cause) {
super(message, cause);
}
public NotEnoughStockException(Throwable cause) {
super(cause);
}
}
2. 상품 Repository
package jpabook.jpashop.repository;
import jpabook.jpashop.domain.Item.Item;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;
import javax.persistence.EntityManager;
import java.util.List;
@Repository
@RequiredArgsConstructor
public class ItemRepository {
private final EntityManager em;
// 아이템 저장
public void save(Item item){
if(item.getId() == null){
em.persist(item);
} else{
em.merge(item); //update와 비슷하다.
}
}
// 아이템 1개 조회
public Item findOne(Long id){
return em.find(Item.class, id);
}
// 전체 아이템 조회
public List<Item> findAll(){
return em.createQuery("select i from Item i", Item.class)
.getResultList();
}
}
3. 상품 Service
package jpabook.jpashop.service;
import jpabook.jpashop.domain.Item.Item;
import jpabook.jpashop.repository.ItemRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class ItemService {
private final ItemRepository itemRepository;
@Transactional //쓰기모드
// 아이템 저장하기
public void saveItem(Item item){
itemRepository.save(item);
}
//전체 아이템 찾기
public List<Item> findItems(){
return itemRepository.findAll();
}
//한가지 아이템 찾기
public Item findOne(Long itemId){
return itemRepository.findOne(itemId);
}
}
반응형
'Spring, java > Spring_Project' 카테고리의 다른 글
[Spring Project] 웹계층 개발 (0) | 2022.08.24 |
---|---|
[Spring Project] 주문 도메인 개발 (0) | 2022.08.23 |
[Spring Project] 회원 관리 및 테스트 (0) | 2022.08.22 |
[Spring Project] 애플리케이션 아키텍쳐 (0) | 2022.08.22 |
[Spring Project] 엔티티 설계 주의사항 (0) | 2022.08.22 |
Comments