[MyBatis - ๐Ÿ› ๏ธTroubleShooting] Mybatis์— camelCase ๋ณ€ํ™˜ ์ ์šฉํ•˜๊ธฐ

Oguยท2024๋…„ 4์›” 12์ผ
1
post-thumbnail

์ง€๋‚œ ํฌ์ŠคํŒ…์—์„œ ๊ธฐ๋ณธ์ƒ์„ฑ์ž ๋˜๋Š” ๋กฌ๋ณต@NoArgsArgument๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋งคํ•‘ํ•  ๊ฒฝ์šฐ ์ฃผ์˜ํ•  ์ ๊ณผ ํ•ด๊ฒฐ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์•˜์Šต๋‹ˆ๋‹ค.

์ง€๋‚œ ํฌ์ŠคํŒ…์€
[MyBatis - ๐Ÿ› ๏ธTroubleShooting] ๊ธฐ๋ณธ ์ƒ์„ฑ์ž์™€ ๊ฐ์ฒด ๋งคํ•‘์‹œ ์ฃผ์˜ํ•  ์ (ํŠน์ • ์ปฌ๋Ÿผ์ด null๋กœ ๋งคํ•‘๋˜์ง€ ์•Š๋Š” ํ˜„์ƒ)
์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด๋ฒˆ ํฌ์ŠคํŒ…์€ ์ œ๊ฐ€ ํ”„๋กœ์ ํŠธ์—์„œ ์ ์šฉํ•œ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜์ธ Mybatis์— camelCase ์ž๋™ ๋ณ€ํ™˜ ์„ค์ • ๊ธฐ๋Šฅ์„ ์ ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

๐Ÿ—‚๏ธ Mybatis์— camelCase ๋ณ€ํ™˜ ๊ธฐ๋Šฅ ์ ์šฉํ•˜๊ธฐ

๋งค๋ฒˆ ์ง์ ‘ alias๋ฅผ ์ ์–ด์ฃผ๊ฑฐ๋‚˜, column์„ ์ง์ ‘ ์ง€์ •ํ•ด์ฃผ๊ธด ๋„ˆ๋ฌด๋‚˜ ๊ท€์ฐฎ๊ณ  ์ƒ์‚ฐ์„ฑ์ด ๋–จ์–ด์ง‘๋‹ˆ๋‹ค.

๋‹คํ–‰ํžˆ Mybatis์—์„œ๋„ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ์–ธ๋”์Šค์ฝ”์–ด(_)๋กœ ๊ตฌ๋ถ„๋œ ์ปฌ๋Ÿผ๋ช…์„ ์ž๋ฐ”์˜ ์นด๋ฉœ ํ‘œ๊ธฐ๋ฒ•(camelCase)์— ๋งž๊ฒŒ ์ž๋™ ๋ณ€ํ™˜ํ•ด์ฃผ๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

ํ•ด๋‹น ๊ธฐ๋Šฅ์„ ์ ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ 2๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
1. .yml ๋˜๋Š” .properties ๋กœ ์ ์šฉ
2. .xml Mybatis ์„ค์ • ํŒŒ์ผ๋กœ ์ ์šฉ

๐Ÿฆ• 1) .yml ๋˜๋Š” .properties๋กœ ์ ์šฉ

.yml

mybatis:
  ...
  configuration:
    mapUnderscoreToCamelCase: true

.properties

db.driver=com.mysql.jdbc.Driver
db.host=jdbc:mysql://localhost:3306/mysql?serverTimezone=UTC&allowPublicKeyRetrieval=true&useSSL=false
db.username=root
db.password=root

๐Ÿฆ• 2) mybatis-config.xml์— camelCase setting ์ ์šฉ

๊ธฐ๋ณธ ์ธ์‹ ๊ฒฝ๋กœ์ธ resources ์•„๋ž˜ mabatis-config.xml ์ด๋ฆ„์œผ๋กœ ๋งˆ์ด๋ฐ”ํ‹ฐ์Šค ์„ค์ • ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๋ฉด ์ž๋™์œผ๋กœ springboot๊ฐ€ ์ธ์‹ํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ๊ณผ ๊ฐ™์ด camelCase ์„ค์ •์„ ์ถ”๊ฐ€ํ•˜๋ฉด, camelCase ๋ณ€์ˆ˜๋„ค์ด๋ฐ์„ ์‚ฌ์šฉํ•ด๋„ ์ž๋™์œผ๋กœ ์ž˜ ๋งคํ•‘ํ•ด์ค๋‹ˆ๋‹ค.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd" >
<configuration>
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true" />
    </settings>
