코딩과 결혼합니다

[Kotlin] 프로젝트 생성과 환경설정 본문

2세/Spring

[Kotlin] 프로젝트 생성과 환경설정

코딩러버 2024. 1. 20. 23:09
728x90

gradle.kts

//...

plugins {
    id("org.springframework.boot") version "3.2.2"
    id("io.spring.dependency-management") version "1.1.4"
    kotlin("jvm") version "1.9.22"
    kotlin("plugin.spring") version "1.9.22"
    kotlin("plugin.jpa") version "1.9.22"
}
//...

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-data-jpa")
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    developmentOnly("org.springframework.boot:spring-boot-devtools")
    runtimeOnly("com.mysql:mysql-connector-j")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
}

allOpen {
    annotation("jakarta.persistence.Entity")
}

noArg {
    annotation("jakarta.persistence.Entity")
}
//...

 

🤔 All Open은 plug.spring에서 오픈해준 것 외에 추가로 오픈해줄 것을 명시

👓 No Alg는 매개변수가 없는 생성자들을 자동으로 추가해줄 때 명시

JPA를 쓸 때 엔티티에 이 두가지를 적용시키기 위해 선언을 해주었다.

 

 

버전을 변수에 담아 일괄 적용하는 방법도 있다.

plugins {
    val kotlinVersion = "1.9.22"
    id("org.springframework.boot") version "3.2.2"
    id("io.spring.dependency-management") version "1.1.4"
    kotlin("jvm") version kotlinVersion
    kotlin("plugin.spring") version kotlinVersion
    kotlin("plugin.jpa") version kotlinVersion
}

yml 설정

- 이전에는 application.properties만 쓰다가 yml로 처음 설정해보았다.

server:
  port: 8080
  servlet:
    context-path: /
    encoding:
      charset: UTF-8
      enabled: true
      force: true //request와 response에 인코딩을 강제
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/study
    username: root
    password: 
  jpa:
    open-in-view: true //영속성 컨텍스트가 트랜잭션 범위를 넘어서 레이어까지 유지
    hibernate:
      ddl-auto: create
    properties:
      hibernate:
        show_sql: false //로그에 SQL을 찍는 것을 설정했기 때문
        format_sql: true
        highlight_sql: true
logging:
  pattern:
    console: "[%d{HH:mm:ss.SSS}][%-5level][%logger.%method:line%line] - %msg%n"
  level:
    org:
      hibernate:
        SQL: debug
        type.descriptor.sql: trace
jwt:
  secret: DadFufN4Oui8Bfv3ScFj6R9fyJ9hD45E6AGFsXgFsRhT4YSdSb