[Spring Boot] MyBatis + MySql

Hayden·2023년 10월 27일

Spring Boot + MyBatis + MySql 연동 방법

사용 버전
Spring Boot : 2.7.16
MyBatis : 2.3.1
MySql : 8.0.34

종속성 추가

application.yml 설정

기본은 application.properties 지만 .yml이 관리가 편하고 key를 중복으로 사용하지 않아도 되서 사용합니다.

1) MySql 설정

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://데이터베이스주소:3306/데이터베이스명?serverTimezone=UTC&characterEncoding=UTF-8
    username: 데이터베이스 아이디
    password: 데이터베이스 비밀번호

2) MyBatis 설정

mybatis:
  mapper-locations: classpath:mapper/**/*Mapper.xml
  configuration:
    map-underscore-to-camel-case: true
  type-aliases-package: com.dev.shop
  1. mapper-locations : java말고 .xml에서 쿼리를 작성할 것이기 때문에 Mapper의 위치를 설정한다.
  1. map-underscore-to-camel-case: true : DB에서 컬럼명이 snake case로 되어 있으면 camel case로 변환하여 주는 설정이다.
  2. type-aliases-package: com.dev.shop : mapper.xml에서 resultType이나 parameterType에 클래스명만 입력할 수 있게 해주는 설정이다.
profile
백엔드 공부

0개의 댓글