๋ง์ด๋ฐํฐ์ค ์ค์ ๊ฐ์ด๋
1. ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ค์
๋ฐ์ดํฐ ์์ค ๊ตฌ์ฑ (MySQL)
์ฃผ์ ์ค์ ํญ๋ชฉ:
์ค์ ํญ๋ชฉ | ์ค๋ช
|
driver-class-name | MySQL JDBC ๋๋ผ์ด๋ฒ ์ง์ |
url | ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ URL (ํธ์คํธ, ํฌํธ, ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ช
, ์ต์
ํฌํจ) |
username/password | ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ ์ ๊ณ์ ์ ๋ณด |
2. MyBatis ํต์ฌ ์ค์
์ค์ ํ์ผ ๊ฒฝ๋ก
๋ณ๋์ MyBatis ์ค์ ํ์ผ์ด ํ์ํ ๊ฒฝ์ฐ ์ฃผ์์ ํด์ ํ๊ณ ์ฌ์ฉํฉ๋๋ค.
์นด๋ฉ ์ผ์ด์ค ์๋ ๋งคํ
๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ธ๋์ค์ฝ์ด ์ปฌ๋ผ๋ช
(board_no)์ ์๋ฐ์ ์นด๋ฉ์ผ์ด์ค ๋ณ์๋ช
(boardNo)์ผ๋ก ์๋ ๋ณํํฉ๋๋ค.
ํ์ ๋ณ์นญ ์ค์
๋งคํผ XML์์ resultType ์ฌ์ฉ ์ ์ ์ฒด ํจํค์ง๋ช
๋์ ํด๋์ค๋ช
๋ง ์ฌ์ฉํ ์ ์์ต๋๋ค.
โข
์ค์ ์ : com.aloha.mybatis.dto.BoardDto
โข
์ค์ ํ: BoardDto
๋งคํผ ํ์ผ ์์น
resources/mybatis/mapper ๋๋ ํ ๋ฆฌ ํ์์ ๋ชจ๋ XML ํ์ผ์ ๋งคํผ๋ก ์ธ์ํฉ๋๋ค.
3. ์ ์ฒด ์ค์ ์์
โข
spring.application.name=mybatis
# ๋ฐ์ดํฐ ์์ค - MySQL
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/aloha?serverTimezone=Asia/Seoul&allowPublicKeyRetrieval=true&useSSL=false&autoReconnection=true
spring.datasource.username=aloha
spring.datasource.password=123456
# Mybatis ์ค์
# mybatis.config-location=classpath:mybatis-config.xml
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.type-aliases-package=com.aloha.mybatis.dto
mybatis.mapper-locations=classpath:mybatis/mapper/**/**.xml
Java
๋ณต์ฌ
โข
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- ์ค์ -->
<settings>
<!-- ์ธ๋์ค์ฝ์ด ์ผ์ด์ค์ธ ์ปฌ๋ผ์ ์นด๋ฉ ์ผ์ด์ค๋ก ๋ณํํ๋ ์ค์ -->
<!-- board_no - boardNo -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<!-- ํ์
๋ณ์นญ ์ค์ -->
<typeAliases>
<!-- ํ
์ด๋ธ๊ณผ ๋งคํํ DTO๊ฐ ์๋ ํจํค์ง ๊ฒฝ๋ก ์ง์ -->
<package name="com.aloha.mybatis.dto"/>
</typeAliases>
</configuration>
XML
๋ณต์ฌ
4. ์ถ๊ฐ ํ
โข
๋ก๊น
์ค์ : SQL ์ฟผ๋ฆฌ ๋ก๊ทธ๋ฅผ ํ์ธํ๋ ค๋ฉด logging.level.com.aloha.mybatis.mapper=DEBUG๋ฅผ ์ถ๊ฐํ์ธ์.
โข
์ปค๋ฅ์
ํ: HikariCP๊ฐ ๊ธฐ๋ณธ์ผ๋ก ์ฌ์ฉ๋๋ฉฐ, ํ์์ spring.datasource.hikari.* ์์ฑ์ผ๋ก ์ธ๋ถ ์กฐ์ ๊ฐ๋ฅํฉ๋๋ค.
โข
๋งคํผ ์ธํฐํ์ด์ค: @Mapper ์ด๋
ธํ
์ด์
์ ์ธํฐํ์ด์ค์ ์ถ๊ฐํ๋ฉด ์๋์ผ๋ก ๋งคํผ๋ก ๋ฑ๋ก๋ฉ๋๋ค.



