[Spring] DB 연동하기

세상을 바꾸는 개발자·2023년 3월 17일
0

DB 연동하기 전에 MySQL에서 스키마 생성하기

create database <DB 명>
use <DB 명>



아래 코드에서 수정해야 할 것

  • url에 DB명 수정
  • username에 DB 유저 아이디 수정 (만들 때 생성했을 것)
  • userpassword에 DB 유저 비밀번호 수정


MySQL

build.gradle

// ..

dependencies {
    // ..
    
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    runtimeOnly 'com.mysql:mysql-connector-j'
}

application.yml

server:
  port: 8010 # 서버 포트(기본값은 8080)
spring:
  thymeleaf:
    cache: false # 타임리프 캐시 끄기
    prefix: file:src/main/resources/templates/ # 타임리프 캐시 끄기(이 설정을 해야 꺼짐)
  devtools:
    livereload:
      enabled: true
    restart:
      enabled: true
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/<DB명>?useUnicode=true&characterEncoding=utf8&autoReconnect=true&serverTimezone=Asia/Seoul
    username: <DB아이디>
    password: <DB비번>
  jpa:
    hibernate:
      ddl-auto: create # DB 테이블 자동생성(엔티티 클래스만 만들면 됨)
      

MariaDB

build.gradle

// ..

dependencies {
    // ..
    
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    runtimeOnly 'org.mariadb.jdbc:mariadb-java-client'
}

application.yml

server:
  port: 8010 # 서버 포트(기본값은 8080)
spring:
  thymeleaf:
    cache: false # 타임리프 캐시 끄기
    prefix: file:src/main/resources/templates/ # 타임리프 캐시 끄기(이 설정을 해야 꺼짐)
  devtools:
    livereload:
      enabled: true
    restart:
      enabled: true
  datasource:
    driver-class-name: org.mariadb.jdbc.Driver
    url: jdbc:mariadb://127.0.0.1:3306/<DB명>?useUnicode=true&characterEncoding=utf8&autoReconnect=true&serverTimezone=Asia/Seoul
    username: <DB아이디>
    password: <DB비번>
  jpa:
    hibernate:
      ddl-auto: create # DB 테이블 자동생성(엔티티 클래스만 만들면 됨)
profile
초심 잃지 않기

0개의 댓글