MyBatis
My-Batis
MyBatis๋?
Mybatis๋ ์๋ฐ ์ค๋ธ์ ํธ์ SQL์ฌ์ด์ ์๋ ๋งคํ ๊ธฐ๋ฅ์ ์ง์ํ๋ ORM(Object relational Mapping)ํ๋ ์์ํฌ์ด๋ค.
SQL์ ๋ณ๋์ ํ์ผ๋ก ๋ถ๋ฆฌํด์ ๊ด๋ฆฌํ๊ฒ ํด์ค๋ค.
Hibernate๋ JAP(Java Persistence Api)์ฒ๋ผ ์๋ก์ด DBํ๋ก๊ทธ๋๋ฐ ํจ๋ฌ๋ค์์ ์ตํ์ผํ๋ ๋ถ๋ด ์์ด SQL์ ๊ทธ๋๋ก ์ด์ฉํ๋ฉด์
JDBC์ฝ๋ ์์ฑ์ ๋ถํธํจ๋ ์ ๊ฑฐํด์ฃผ๊ณ ๋๋ฉ์ธ ๊ฐ์ฒด๋ VO๊ฐ์ฒด๋ฅผ ์ค์ฌ์ผ๋ก ๊ฐ๋ฐ์ด ๊ฐ๋ฅํ๋ค๋ ์ฅ์ ์ด ์๋ค
java ์์ mybatis ๋ฅผ ์ฌ์ฉํ ๋ ๋ฆฝ์ ์ธ DB์ ๊ทผ ํด๋์ค ๊ฐ๋ฐ์ ํ ์ ์๋๋ก ํด์ค๋ค.
- ์๋ฐ์์ ์ ๊ณตํ๋ JDBC๋ฅผ ๋ณด๋ค ํธ๋ฆฌํ๊ฒ ์ฌ์ฉํ ์ ์๋๋ก ํด์ฃผ๋ ํ๋ ์์ํฌ
- SQL ์ฟผ๋ฆฌ๋ฌธ์ ์ฝ๋ ๋ด์์ ์ฐ์ง ์๊ณ Mapper ํ์ผ์์ ๊ด๋ฆฌํจ์ผ๋ก์จ ์ฝ๋์ SQL ์ฟผ๋ฆฌ๋ฅผ ๋ถ๋ฆฌ
- SQL ์ฟผ๋ฆฌ ์์ ์ ์ฝ๋๋ฅผ ์ง์ ์์ ํ์ง ์์๋ ๋ผ ์ ์ง๋ณด์ ์ ์์ ์ฑ์ ๋์
- ์ฝ๋๋ฅผ ๊ฐ์ํํ์ฌ ๊ฐ์์ฑ์ ๋์ฌ์ค
Spring-FrameWork์์ ๋ํ JDBC๋ฅผ ์ง์ํ์ง๋ง My-Batis๊ฐ ์ฝ๋๋ฅผ ๋ ๊ฐ๊ฒฐํ๊ฒ
์์ฑํ ์ ์๋ ์ฅ์ ์ ๊ฐ์ง๊ณ ์์ด My-Batis๋ฅผ ๋ ๋ง์ด ์ฌ์ฉํ๋ค.
์ฝ๊ฒ ๋งํ์๋ฉด DB์ ์ฐ๊ฒฐํ๋ Connection๊ณผ sql์ ์์ฑํ๋ ๋ถ๋ถ์ mybatis์์ ๋๋ ๊ด๋ฆฌํด์ฃผ๋ ๊ฒ์ด๋ค.
์ํ์น ํฐ์ผ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก ibatis๋ก ์์ํด์ 3.0๋ฒ์ ผ๋ถํฐ MyBatis๋ผ๋ ์ด๋ฆ์ผ๋ก ๋ ๋ฆฝํ๋ค.
Spring-FrameWork๋ Open-Source๋ก ๋ค๋ฅธ FrameWork๋ฅผ ํ์ฅํ์ฌ ์ฌ์ฉ์ด ๊ฐ๋ฅํ๋ค.
MyBatis ์ค์น
MyBatis ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๋ค์ด๋ฐ์ ์ฌ์ฉ ํ ์ ์๋ค.
https://blog.mybatis.org/ ์ ์ฌ์ดํธ๋ฅผ ์ด์ฉํ์ฌ ๋ค์ด๋ก๋ ํ ์ ์๋ค.
ํ๊ธ๋ก ๋ฒ์ญ๋ https://mybatis.org/mybatis-3/ko/index.html๋ ์๋ค.
MyBatis ์ฐ๋
MyBatis์ ํต์ฌ MetaDate๋ก DB์ ์ฐ๊ฒฐ ์ ๋ณด๋ฅผ ๋ด์ config.xml์ ์ฌ์ฉํ์ฌ Connection์ ํ๋ค.
<?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">
<!--
mybatis-config.xml :: MyBatis Framework ์ ํต์ฌ MetaData ๋ก ์๋์ ์ค์
- properties ๊ด๋ฆฌ : ์ ํ๋ฆฌ์ผ์ด์
๋ฐฐํฌ์ ๊ณตํต๋ถ๋ถ, ๊ฐ ํ๊ฒฝ๋ณ ๋ค๋ฅธ ๊ฐ๋ค์ Properties
ํ์ผ์ ๋
๋ฆฝ์ ์ผ๋ก ๊ด๋ฆฌ.
- ClassName ๊ด๋ฆฌ : FQCN(Full Qualified Class Name) ์ ๋ณ์นญ(Alias)๊ด๋ฆฌ
- ๊ธฐํ ์ค์ : ํธ๋์ญ์
์ฒ๋ฆฌ, ์ปค๋ฅ์
ํ ์ค์ , SQL ๋ฑ ์ฌ๋ฌ ํ๊ฒฝ์ค์ ์ ๋ฑ๋ก,์ค์ ...
-->
<!-- config.xml -->
<configuration>
<properties resource="sql/jdbc.properties"/>
<settings>
<setting name="callSettersOnNulls" value="false"/>
</settings>
<typeAliases>
<typeAlias alias="user" type="mybatis.service.domain.User"/>
</typeAliases>
<environments default="development">
<environment id="development">
<!-- Transaction Definition -->
<transactionManager type="JDBC"/>
<!-- javax.sql.DataSource Definition -->
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:xe"/>
<property name="username" value="ibatis"/>
<property name="password" value="ibatis"/>
</dataSource>
</environment>
</environments>
<!-- SQL Definition :: file include -->
<!-- ๊ฐ๊ฐ sql์ ๋ง๋ MetaData๋ฅผ ์์ฑํ์ฌ ๊ฐ๋ณ์ ๋
๋ฆฝ ๊ฐ๋ฐ์ ํ ์ ์๊ฒ ํจ -->
<mappers>
<mapper resource="sql/UserMapper.xml"/>
<mapper resource="sql/UserMapper01.xml"/>
<mapper resource="sql/UserMapper02.xml"/>
<mapper resource="sql/UserMapper03.xml"/>
<mapper resource="sql/UserMapper04.xml"/>
<mapper resource="sql/UserMapper05.xml"/>
<mapper resource="sql/UserMapper06.xml"/>
<mapper resource="sql/UserMapper07.xml"/>
<mapper resource="sql/UserMapper08.xml"/>
<mapper resource="sql/UserMapper09.xml"/>
<mapper resource="sql/UserMapper10.xml"/>
</mappers>
</configuration>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--
UserMapper.xml :: SQL ๋ฅผ ๊ฐ๋ MetaData
- MyBatis Framework ์ XML MetaData ๋ฅผ ์ฌ์ฉ SQL๊ตฌ๋ฌธ์ ๊ด๋ฆฌ
(SQL์ xml ์ ํตํด ์ ์ ํ ์บก์ํ ๋์ด ์ ํ๋ฆฌ์ผ์ด์ ์ธ๋ถ๋ก ๋ถ๋ฆฌํจ)
- SQL๋ฌธ์ ์
๋ ฅ(?) / ์ถ๋ ฅ(?)์ ๋งคํํ๋ค. <== ์๋์ select element ์ฐธ์กฐ
-->
<!-- UserMapper.xml -->
<mapper namespace="UserMapper">
<!-- ์์ namespace ์ด์ฉ Application ์์ UserMapper.~~ ๋ก ์๋์ SQL ์ ์ฐธ์กฐ
:: ์๋์ query ์ํ ํ ๊ฒฐ๊ณผ resultType ์ ์ ์ธ๋ User Domain Object
Field ( property )์ ์๋์ฐ๊ฒฐ(?,๋ฐ์ธ๋ฉ)
:: SQL / Query ์ Alias ์ User Domain Object ์ Field ์ด๋ฆ ๋์ผํ ๊ฒ ํ์ธ. -->
<select id="getUserList" resultType="mybatis.service.domain.User">
SELECT
user_id AS userId,
user_name AS userName,
password AS password,
age AS age,
grade AS grade,
reg_date AS regDate
FROM users
</select>
</mapper>
์์ ๊ฐ์ด ์ฝ๊ฒ sql์ ๋ค๋ฃฐ์ค ์๋ค๋ฉด ์ฝ๋๋ฅผ ์์ฑํ์ฌ MetaData๋ก ๊ด๋ฆฌ ํ ์ ์๋ค.
์ฟผ๋ฆฌ๋ฌธ ์์ฑ
CRUD

