티스토리 뷰

IT/Spring

[Spring] SpringBoot + MariaDB JPA

trace90 2022. 8. 26. 16:55
JPA 적용하기

JDBC, MyBatis에 이어서 이번엔 JPA 연결해서 사용해 보겠습니다.

프로젝트 구조

 

build.gradle

//JPA
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'

 

MovieVO.java

package com.api.opendata.model.boxoffice;

import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;

@Getter
@Setter
@Entity
@Table(name="tbl_Movie")
public class MovieVO {
    @Id
    @Column
    //@GeneratedValue(strategy= GenerationType.IDENTITY)
    private int id;
    @Column(name = "repGenreNm", length = 30, nullable = true)
    private String repGenreNm;
    @Column(name = "movieNm", length = 300, nullable = true)
    private String movieNm;
}

 

BoxOfficeRepository.java

package com.api.opendata.dao;

import org.springframework.stereotype.Repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.api.opendata.model.boxoffice.MovieVO;

@Repository
public interface BoxOfficeRepository extends JpaRepository<MovieVO, String> {
}

 

BoxOfficeService.java

package com.api.opendata.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.api.opendata.common.util.Utility;
import com.api.opendata.model.boxoffice.*;
import com.api.opendata.process.BoxOffice;
import com.api.opendata.dao.BoxOfficeRepository;
import java.util.HashMap;
import java.util.List;

@Service
public class BoxOfficeService {    
    @Autowired
    private BoxOfficeRepository boxOfficeRepository;

    public HashMap<String, String> RunSearch(String targetDt)
    {        
        HashMap<String, String> result = new HashMap<String, String>();

        try{
        
            //MovieList JPA
            List<MovieVO> movieList = boxOfficeRepository.findAll();
            result.put("MovieListJPA", MovieListVOCard(movieList));
            
        }catch (Exception e){
            result.put("Error", e.getMessage());
        }

        return result;
    }

    public String MovieListVOCard(List<MovieVO> movieList){
        StringBuffer movieListCard = new StringBuffer();

        for(MovieVO movie: movieList){
            movieListCard.append(movie.getId() + " | " + movie.getRepGenreNm() + " | " + movie.getMovieNm());

            movieListCard.append(System.lineSeparator());
        }

        return movieListCard.toString();
    }
}

 

자! 여기까지 잘 따라오셨다면 확인해 봐야겠죠?

이전에 만들어두었던 Controller로 호출해보겠습니다.

 

http://localhost:8080/api/boxoffice/search?targetDt=20220815

MovieVO에 테이블명도 컬럼명도 명시적으로 적어두었는데...

"movie_nm"?? 컬럼명을 찾을수 없다는 에러가 발생 하게됩니다.

 

Hibernate 명명규칙이 있는데 내부적으로 명칭을 바꿔버리게 되므로, 아래 설정을 통해 명시한대로 호출하도록 해보겠습니다.

 

application.properties

spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

 

이때!! 참고할점이 한가지 더있습니다.

MariaDB의 경우 설치시 설정 한게 없다면 "lower_case_table_names = 0"으로 되어있으실텐데요.

MariaDB에서도 대/소문자 구분을 하기때문에 환경에 맞춰 변경이 필요하다면 변경해주어야 합니다.

 

/etc/mysql/my.cnf

  • lower_case_table_names = 0
    • UNIX 기반 시스템의 기본값
    • 테이블이름, 별명 및 데이터베이스 이름이 대소문자를 구분하여 비교
  • lower_case_table_names = 1
    • Windows의 기본값
    • 테이블이름, 별명 및 데이터베이스 이름이 소문자로 저장되며 대소문자를 구분하지 않습니다.
  • lower_case_table_names = 2
    • Windows의 기본값
    • 테이블이름, 별명 및 데이터베이스 이름이 선언된대로 저장되지만 소문자로 비교합니다.

 

다시 호출하여 결과가 잘 나오는지 확인 해보겠습니다.

http://localhost:8080/api/boxoffice/search?targetDt=20220815

응답 결과

 

댓글
최근에 올라온 글
TAG
more
글 보관함
«   2024/05   »
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