728x90
반응형
##요구조건
:모아보기 태그를 클릭하면 상품의 사진, 이름, 가격, 최저가 정보 표시
1.Timestamped 클래스 만들기
1) 클래스
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
package com.sparta_spring.sparta_spring_week04.models;
import lombok.Getter;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.EntityListeners;
import javax.persistence.MappedSuperclass;
import java.time.LocalDateTime;
@Getter
@MappedSuperclass // 멤버 변수가 컬럼이 되도록 합니다.
@EntityListeners(AuditingEntityListener.class) // 변경되었을 때 자동으로 기록합니다.
public abstract class Timestamped {
@CreatedDate // 최초 생성 시점
private LocalDateTime createdAt;
@LastModifiedDate // 마지막 변경 시점
private LocalDateTime modifiedAt;
}
|
cs |
2) main 함수에 EnableJpaAuditing 넣어야 시간 변경 가능
1
2
3
4
5
6
7
|
@EnableJpaAuditing // 시간 자동 변경이 가능하도록 합니다.
@SpringBootApplication // 스프링 부트임을 선언합니다.
public class Week04Application {
public static void main(String[] args) {
SpringApplication.run(Week04Application.class, args);
}
}
|
cs |
##Product 클래스 만들기
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
|
package com.sparta_spring.sparta_spring_week04.models;
import lombok.Getter;
import lombok.NoArgsConstructor;
import javax.persistence.*;
@Entity // 테이블 역할
@Getter
@NoArgsConstructor // 빈 생성자
public class Product {
// ID
@GeneratedValue(strategy = GenerationType.AUTO) // 자동생성
@Id
private Long id;
// 열
// 상품 이름
@Column(nullable = false)
private String title;
// 상품 사진
@Column(nullable = false)
private String img;
// 상품 링크
@Column(nullable = false)
private String link;
// 최저가
@Column(nullable = false)
private int lprice;
// 내 상품 가격
@Column(nullable = false)
private int myprice;
}
|
cs |
##ProductRepository 만들기
: JpaRepository 상속
1
2
3
4
5
6
7
8
9
10
11
12
|
package com.sparta_spring.sparta_spring_week04.models;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.jpa.repository.JpaRepository;
public interface ProductRepository extends JpaRepository<Product,Long> {
}
|
cs |
## ProductRestController 만들기
: 컨트롤러 어토테이션, id column 들 정의
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
|
package com.sparta_spring.sparta_spring_week04.controller;
import com.sparta_spring.sparta_spring_week04.models.Product;
import com.sparta_spring.sparta_spring_week04.models.ProductRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController // json 형식 컨트롤러
@RequiredArgsConstructor // 의존주입 생성자 -> final 변수에 적용
public class ProductRestController {
private final ProductRepository productRepository;
// 상품조회한 목록 불러오기
@GetMapping("/api/products")
public List<Product> getProducts(){
return productRepository.findAll();
}
// 관심상품 등록
// @PostMapping("/api/products")
}
|
cs |
반응형
'SpringBoot > 스파르타 웹개발의 봄 spring' 카테고리의 다른 글
[스파르타 웹개발의 봄 spring] 04.08 키워드로 상품 검색하기 - NaverShopSearch 발전시키기 (0) | 2021.07.15 |
---|---|
[스파르타 웹개발의 봄 spring] 04.07 관심 상품 등록하기 (0) | 2021.07.15 |
[스파르타 웹개발의 봄 spring] 04.05 프로젝트 설계하기 (0) | 2021.07.15 |
[스파르타 웹개발의 봄 spring] 04.03 네이버 API를 ARC로 사용해보기 (0) | 2021.07.13 |
[스파르타 웹개발의 봄 spring] 04.02 네이버 API 신청하기 (0) | 2021.07.13 |