| ย | ๋ป | SQL |
|---|---|---|
| C | CREATE | <select> |
| R | READ | <insert> |
| U | UPDATE | <update> |
| D | DELETE | <delete> |
CRUD๋ชจ๋ ๊ฐ๊ฐ์ ๋ง๋ SQL ์ฟผ๋ฆฌ๋ฌธ์ ์์ฑํ์ฌ tag์์ ๋ฃ์ด์ฃผ๋ฉด ๋๋ค.
CRUD ์์ฑ
| ์์ฑ | ย |
|---|---|
| id | ์ฟผ๋ฆฌ๋ฌธ์ ์๋ณ์ |
| resultType | select์ ๊ฒฐ๊ณผ์ ๋ฐ์ดํฐ ํ์ |
| resultMap | resultMap์ผ๋ก ๋งตํ๋ ๋ฐ์ดํฐ๋ฅผ select์ ๊ฒฐ๊ณผ์ ๋ฐ์ดํฐ ํ์ |
| parameterType | ๋งค๊ฐ๋ณ์์ ๋ฐ์ดํฐ ํ์ |
๋์ SQL
MyBatis๋ ๋์ sql๋ก ํ๊ทธ๋ฅผ ํตํด ์ํฉ์ ๋ฐ๋ผ ์ฌ๋ฌ๊ฐ์ง ์ผ์ด์ค๋ฅผ ์ฒ๋ฆฌํ ์ ์๋ค.
| ย | ย |
|---|---|
| <if test=โ์กฐ๊ฑดโ>SQL</if> | ์กฐ๊ฑด์ด ์ฐธ์ผ ๊ฒฝ์ฐ SQL๋ฌธ์ ๋ฐํํ๋ค. |
| <choose> <when test=โ์กฐ๊ฑด1โ>SQL</when> <when test=โ์กฐ๊ฑด2โ>SQL</when> <otherwise>SQL</otherwise> <choose> |
๊ฒ์ฌํ ์กฐ๊ฑด์ด ์ฌ๋ฌ๊ฐ์ผ ๊ฒฝ์ฐ ์ฌ์ฉ ์๋ฐ์ switch๋ฌธ๊ณผ ์ ์ฌํ๋ค. ์ผ์นํ๋ ์กฐ๊ฑด์ด ์์ผ๋ฉด <otherwise>๋ฐํ๋๋ค. |
| <where> <if test=โ์กฐ๊ฑด1โ>SQL</if> <if test=โ์กฐ๊ฑด2โ>SQL</if> </where> |
SQL๋ฌธ์ WHERE์ ์ ๋ง๋ค๋ ์ฌ์ฉํ๋ค. <where> ์์ ์กฐ๊ฑด์์์ ์ผ์นํ๋ ์กฐ๊ฑด์ด ์์ผ๋ฉด WHERE์ ์ ๋ง๋ค์ด ๋ฐํํ๊ณ ์์ผ๋ฉด ๋ง๋ค์ง ์๋๋ค. |
| <trim prefix=โ์ ๋์ดโ prefixOverrides=โ๋ฌธ์์ด|๋ฌธ์์ดโ> <if test=โ์กฐ๊ฑด1โ>SQL</if> <if test=โ์กฐ๊ฑด2โ>SQL</if></trim> |
prefix=โwhere(set)โ ์์ฑ์ ์ฌ์ฉํ์ฌ WHERE์ SET์ ๊ตฌํ prefixOverrides์ ์ง์ ๋ ๋ฌธ์์ด๊ณผ ์ผ์นํ๋ ๋ฌธ์์ด์ ์ ๊ฑฐํ๊ณ prefix๋ก ์ง์ ํ ์ ๋์ด๋ฅผ ๋ถ์ธ๋ค. |
| <set> <if test=โ์กฐ๊ฑด1โ>SQL</if> <if test=โ์กฐ๊ฑด2โ>SQL</if> </set> |
UPDATE๋ฌธ์ SET์ ์ ๋ง๋ค๋ ์ฌ์ฉํ๋ค. <set> ์์ ์กฐ๊ฑด์์์ ์ผ์นํ๋ ์กฐ๊ฑด์ด ์์ผ๋ฉด SET์ ์ ๋ง๋ค์ด ๋ฐํํ๊ณ ์์ผ๋ฉด ๋ง๋ค์ง ์๋๋ค. SET์ ์ ํญ๋ชฉ์ด ์ฌ๋ฌ ๊ฐ์ผ ๊ฒฝ์ฐ ์๋์ผ๋ก ์ฝค๋ง(,)๋ฅผ ๋ถ์ธ๋ค. |
| <foreach item=โํญ๋ชฉโ index=โ์ธ๋ฑ์คโ collection=โ๋ชฉ๋กโ open=โ์์๋ฌธ์์ดโ close=โ์ข ๋ฃ๋ฌธ์์ดโ separator=โ๊ตฌ๋ถ์โ> </foreach> |
๋ชฉ๋กํ ๋ฐ์ดํฐ๋ก SQL๋ฌธ์ ๋ง๋ค๋ ์ฌ์ฉํ๋ค. ํนํ IN(๊ฐ, ๊ฐ, โฆ) ์กฐ๊ฑด์ ๋ง๋ค๋ ํธ๋ฆฌํ๋ค. item : ํ ๊ฐ์ ํญ๋ชฉ์ ๊ฐ๋ฆฌํค๋ ๋ณ์ ์ด๋ฆ ์ง์ index : ์ธ๋ฑ์ค ๊ฐ์ ๊บผ๋ผ๋ ์ฌ์ฉํ ๋ณ์ ์ด๋ฆ ์ง์ collection : java.util.List ๊ตฌํ์ฒด๋ ๋ฐฐ์ด ๊ฐ์ฒด ์ง์ open : ์ต์ข ๋ฐํ๊ฐ์ ์ ๋์ด ์ง์ close : ์ต์ข ๋ฐํ๊ฐ์ ์ ๋ฏธ์ด ์ง์ separator : ๊ตฌ๋ถ์ ๋ฌธ์์ด ์ง์ |
| <bind name=โ๋ณ์๋ช โ value=โ๊ฐโ/> | ๋ณ์๋ฅผ ์์ฑํ ๋ ์ฌ์ฉ |
์ฐธ๊ณ : https://atoz-develop.tistory.com/entry/MyBatis-%EB%8F%99%EC%A0%81-SQL-choose%EC%99%80-set%EC%9D%84-%EC%82%AC%EC%9A%A9%ED%95%98%EC%97%AC-%EB%8F%99%EC%A0%81-SQL-%EB%A7%8C%EB%93%A4%EA%B8%B0
๊ทธ์ธ ์์ ๋๋ฉด ์ข์ Tag
| ย | ย |
|---|---|
| <sql id=โ์๋ณ์โ> | ์์ฃผ์ฐ์ด๋ sql๋ฌธ์ ์ฝคํผ๋ํธ ๋ชจ๋ํ(?)ํ์ฌ ์ฌ์ฌ์ฉ์ฑ์ ์ค |
| <include refid=โsqlํ๊ทธโ> | <sql>๋ก ๋ง๋ค์ด๋ ์ฟผ๋ฆฌ๋ฌธ์ ๋ถ๋ฌ์ด |
์์
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="UserMapper10">
<resultMap id="userSelectMap" type="user">
<result property="userId" column="user_id" jdbcType="VARCHAR" />
<result property="userName" column="user_name" jdbcType="VARCHAR" />
<result property="password" column="password" jdbcType="VARCHAR" />
<result property="age" column="age" jdbcType="INTEGER" />
<result property="grade" column="grade" jdbcType="NUMERIC" />
<result property="regDate" column="regDate" jdbcType="DATE" />
</resultMap>
<insert id="addUser" parameterType="user">
INSERT
INTO users(user_id, user_name, password,age, grade ,reg_date)
VALUES( #{userId},
#{userName},
#{password},
#{age:INTEGER},
<if test="grade==0">null</if>
<if test="grade!=0">#{grade}</if>,
#{regDate:DATE})
</insert>
<select id="getUser" parameterType="string" resultMap="userSelectMap">
SELECT
user_id ,user_name ,password ,age ,grade ,reg_date
FROM users
WHERE user_id = #{value}
</select>
<update id="updateUser" parameterType="user">
UPDATE users
<set>
<if test="userName != null">user_name = #{userName},</if>
<if test="password != null">password = #{password},</if>
age = #{age:INTEGER},
grade = <if test="grade == 0">null</if>
<if test="grade != 0">#{grade}</if>
</set>
WHERE user_id = #{userId}
</update>
<delete id="removeUser" parameterType="java.lang.String">
DELETE
FROM users
WHERE user_id = #{value}
</delete>
<sql id="select-users">
SELECT
user_id , user_name , password , age , grade , reg_date
FROM users
</sql>
<sql id="orderby-userId-desc">
ORDER BY user_id DESC
</sql>
<select id="getUserList" parameterType="mybatis.service.domain.Search" resultMap="userSelectMap">
<include refid="select-users"/>
<where>
<choose>
<when test="searchCondition == 'userId'">
<if test="userId.size != 0">
user_id IN
<foreach item="value" collection="userId" open="(" separator="," close=")">
#{value}
</foreach>
</if>
</when>
<when test="searchCondition == 'userName'">
<if test="userName[0] != null">
user_name IN
<foreach item="value" collection="userName" open="(" separator="," close=")">
#{value}
</foreach>
</if>
</when>
</choose>
<!-- trim ๊ณผ where ์ฌ์ฉ ๊ฐ๋ฅ
<if test="searchCondition == 'userId'">
<if test="userId.size != 0">
user_id IN
<foreach item="value" collection="userId" open="(" separator="," close=")">
#{value}
</foreach>
</if>
</if>
<if test="searchCondition == 'userName'">
<if test="userName[0] != null">
user_name IN
<foreach item="value" collection="userName" open="(" separator="," close=")">
#{value}
</foreach>
</if>
</if>
-->
</where>
<include refid="orderby-userId-desc"/>
</select>
</mapper>
Leave a comment