Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 백준
- http
- AOP
- Spring Boot
- Greedy
- Proxy
- transaction
- pointcut
- springdatajpa
- db
- Android
- 알고리즘
- 스프링 핵심 기능
- 스프링
- 그리디
- 스프링 핵심 원리
- JDBC
- 김영한
- kotlin
- QueryDSL
- Servlet
- 인프런
- SpringBoot
- spring
- Exception
- jpa
- JPQL
- java
- 자바
- Thymeleaf
Archives
- Today
- Total
개발자되기 프로젝트
['21.07.12] Movie Entity, Repository, API Controller 본문
1. 오늘의 목표
API controller를 통해 DB에 movie 객체 CRUD
2. Movie, Movie Repository 생성
<Movie class> 필드명은 NAVER API의 reponse내용을 참고했다.
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class Movie {
@Id
@GeneratedValue
private int id;
private String title;
private String link;
private String image;
private String subtitle;
private LocalDate pubDate;
private String director;
private String actor;
private String userRating;
}
<MovieRepository>
public interface MovieRepository extends JpaRepository<Movie, Integer> {
}
<Repository Test> Movie객체가 DB에 반영되고, query가 잘 동작하는지 확인해보자.
@Test
void repositoryTest(){
Movie movie = new Movie();
movie.setActor("이지은");
movie.setDirector("봉준호");
movie.setSubtitle("미정");
movieRepository.save(movie);
System.out.println(movieRepository.findAll());
movieRepository.deleteById(1);
System.out.println(movieRepository.findAll());
}
그런데 log가 하나도 안뜬다..
[Movie(id=1, title=null, link=null, image=null, subtitle=미정, pubDate=null, director=봉준호, actor=이지은, userRating=null)]
[]
- Hibernate 설정
hibernate log설정 및 초기화 설정도 같이 해주자.
<application.yml>
spring:
h2:
console:
enabled: true
jpa:
show-sql: true
properties:
hibernate:
format_sql: true
generate-ddl: true
<result> 쿼리가 정상적으로 동작하는 것을 확인했다!
Hibernate:
call next value for hibernate_sequence
Hibernate:
insert
into
movie
(actor, director, image, link, pub_date, subtitle, title, user_rating, id)
values
(?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate:
select
movie0_.id as id1_0_,
movie0_.actor as actor2_0_,
movie0_.director as director3_0_,
movie0_.image as image4_0_,
movie0_.link as link5_0_,
movie0_.pub_date as pub_date6_0_,
movie0_.subtitle as subtitle7_0_,
movie0_.title as title8_0_,
movie0_.user_rating as user_rat9_0_
from
movie movie0_
[Movie(id=1, title=null, link=null, image=null, subtitle=미정, pubDate=null, director=봉준호, actor=이지은, userRating=null)]
Hibernate:
select
movie0_.id as id1_0_0_,
movie0_.actor as actor2_0_0_,
movie0_.director as director3_0_0_,
movie0_.image as image4_0_0_,
movie0_.link as link5_0_0_,
movie0_.pub_date as pub_date6_0_0_,
movie0_.subtitle as subtitle7_0_0_,
movie0_.title as title8_0_0_,
movie0_.user_rating as user_rat9_0_0_
from
movie movie0_
where
movie0_.id=?
Hibernate:
delete
from
movie
where
id=?
Hibernate:
select
movie0_.id as id1_0_,
movie0_.actor as actor2_0_,
movie0_.director as director3_0_,
movie0_.image as image4_0_,
movie0_.link as link5_0_,
movie0_.pub_date as pub_date6_0_,
movie0_.subtitle as subtitle7_0_,
movie0_.title as title8_0_,
movie0_.user_rating as user_rat9_0_
from
movie movie0_
[]
2. API Controller
<ApiController class>
@RestController
@RequestMapping("/api")
@Slf4j
@RequiredArgsConstructor
public class ApiController {
private final MovieRepository movieRepository;
@PostMapping
public Movie addMovie(@RequestBody Movie movie){
movieRepository.save(movie);
return movie;
}
@GetMapping("/get")
public List<Movie> getFindAll(){
var result = movieRepository.findAll();
result.forEach(System.out::println);
return result;
}
@DeleteMapping("/{id}")
public void deleteMovie(@PathVariable int id){
movieRepository.deleteById(id);
}
}
API 테스트는 swagger를 활용해서 진행하겠다.
<Post>
<Get>
<Delete>
<Get>
API Controller를 통해 movie entity가
DB에 저장, 조회, 업데이트, 삭제가 정상적으로 진행되는걸 확인했다!
'Project > 영화리뷰 관리' 카테고리의 다른 글
['21.07.17] Null check, ENum (0) | 2021.07.17 |
---|---|
['21.07.15] Naver 영화검색 API (0) | 2021.07.15 |
['21.07.14] Entity, DTO 분리 (0) | 2021.07.15 |
['21.07.14] Service class생성, api test (0) | 2021.07.14 |
['21.07.13] Naver API 연동 (0) | 2021.07.13 |
Comments