728x90
반응형
##인텔리제이에서 git 생성
1.VCS > Create Git Repository
2.Preferences > Commit > Use non-modal commit interface 체크박스 해제
3. commit 하기
##DB 의 id, pw 입력 application.properties 에서 한번에 설정
1
2
3
4
|
spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:springcoredb
spring.datasource.username=내가 설정한 아이디
spring.datasource.password=내가 설정한 비밀번호
|
cs |
##패키지로 클래스 구분하기
-controller
-service
-repository
-dto
-util :
-model:
##BeanConfiguration 사용하지 않고 스프링 어노테이션으로 자동 생성 기능 사용
1. ProductService
-@Service, @Transactional 로 자동생성 기능 사용하기
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
package com.sparta_spring.sparta_springcore_week01.service;
import com.sparta_spring.sparta_springcore_week01.model.Product;
import com.sparta_spring.sparta_springcore_week01.dto.ProductMypriceRequestDto;
import com.sparta_spring.sparta_springcore_week01.dto.ProductRequestDto;
import com.sparta_spring.sparta_springcore_week01.repository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.sql.SQLException;
import java.util.List;
@Service
public class ProductService {
private final ProductRepository productRepository;
private static final int MIN_PRICE = 100;
// DI의존주입 ProductRepository 재활용
//생성자
@Autowired // DI 받는것 표시
public ProductService(ProductRepository productRepository){
this.productRepository = productRepository;
}
public List<Product> getProducts(){
return productRepository.findAll();
}
@Transactional // 메소드 동작이 SQL 쿼리문임을 선언합니다.
public Product createProduct(ProductRequestDto requestDto) {
// 요청받은 DTO 로 DB에 저장할 객체 만들기
Product product = new Product(requestDto);
productRepository.save(product);
return product;
}
@Transactional
public Product updateProduct(Long id, ProductMypriceRequestDto requestDto) {
Product product = productRepository.findById(id).orElseThrow(
() -> new NullPointerException("해당 아이디가 존재하지 않습니다.")
);
// 변경될 관심 가격이 유효한지 확인합니다.
int myPrice = requestDto.getMyprice();
if (myPrice < MIN_PRICE) {
throw new IllegalArgumentException("유효하지 않은 관심 가격입니다. 최소 " + MIN_PRICE + " 원 이상으로 설정해 주세요.");
}
product.updateMyPrice(requestDto);
return product;
}
}
|
cs |
2.ProductRepository
-JpaRepository 상속받아 사용하기
1
2
3
4
5
6
7
8
9
10
|
package com.sparta_spring.sparta_springcore_week01.repository;
import com.sparta_spring.sparta_springcore_week01.model.Product;
import org.springframework.data.jpa.repository.JpaRepository;
public interface ProductRepository extends JpaRepository<Product,Long> {
}
|
cs |
반응형
'SpringBoot > 스파르타 스프링 심화' 카테고리의 다른 글
[스파르타 스프링 심화] 02.03. 쿠키와 세션 (0) | 2021.08.13 |
---|---|
[스파르타 스프링 심화] 02.02 웹의 인증(Authentication ) 및 인가(Authorization) (0) | 2021.08.13 |
[스파르타 스프링 심화] 01.16 스프링 IoC 컨테이너 사용하기 (0) | 2021.08.12 |
[스파르타 스프링 심화] 01.15 DI (dependency Injection,의존성 주입) 사용하기 , Ioc(제어의 역전) (0) | 2021.08.12 |
[스파르타 스프링 심화] 01.14. DI (의존성 주입)의 이해 (0) | 2021.08.12 |