Notice
Recent Posts
Recent Comments
Link
«   2025/01   »
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
Archives
Today
Total
관리 메뉴

개발자되기 프로젝트

['21.07.12] Movie Entity, Repository, API Controller 본문

Project/영화리뷰 관리

['21.07.12] Movie Entity, Repository, API Controller

Seung__ 2021. 7. 12. 22:33

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설정 및 초기화 설정도 같이 해주자.

 

 

JPA/Hibernate 초기화(ddl-auto, initialization-mode 등)

0. Hibernate JPA의 구현체로 jpa를 한번 감싸서 사용하기 쉽게 해줌 JPA 소개 1. ORM(Object Relational Mapping)  객체와 관계형 Database를 자동으로 mapping시켜주는 것을 말한다.  객체지향 프로그래밍의..

bsh-developer.tistory.com

<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를 활용해서 진행하겠다.

 

Swagger

Swagger : 개발한 REST API를 편리하게 문서화 하여 관리 및 제3 사용자가 편하게 API를 호출해보고 테스트 할 수 있는 프로젝트 SpringBoot는 springfox-boot-starter를 아래와 같이 gradle dependencies에 추가..

bsh-developer.tistory.com

 

Swagger 설정

1. @Api 기존에 작성한 controller에 @Api(tags = {""}를 추가하자. ""안에 간단한 tag를 입력할 수 있는데. swagger ui에 보여지는 이름?이라고 보면 된다. @Api(tags = {"API 정보를 제공하는 Controller"}) @Re..

bsh-developer.tistory.com

 

<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