</configuration>

Mybatis config ๊ฒฝ๋กœ ์„ค์ •

๊ธฐ๋ณธ ๊ฒฝ๋กœ๊ฐ€ ์•„๋‹Œ ๋‹ค๋ฅธ ๊ฒฝ๋กœ์— ์„ค์ • ํŒŒ์ผ์„ ์ƒ์„ฑํ•œ๋‹ค๋ฉด, ๊ทธ ๊ฒฝ๋กœ๋ฅผ ์•Œ๋ ค์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.
๋‹ค์Œ๊ณผ ๊ฐ™์ด config-location ๊ฒฝ๋กœ๋ฅผ ์ง์ ‘ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

mybatis:
  mapper-locations: classpath:/mappers/**.xml
  config-location : classpath:/config/mybatis-config.xml

์ฃผ์˜ํ•  ์ ์€, mapper-locations๋Š” * ์™€์ผ๋“œ์นด๋“œ๋ฅผ ์‚ฌ์šฉํ•œ ๋ณต์ˆ˜ ๊ฒฝ๋กœ๋กœ ์ง€์ •์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ,
config-location ์ฆ‰ ์„ค์ • ํŒŒ์ผ์˜ ๊ฒฝ๋กœ๋Š” ํŒŒ์ผ ์ด๋ฆ„์„ ํฌํ•จํ•ด ๋”ฑ ํ•˜๋‚˜๋งŒ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ฐธ๊ณ  org.mybatis.spring.boot.autoconfigure.MybatisProperties.java

  • mapperLocations
    ์ง์ ‘ MyBatisProperties ํด๋ž˜์Šค์˜ ์ฝ”๋“œ๋ฅผ ํ™•์ธํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
public void setMapperLocations(String[] mapperLocations) {
  this.mapperLocations = mapperLocations;
}
  • config-location
public void setConfigLocation(String configLocation) {
  this.configLocation = configLocation;
}

๐Ÿ—‚๏ธ ๊ฒฐ๊ณผ

์ €์˜ ๊ฒฝ์šฐ, Mybatis์— ๊ด€๋ จ๋œ ์„ค์ •์€ ํ•˜๋‚˜์˜ ํŒŒ์ผ์—์„œ ์ค‘์•™ ์ง‘์ค‘ํ•˜์—ฌ ๊ด€๋ฆฌ ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„ mybatis-config.xml ํŒŒ์ผ์„ ๋”ฐ๋กœ ์ƒ์„ฑํ•˜์—ฌ ์ ์šฉํ•˜๊ธฐ๋กœ ํ–ˆ์Šต๋‹ˆ๋‹ค.

๊ทธ ๊ฒฐ๊ณผ, mybatis-config.xml ์— camelCase ์„ค์ •๊ณผ ๊ธฐ๋ณธ์ƒ์„ฑ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ
์ง์ ‘ column ๋ช…์‹œ๋‚˜ alias ๋ช…์‹œ ์—†์ด camelCase ํฌํ•จ ์ž๋™์œผ๋กœ ๊ฐ™์€ ๋„ค์ด๋ฐ์˜ ๋ณ€์ˆ˜-์ปฌ๋Ÿผ ์„ ์ธ์‹ํ•˜์—ฌ ๋งคํ•‘ํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

์ •์ƒ์ ์œผ๋กœ ๋งคํ•‘์ด ๋œ ๋ชจ์Šต์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

profile
Hello! I am Ogu, a developer who loves learning and sharing! ๐Ÿค๐Ÿค <br> ใ“ใ‚“ใซใกใฏ๏ผๅญฆใถใ“ใจใจๅ…ฑๆœ‰ใ™ใ‚‹ใ“ใจใŒๅฅฝใใช้–‹็™บ่€…ใฎOguใงใ™๏ผ๐Ÿค

0๊ฐœ์˜ ๋Œ“๊ธ€