๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Java·๏ปฟServlet·๏ปฟJSP/Library·Framework

MyBatis ์„ค์ • ํŒŒ์ผ ์ž‘์„ฑ ๋ฐฉ๋ฒ•

by Leica 2020. 2. 24.
๋ฐ˜์‘ํ˜•
  • ๊ด€๋ จ ๊ธ€

- [JAVA/WEB] ์›น ํ”„๋กœ์ ํŠธ์— MyBatis ์„ธํŒ… ๋ฐ ์ ์šฉํ•˜๊ธฐ

- MyBatis ์„ค์ • ํŒŒ์ผ - SQL Mapper ์ž‘์„ฑ ๋ฐฉ๋ฒ•


MyBatis ์„ค์ • ํŒŒ์ผ ์ž‘์„ฑ ๋ฐฉ๋ฒ•

MyBatis ํ”„๋ ˆ์ž„์›Œํฌ๊ฐ€ ์ฐธ์กฐํ•˜๋Š” XML ํŒŒ์ผ์€ MyBatis ์„ค์ • ํŒŒ์ผ๊ณผ SQL mapper ํŒŒ์ผ๋กœ ๋‚˜๋ˆ„์–ด์ง„๋‹ค. ๋ณธ ํฌ์ŠคํŒ…์—์„œ๋Š” MyBatis ์„ค์ • ํŒŒ์ผ ์ž‘์„ฑ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ๋‹ค๋ฃฐ๊ฒƒ์ด๋‹ค.

 

MyBatis๋Š”

  • ์ž์ฒด ์ปค๋„ฅ์…˜ํ’€ ๊ตฌ์ถ•
  • ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๊ฒฐ ์ •๋ณด๋ฅผ ์„ค์ •ํ•ด ๋‘๊ณ  ๊ฐœ๋ฐœ, ํ…Œ์ŠคํŠธ, ์šด์˜์— ๋”ฐ๋ผ ์‚ฌ์šฉํ•  DB ์ง€์ •
  • SELECT ๊ฒฐ๊ณผ ์บ์‹ฑ
  • VO(Value Object, ๊ฐ’ ๊ฐ์ฒด)์— alias ๋ถ€์—ฌ

๋“ฑ์˜ ๊ธฐ๋Šฅ์„ ์ง€์›ํ•œ๋‹ค.

๋”ฐ๋ผ์„œ MyBatis ์„ค์ •ํŒŒ์ผ์—๋Š” ํ•ด๋‹น ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋™์ž‘ ํ™˜๊ฒฝ์„ ์„ค์ •ํ•œ๋‹ค.

 

๋‹ค์Œ์€ MyBatis ์„ค์ • ํŒŒ์ผ์˜ ์˜ˆ์ด๋‹ค.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <properties resource="com/atoz_develop/spms/dao/db.properties"/>
 
    <typeAliases>
        <typeAlias type="com.atoz_develop.spms.vo.Project" alias="project"/>
        <typeAlias type="com.atoz_develop.spms.vo.Member" alias="member"/>
    </typeAliases>
 
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>
 
    <mappers>
        <mapper resource="com/atoz_develop/spms/dao/MySqlProjectDao.xml"/>
    </mappers>
</configuration>
cs

 

1. XML๊ณผ DTD ์„ ์–ธ

๊ฐ€์žฅ ๋จผ์ € XML ์„ ์–ธ๊ณผ DTD ์„ ์–ธ์ด ์˜จ๋‹ค.

 

 

2. Root Element - <configuration>

MyBatis ์„ค์ •์˜ ๋ฃจํŠธ ์—˜๋ฆฌ๋จผํŠธ๋Š” <configuration>์ด๋‹ค.

 

๐Ÿ“ ์ฐธ๊ณ  - <configuration>์˜ DTD ๊ทœ์น™

<!ELEMENT configuration (properties?, settings?, typeAliases?, typeHandlers?, objectFactory?, objectWrapperFactory?, plugins?, environments?, databaseIdProvider?, mappers?)>

 

