Git Product home page Git Product logo

jwt_jpa's Introduction

jwt_jpa

이번에는 Member 테이블을 만들어서 회원 관련된 작업을 해보려 합니다. 회원의 로그인 관리를 위해 JWT를 사용할 것이고, DB 연동은 MyBatis가 아니라 JPA로 해볼 것입니다.

작업 환경은 다음과 같습니다.

  • Tool: IntelliJ Ultimate / MySQL Workbench
  • Version: JDK 11 / Spring Boot 2.7.12 (Gradle) / MySQL 8.0.32

JWT를 적용시키기 전에, JPA 연동과 테스트코드 작성을 먼저 했습니다. (2023.06.27)

설정

  • Gradle 적용 : 지난 board 토이프로젝트에서는 Maven으로 빌드했었는데, 이번에는 Gradle로 했습니다. Maven은 익숙했습니다. pom.xml로 라이브러리를 가져오고, mvnrepository 사이트에서 필요한 라이브러리들을 가져오는 것도 괜찮았습니다. 그러다 Gradle의 빌드 성능이 좋다는 말을 듣고 처음 적용시켰습니다. Gradle은 유연함과 성능에 초점을 둔 오픈소스 빌드도구입니다. 이전의 빌드 도구였던 Ant와 Maven을 보완하여 나온만큼 장점이 많습니다. 먼저 Gradle과 Maven의 성능 차이를 보겠습니다.

(출처 : coco3o)

성능이 좋을 뿐더러 가독성도 좋습니다. 그럼 Gradle-Groovy와 Gradle-Kotlin 중에는 무엇이 더 좋을까요? Kotlin이 자동완성, 컴파일 오류 잡기, 빠른 문서보기, 리팩터링에는 더 편하다고 합니다. 참고

  • Spring Boot 2.XX : 버전 3부터는 JDK 17이 기본이라고 하여, 11을 쓰기에 2점대를 골랐습니다.
  • Jar 와 War : Jar는 Java 프로젝트를 압축한 파일이고, War은 웸 어플리케이션 압축 파일입니다. War가 사이즈가 더 크고, 그만큼 사전에 정의된 구조가 있습니다. War 파일을 실행하려면 웹서버 또는 WAS가 필요합니다. Spring Boot 에서 가이드 하는 표준은 Jar이며, Jar만으로도 충분하기에 Jar을 선택했습니다.
  • Java 11
  • Dependencies : 어떤 기능이 필요한지에 따라 추가하는 라이브러리입니다.
    • Lombok:
      • RequiredArgsConstructor: [ final을 추가한 멤버에 Bean을 주입해준다. ]
      • Getter / Setter / Data: [ 기본으로 작성하는 메소드들을 대신 작성해준다. ]
      • Slf4j: [ 로그를 찍도록 도와준다. ]
    • Spring Boot Dev Tools:
      • spring-boot-autoconfigure
      • spring-context
        • spring-aop/beans/core
      • spring-core
    • Spring Web: (spring-boot-starter-web)
      • spring-boot-starter-json: [ JSON ]
        • jackson-databind: [ 데이터 바인드 ]
      • spring-boot-starter-tomcat: [ 톰캣 ]
      • spring-web/webmvc: [ 웹 MVC ]
      • spring-boot-starter-test
        • assertj-core: [ 어썰트J ]
        • junit-jupiter: [ JUnit ]
        • mockito-core: [ 모키토 ]
        • mockito-junit-core
        • jsonassert
        • spring-test
        • xmlunit-core
    • MySQL Driver:
      • mysql-connector-j: [ MySQL 커넥터 ]
    • Spring Data JPA:
      • persistence-api: [ 영속성 ]
      • transaction-api: [ 트랜잭션 ]
      • hibernate-core: [ 하이버네이트 ]
      • spring-boot-starter-aop
      • spring-boot-starter-jdbc
        • HikariCP: [ 히카리 커넥션 풀링 ]
        • spring-jdbc: [ JDBC ]
      • spring-data-jpa
        • slf4j-api: [ SLF4J 로깅 ]
        • spring-data-commons
      • spring-aspect
        • aspectweaver
    • Java Mail Sender: (spring-boot-starter-mail)
      • jakarta.mail: [ 메일 ]
      • spring-context-support

스프링 부트 파일을 만들고 바로 실행하면 dataSource가 정의되지 않았다는 에러메세지가 나온다. 그러므로 DB 연결을 먼저 해줘야 한다.

src/main/resources/application.yml 파일에서 설정해주자

이제 MVC 패턴을 적용시키려고 한다. 어떻게 구성해야 할까? 지금까지 해왔던 프로젝트는 JDBC만 사용하거나 MyBatis를 활용했다.

JPA

DB 연결 부분을 JPA로 갈아끼워보자.

JPA 엔티티의 생명주기는 다음과 같다.

EntityManager가 엔티티를 생성한다. 이 코드에서는 Spring Data JPA의 도움으로 인스턴스를 주입받아 생성된다. 결합도가 떨어져서 유지보수성이 증가한다. 그 후, persist(), find(), createQuery() 등의 메소드를 호출하는 것이 JPA를 사용하는 것이다. Reposiroty 파일을 보면 직관적으로 알 수 있다.


테스트코드

코드

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.