프로젝트 환경설정

프로젝트 생성

Spring Initializr 에서 쉽게 프로젝트를 생성할 수 있다.

  • Gradle

  • Java

  • Spring Boot 2.1.7

  • groupId: jpabook

  • artifactId: jpashop

  • web, thymeleaf, jpa, h2, lombok, validation

라이브러리 살펴보기

gradle 의존 관계 보기

./gradlew dependencies —configuration compileClasspath
plugins {
    id 'org.springframework.boot' version '2.4.3'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}

group = 'jpabook'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    // 버전이 없는 라이브러리는 스프링 부트 버전과 궁합이 맞는 것을 자동으로 가져온다.
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation 'org.springframework.boot:spring-boot-starter-validation'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-devtools'
    compileOnly 'org.projectlombok:lombok'
    runtimeOnly 'com.h2database:h2'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation("org.junit.vintage:junit-vintage-engine") {
        exclude group: "org.hamcrest", module: "hamcrest-core"
    }
}

test {
    useJUnitPlatform()
}

스프링 부트 라이브러리

  • spring-boot-starter-web

    • spring-boot-starter-tomcat

      • 톰캣 (웹서버)

    • spring-webmvc

      • 스프링 웹 MVC

  • spring-boot-starter-thymeleaf

    • 타임리프 템플릿 엔진(View)

  • spring-boot-starter-data-jpa

  • spring-boot-starter-aop

  • spring-boot-starter-jdbc

    • HikariCP 커넥션 풀 (부트 2.0 기본)

    • hibernate + JPA

      • 하이버네이트 + JPA

    • spring-data-jpa

      • 스프링 데이터 JPA

  • spring-boot-starter(공통)

    • 스프링 부트 + 스프링 코어 + 로깅

    • spring-boot

      • spring-core

    • spring-boot-starter-logging

      • logback, slf4j

테스트 라이브러리

  • spring-boot-starter-test

    • junit

      • 테스트 프레임워크

    • mockito

      • 목 라이브러리

    • assertj

      • 테스트 코드를 좀 더 편하게 작성하게 도와주는 라이브러리

    • spring-test

      • 스프링 통합 테스트 지원

  • 핵심 라이브러리

    • 스프링 MVC

    • 스프링 ORM

    • JPA, 하이버네이트

    • 스프링 데이터 JPA

  • 기타 라이브러리

    • H2 데이터베이스 클라이언트

    • 커넥션 풀

      • 부트 기본은 HikariCP

    • WEB(thymeleaf)

    • 로깅 SLF4J & LogBack

    • 테스트

스프링 데이터 JPA는 스프링과 JPA를 먼저 이해하고 사용해야 하는 응용기술이다.

View 환경 설정

thymeleaf 템플릿 엔진

thymeleaf 공식 사이트

스프링 공식 튜토리얼

스프링부트 메뉴얼

@Controller
public class HelloController {

    @GetMapping("hello")
    public String hello(Model model) {
        model.addAttribute("data", "hello!!");
        return "hello";
    }
}

resources:template/ + {ViewName} + .html 경로를 통해 스프링 부트에 thymeleaf viewName이 매핑된다. 기본 정적 화면은 /static/index.html에 정의한다.

spring-boot-devtools 라이브러리를 추가한 뒤 html 파일을 컴파일만 하면 서버 재시작 없이 변경 사항이 반영된다. build-recompile 메뉴에서 컴파일 할 수 있다.

H2 데이터베이스 설치

개발이나 테스트 용도로 사용할 수 있는 가볍고 편리한 데이터베이스다. 웹 화면을 제공한다.

spring:
  datasource:
    url: jdbc:h2:tcp://localhost/~/jpashop
    username: sa
    password:
    driver-class-name: org.h2.Driver
  jpa:
    hibernate:
      ddl-auto: create  # 애플리케이션 실행 시점에 테이블을 drop하고 다시 생성한다.
    properties:
      hibernate:
        #      show_sql: true   # System.out에 하이버네이트 실행 SQL을 남긴다.
        format_sql: true
logging.level:
  org.hibernate.SQL: debug  # logger를 통해 하이버네이트 실행 SQL을 남긴다.

운영 환경의 모든 로그는 가급적 로거를 통해 남겨야 한다. show_sqlSystem.out에서 찍기 때문에 가급적 안 쓰는 게 좋다.

application.yml같은 yml 파일은 띄어쓰기 2칸으로 계층을 만든다. 따라서 반드시 맞춰줘야 한다.

JPA와 DB 설정, 동작 확인

@Entity
@Getter
@Setter
public class Member {

    @Id
    @GeneratedValue
    private Long id;
    private String username;
}

쿼리 파라미터 로그 남기기

기본 SQL 로그는 파라미터가 안 나와서 답답할 때가 많다.

spring:
  datasource:
    url: jdbc:h2:tcp://localhost/~/jpashop
    username: sa
    password:
    driver-class-name: org.h2.Driver
  jpa:
    hibernate:
      ddl-auto: create
    properties:
      hibernate:
        format_sql: true
logging.level:
  org.hibernate.SQL: debug
  # 파라미터를 로기로 남기는 옵션을 추가한다.
  org.hibernate.type: trace

아래에 각 파라미터의 값을 출력해준다.

implementation 'com.github.gavlyukovskiy:p6spy-spring-boot-starter:1.5.6'

외부 라이브러리를 사용하는 방법도 있다.

spring-boot-data-source-decorator

파라미터 값이 출력되는 것을 볼 수 있다.

참고로 운영 시스템에 외부 라이브러리를 쓸 때는 꼭 성능 테스트를 해봐야 한다.

Last updated