๋‹ค์Œ์€ <configurations>์˜ child elements ์ค‘ ์ฃผ์š” ์—˜๋ฆฌ๋จผํŠธ์˜ ์šฉ๋„๋ฅผ ์ •๋ฆฌํ•œ ํ‘œ์ด๋‹ค.

 

์—˜๋ฆฌ๋จผํŠธ ์šฉ๋„
properties ํ”„๋กœํผํ‹ฐ ํŒŒ์ผ ๊ฒฝ๋กœ
<property>์— ๊ฐœ๋ณ„ ํ”„๋กœํผํ‹ฐ ์ •์˜
settings ํ”„๋ ˆ์ž„์›Œํฌ ์‹คํ–‰ ํ™˜๊ฒฝ ์„ค์ •
typeAliases ์ž๋ฐ” ํด๋ž˜์Šค ์ด๋ฆ„์— ๋Œ€ํ•œ alias ์„ค์ •
typeHandlers ์ปฌ๋Ÿผ ๊ฐ’์„ ์ž๋ฐ” ๊ฐ์ฒด๋กœ, ์ž๋ฐ” ๊ฐ์ฒด๋ฅผ ์ปฌ๋Ÿผ ๊ฐ’์œผ๋กœ ๋ณ€ํ™˜ํ•ด์ฃผ๋Š” ํด๋ž˜์Šค ์„ค์ •
environments ํ”„๋ ˆ์ž„์›Œํฌ์—์„œ ์‚ฌ์šฉํ•  ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ •๋ณด(ํŠธ๋žœ์žญ์…˜ ๊ด€๋ฆฌ์ž, ๋ฐ์ดํ„ฐ ์†Œ์Šค) ์„ค์ •
mappers SQL mapper ํŒŒ์ผ ๊ฒฝ๋กœ

 

1) <properties>

๋ณดํ†ต ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๊ฒฐ ์ •๋ณด์ฒ˜๋Ÿผ ์ž์ฃผ ๋ณ€๊ฒฝ๋ ์ˆ˜ ์žˆ๋Š” ๊ฐ’์€ ํ”„๋กœํผํ‹ฐ ํŒŒ์ผ์— ์ž‘์„ฑํ•œ ๋’ค ์ด๋ฅผ ์ฐธ์กฐํ•ด์„œ ๊ฐ’์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค.

<properties>๋Š” ์ด๋Ÿฐ ์šฉ๋„๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

 

ํ”„๋กœํผํ‹ฐ ํŒŒ์ผ์ด CLASSPATH์— ์žˆ์œผ๋ฉด resource ์†์„ฑ์„ ์‚ฌ์šฉํ•ด์„œ ์„ค์ •ํ•˜๊ณ  CLASSPATH ๋ฐ–์— ์œ„์น˜ํ•œ๋‹ค๋ฉด url ์†์„ฑ์„ ์‚ฌ์šฉํ•œ๋‹ค.

 

* ๊ฐœ๋ณ„ ํ”„๋กœํผํ‹ฐ ์„ค์ •

ํ”„๋กœํผํ‹ฐ ํŒŒ์ผ์— ์ •์˜๋œ ๊ฒƒ ์™ธ์— ์ถ”๊ฐ€๋กœ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ •์˜ํ•˜๋ ค๋ฉด <properties> ํ•˜์œ„์— <property>๋ฅผ ์„ค์ •ํ•œ๋‹ค.

 

1
2
3
4
<properties resource="com/atoz_develop/spms/dao/db.properties">
    <property name="username" value="lalala"/>
    <property name="password" value="lololo"/>
</properties>
cs

 

* ํ”„๋กœํผํ‹ฐ ๊ฐ’ ์ฐธ์กฐ

ํ”„๋กœํผํ‹ฐ ๊ฐ’์€ ${ํ”„๋กœํผํ‹ฐ๋ช…} ํ˜•์‹์œผ๋กœ ์ฐธ์กฐํ•œ๋‹ค.

 

 

2) <typeAlias>

SQL mapper ํŒŒ์ผ์—์„œ parameterType์ด๋‚˜ resultType์„ ์ง€์ •ํ• ๋•Œ ๊ธด ์ด๋ฆ„์˜ ํด๋ž˜์Šค๋ช… ๋Œ€์‹  ์งง์€ ์ด๋ฆ„์˜ alias๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

