[spring] API 개발 고급 준비
API 개발 고급 - 준비
등록이랑 수정은 성능 문제가 거의 발생하지 않는다.
단순한 조회이기 때문에
문제는 조회가 문제이다.
조회용 샘플 데이터 입력을 하고 서버 내렸다 올려도 남아있도록 한다
조회용 샘플 데이터 입력
총 두건의 주문을 넣는것임
@Component로 스프링이 Component스캔대상으로 만들고
스프링빈 다 엮이고 나서 마지막에 @PostConstruct가 호출
package jpabook.jpashop;
import jpabook.jpashop.domain.*;
import jpabook.jpashop.domain.item.Book;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.PostConstruct;
import javax.persistence.EntityManager;
/*
총 주문 2개
* userA
JPA1 BOOK
JPA2 BOOK
userB
SPRING1 BOOK
SPRING2 BOOK
* */
@Component
@RequiredArgsConstructor
public class InitDb {
private final InitService initService;
@PostConstruct
public void init() {
initService.dbInit1();
initService.dbInit2();
}
@Component
@Transactional
@RequiredArgsConstructor
static class InitService {
private final EntityManager em;
public void dbInit1() {
Member member = new Member();
member.setName("userA");
member.setAddress(new Address("서울", "1", "1111"));
em.persist(member);
Book book1 = new Book();
book1.setName("JPA1 BOOK");
book1.setPrice(10000);
book1.setStockQuantity(100);
em.persist(book1);
Book book2 = new Book();
book2.setName("JPA2 BOOK");
book2.setPrice(20000);
book2.setStockQuantity(100);
em.persist(book2);
OrderItem orderItem1 = OrderItem.createOrderItem(book1, 10000, 1);
OrderItem orderItem2 = OrderItem.createOrderItem(book2, 20000, 2);
Delivery delivery = new Delivery();
delivery.setAddress(member.getAddress());
Order order = Order.createOrder(member, delivery, orderItem1, orderItem2);
em.persist(order);
}
}
}
application.yml에 ddl-auto: create 로 변경했음
샘플데이터 잘 들어가있음
package jpabook.jpashop;
import jpabook.jpashop.domain.*;
import jpabook.jpashop.domain.item.Book;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.PostConstruct;
import javax.persistence.EntityManager;
/*
총 주문 2개
* userA
JPA1 BOOK
JPA2 BOOK
userB
SPRING1 BOOK
SPRING2 BOOK
* */
@Component
@RequiredArgsConstructor
public class InitDb {
private final InitService initService;
@PostConstruct
public void init() {
initService.dbInit1();
}
@Component
@Transactional
@RequiredArgsConstructor
static class InitService {
private final EntityManager em;
public void dbInit1() {
Member member = createMember("userA", "서울", "1", "1111");
em.persist(member);
Book book1 = createBook("JPA1 BOOK", 10000, 100);
em.persist(book1);
Book book2 = createBook("JPA2 BOOK", 20000, 100);
em.persist(book2);
OrderItem orderItem1 = OrderItem.createOrderItem(book1, 10000, 1);
OrderItem orderItem2 = OrderItem.createOrderItem(book2, 20000, 2);
//Delivery delivery = new Delivery();
//delivery.setAddress(member.getAddress());
//Order order = Order.createOrder(member, delivery, orderItem1, orderItem2);
Order order = Order.createOrder(member, createDelivery(member),
orderItem1, orderItem2);
em.persist(order);
}
public void dbInit2() {
Member member = createMember("userB", "진주", "2", "2222");
em.persist(member);
Book book1 = createBook("SPRING1 BOOK", 20000, 200);
em.persist(book1);
Book book2 = createBook("SPRING2 BOOK", 40000, 300);
em.persist(book2);
Delivery delivery = createDelivery(member);
OrderItem orderItem1 = OrderItem.createOrderItem(book1, 20000, 3);
OrderItem orderItem2 = OrderItem.createOrderItem(book2, 40000, 4);
Order order = Order.createOrder(member, delivery, orderItem1,
orderItem2);
em.persist(order);
}
private Member createMember(String name, String city, String street, String zipcode) {
Member member = new Member();
member.setName(name);
member.setAddress(new Address(city, street, zipcode));
return member;
}
private Book createBook(String name, int price, int stockQuantity) {
Book book = new Book();
book.setName(name);
book.setPrice(price);
book.setStockQuantity(stockQuantity);
return book;
}
private Delivery createDelivery(Member member) {
Delivery delivery = new Delivery();
delivery.setAddress(member.getAddress());
return delivery;
}
}
}
Author And Source
이 문제에 관하여([spring] API 개발 고급 준비), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@vector13/spring-API-개발-고급-준비저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)