<typeAlias>๋Š” ์ด๋Ÿฐ SQL mapper์—์„œ ์‚ฌ์šฉํ•  alias๋ฅผ ์„ค์ •ํ•œ๋‹ค.

 

 

* SQL Mapper์—์„œ alias ์‚ฌ์šฉ

MyBatis ์„ค์ • ํŒŒ์ผ์— ์„ค์ •๋œ alias๋Š” SQL mapper์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

 

 

* MyBatis Built-In Alias

MyBatis์—๋Š” int, double, boolean๊ฐ™์€ ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด๋‚˜ wrapper ํด๋ž˜์Šค์— ๋Œ€ํ•ด ๋ฏธ๋ฆฌ alias๊ฐ€ ์ •์˜๋˜์–ด ์žˆ๋‹ค.

๋‹ค์Œ์€ ๋ฏธ๋ฆฌ ์ •์˜๋œ alias ๋ชฉ๋ก์ด๋‹ค.

 

alias type alias type
_byte byte byte java.lang.Byte
_short short short java.lang.Short
_int, _integer int int, integer java.lang.Integer
_long long long java.lang.Long
_float float float java.lang.Float
_double double double java.lang.Double
_boolean boolean boolean java.lang.Boolean
string java.lang.String date java.util.Date
decimal java.math.BigDecimal bigdecimal java.math.BigDecimal
map java.util.Map hashmap java.util.HashMap
list java.util.list arraylist java.util.ArrayList
collection java.util.Collection iterator java.util.Iterator
object java.lang.Object    

 

3) <environments>

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ™˜๊ฒฝ ์ •๋ณด๋ฅผ ์„ค์ •ํ• ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

์ด ํƒœ๊ทธ๋ฅผ ์ด์šฉํ•˜๋ฉด ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ ‘์† ์ •๋ณด๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

์„ค์ •๋œ DB ์ •๋ณด ์ค‘ ํ•˜๋‚˜๋ฅผ ์„ ํƒํ•  ๋•Œ default ์†์„ฑ์„ ์‚ฌ์šฉํ•œ๋‹ค.

 

 

<environment>์— ๊ฐ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ํŠธ๋žœ์žญ์…˜ ๊ด€๋ฆฌ ๋ฐฉ์‹, ๋ฐ์ดํ„ฐ ์†Œ์Šค๋ฅผ ์„ค์ •ํ•œ๋‹ค.

 

* <transactionManager>

MyBatis์—์„œ ํŠธ๋žœ์žญ์…˜์„ ๊ด€๋ฆฌํ•˜๋Š” ๋ฐฉ์‹์—๋Š” ๋‘ ๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค.

 

ํŠธ๋žœ์žญ์…˜ ๊ด€๋ฆฌ ์œ ํ˜• ์„ค๋ช…
JDBC ์ง์ ‘ JDBC์˜ commit, rollback ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜์—ฌ MyBatis ์ž์ฒด์—์„œ ํŠธ๋žœ์žญ์…˜์„ ๊ด€๋ฆฌ
MANAGED ์„œ๋ฒ„์˜ ํŠธ๋žœ์žญ์…˜ ๊ด€๋ฆฌ ๊ธฐ๋Šฅ์„ ์ด์šฉ
์ฆ‰ Java EE ์„œ๋ฒ„๋‚˜ ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ์—์„œ ํŠธ๋žœ์žญ์…˜์„ ๊ด€๋ฆฌ

 

<transactionManager>์˜ type ์†์„ฑ์— JDBC ๋˜๋Š” MANAGED๋ฅผ ์ง€์ •ํ•˜๋ฉด ๋œ๋‹ค.

 

* <dataSource>

MyBatis๋Š” JDBC ํ‘œ์ค€ ์ธํ„ฐํŽ˜์ด์Šค์ธ javax.sql.DataSource ๊ตฌํ˜„์ฒด๋ฅผ ์ด์šฉํ•˜์—ฌ DB ์ปค๋„ฅ์…˜์„ ๋‹ค๋ฃฌ๋‹ค.

MyBatis์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋ฐ์ดํ„ฐ ์†Œ์Šค ์œ ํ˜•์€ ๋‹ค์Œ ์„ธ ๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค.

 

๋ฐ์ดํ„ฐ ์†Œ์Šค ์œ ํ˜• ์„ค๋ช…
UNPOOLED DB ์ปค๋„ฅ์…˜์„ ์š”์ฒญํ•  ๋•Œ๋งˆ๋‹ค ์ปค๋„ฅ์…˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
๋†’์€ ์„ฑ๋Šฅ์„ ์š”๊ตฌํ•˜์ง€ ์•Š๋Š” ๋‹จ์ˆœํ•œ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์— ์ ํ•ฉํ•˜๋‹ค.
POOLED MyBatis ์ž์ฒด DB ์ปค๋„ฅ์…˜ํ’€์„ ์‚ฌ์šฉํ•œ๋‹ค.
๋ฏธ๋ฆฌ DB ์ปค๋„ฅ์…˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด ๋‘๊ณ  ์š”์ฒญํ•˜๋ฉด ์ฆ‰์‹œ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๊ฒฐ์„ ์ดˆ๊ธฐํ™”ํ•˜๊ณ  ์‚ฌ์šฉ์ž๋ฅผ ์ธ์ฆํ•˜๋Š” ๊ณผ์ •์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— ์†๋„๊ฐ€ ๋น ๋ฅด๋‹ค.
JNDI Java EE ์„œ๋ฒ„, ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ์—์„œ ์ œ๊ณตํ•˜๋Š” ๋ฐ์ดํ„ฐ ์†Œ์Šค๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

 

<dataSource>์˜ type ์†์„ฑ์— UNPOOLED, POOLED ๋˜๋Š” JNDI๋ฅผ ์ง€์ •ํ•˜๋ฉด ๋œ๋‹ค.

 

4) <mappers>

SQL mapper ํŒŒ์ผ๋“ค์˜ ์ •๋ณด๋ฅผ ์„ค์ •ํ•œ๋‹ค.

๊ฐ mapper ํŒŒ์ผ์„ <mapper> ํƒœ๊ทธ๋กœ ์ •์˜ํ•œ๋‹ค.

 

 

* resource์™€ url ์†์„ฑ

<properties>์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ CLASSPATH์— ์žˆ์„ ๊ฒฝ์šฐ resource ์†์„ฑ์— ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•˜๊ณ  CLASSPATH ์™ธ๋ถ€์— ์žˆ์„ ๊ฒฝ์šฐ url ์†์„ฑ์— ๊ฒฝ๋กœ๋ฅผ ์ง€์ •ํ•œ๋‹ค.

 

MyBatis ์„ค์ • ํŒŒ์ผ ์ž‘์„ฑ ๊ฐ€์ด๋“œ ๋ฌธ์„œ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์••์ถ• ํŒŒ์ผ ๋‚ด์— mybatis-x.x.x.pdf ์™€ ๊ฐ™์ด PDF ํŒŒ์ผ๋กœ๋„ ์ œ๊ณต๋œ๋‹ค.

ํ•ด๋‹น ๋ฌธ์„œ๋ฅผ ํ•จ๊ป˜ ์ฐธ์กฐํ•˜๋ฉด ๋” ์ž์„ธํ•œ ์ •๋ณด๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.


  • ๊ด€๋ จ ๊ธ€

- [JAVA/WEB] ์›น ํ”„๋กœ์ ํŠธ์— MyBatis ์„ธํŒ… ๋ฐ ์ ์šฉํ•˜๊ธฐ

- MyBatis ์„ค์ • ํŒŒ์ผ - SQL Mapper ์ž‘์„ฑ ๋ฐฉ๋ฒ•

 

References

์—ดํ˜ˆ๊ฐ•์˜ ์ž๋ฐ” ์›น ๊ฐœ๋ฐœ ์›Œํฌ๋ถ(ํ”„๋ฆฌ๋ ‰, 2016, ์—„์ง„์˜)

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€