mybatis - low-hill/Knowledge GitHub Wiki
<where>
<choose>
<when test="ids == null">
<if test="name != null">
displayNm LIKE CONCAT('%', #{name}, '%')
</if>
</when>
<otherwise>
AND id IN ( <foreach collection="ids" item="id" separator=",">#{id}</foreach> )
</otherwise>
</choose>
</where>
insert into user(name, gender, email)
values
<foreach collection="members" item="item" separator=",">
(
#{item.name}
, #{item.gender}
, #{item.email}
)
</foreach>
on duplicate key update
name = values(name)
gender = values(gender)
email = values(email)
useGeneratedKeys=โtrueโ ๋ก ์ค์ ํ๊ณ keyProperty์ ์๋์์ฑํค ์ปฌ๋ผ์ ์ ํ
<insert id="insertAuthor" useGeneratedKeys="true" keyProperty="id">
insert into Author (username,password,email,bio)
values (#{username},#{password},#{email},#{bio})
</insert>
- ๋ค์ค ๋ ์ฝ๋ ์ ๋ ฅ ์
<insert id="insertAuthor" useGeneratedKeys="true"
keyProperty="id">
insert into Author (username, password, email, bio) values
<foreach item="item" collection="list" separator=",">
(#{item.username}, #{item.password}, #{item.email}, #{item.bio})
</foreach>
</insert>
- keyProperty: selectKey๊ตฌ๋ฌธ์ ๊ฒฐ๊ณผ๊ฐ ์ ํ ๋ property(resultType[class]์ ๋ฉค๋ฒ๋ณ์์ ์ผ์น)
- resultType: ๊ฒฐ๊ณผ์ ํ์
- order: BEFORE / AFTER
- BEFORE: selectKey๊ตฌ๋ฌธ์ ์ฟผ๋ฆฌ๋ฅผ ๋จผ์ ์คํ
- AFTER: selectKey๊ตฌ๋ฌธ์ ์ฟผ๋ฆฌ๋ฅผ ๋ง์ง๋ง์ ์คํ
<insert id="addUser" parameterType="User">
<selectKey keyProperty="userId,userName" resultType="User" order="AFTER">
SELECT USER_ID AS userId
, USER_NAME AS userName
FROM USER
ORDER BY USER_ID DESC
LIMIT 1
</selectKey>
INSERT INTO USER(col1, ...)
VALUES (#{...}, ...)
</insert>
- [https://mybatis.org/mybatis-3/]
- [https://deeplify.dev/back-end/spring/select-key]
- constructor - ์ธ์คํด์คํ๋๋ ํด๋์ค์ ์์ฑ์์ ๊ฒฐ๊ณผ๋ฅผ ์ฝ์
ํ๊ธฐ ์ํด ์ฌ์ฉ๋จ
- idArg - ID ์ธ์. ์ ๋ฐ์ ์ผ๋ก ์ฑ๋ฅ์ ํฅ์
- arg - ์์ฑ์์ ์ฝ์ ๋๋ ์ผ๋ฐ์ ์ธ ๊ฒฐ๊ณผ
- id โ ID ๊ฒฐ๊ณผ. ์ ๋ฐ์ ์ผ๋ก ์ฑ๋ฅ์ ํฅ์
<id property="id" column="์ปฌ๋ผ๋ช
"/>
- result โ ํ๋๋ ์๋ฐ๋น ํ๋กํผํฐ์ ์ฝ์ ๋๋ ์ผ๋ฐ์ ์ธ ๊ฒฐ๊ณผ
<result property="ํ๋๋ช
" column="์ปฌ๋ผ๋ช
"/>
- association โ ๋ณต์กํ ํ์ ์ ์ฐ๊ด๊ด๊ณ. ๋ง์ ๊ฒฐ๊ณผ๋ ํ์ ์ผ๋ก ๋ํ๋๋ค.
- ์ค์ฒฉ๋ ๊ฒฐ๊ณผ ๋งคํ โ resultMap ์ค์ค๋ก์ ์ฐ๊ด๊ด๊ณ
- collection โ ๋ณต์กํ ํ์ ์ ์ปฌ๋ ์
- ์ค์ฒฉ๋ ๊ฒฐ๊ณผ ๋งคํ โ resultMap ์ค์ค๋ก์ ์ฐ๊ด๊ด๊ณ
- discriminator โ ์ฌ์ฉํ resultMap ์ ํ๋จํ๊ธฐ ์ํ ๊ฒฐ๊ณผ๊ฐ์ ์ฌ์ฉ
- case โ ๋ช๊ฐ์ง ๊ฐ์ ๊ธฐ์ดํ ๊ฒฐ๊ณผ ๋งคํ
association ์๋ฆฌ๋จผํธ๋ โhas-oneโํ์ ์ ๊ด๊ณ๋ฅผ ๋ค๋ฃฌ๋ค.
๋ง์ด๋ฐํฐ์ค๋ ๊ด๊ณ๋ฅผ ์ ์ํ๋ ๋๊ฐ์ง ๋ฐฉ๋ฒ์ ์ ๊ณตํ๋ค.
- ๋ดํฌ๋(Nested) Select: ๋ณต์กํ ํ์ ์ ๋ฆฌํดํ๋ ๋ค๋ฅธ ๋งคํ๋ SQL ๊ตฌ๋ฌธ์ ์คํํ๋ ๋ฐฉ๋ฒ.
- ๋ดํฌ๋(Nested) Results: ์กฐ์ธ๋ ๊ฒฐ๊ณผ๋ฌผ์ ๋ฐ๋ณต์ ์ผ๋ก ์ฌ์ฉํ์ฌ ๋ดํฌ๋ ๊ฒฐ๊ณผ ๋งคํ์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ.
<resultMap id="blogResult" type="Blog">
<association property="author" column="author_id" javaType="Author" select="selectAuthor"/>
</resultMap>
<select id="selectBlog" resultMap="blogResult">
SELECT * FROM BLOG WHERE ID = #{id}
</select>
<select id="selectAuthor" resultType="Author">
SELECT * FROM AUTHOR WHERE ID = #{id}
</select>
์ ๋ฐฉ๋ฒ์ โN+1 Selects ๋ฌธ์ โ ์ผ๋ก ์๋ ค์ง ๋ฌธ์ ์ ์ ๊ฐ์ง๋ค. N+1 ์กฐํ ๋ฌธ์ ๋ ์ฒ๋ฆฌ๊ณผ์ ์ ํน์ด์ฑ์ผ๋ก ์ธํด ์ผ๊ธฐ๋๋ค.
- ๋ ์ฝ๋์ ๋ชฉ๋ก์ ๊ฐ์ ธ์ค๊ธฐ ์ํด ํ๋์ SQL ๊ตฌ๋ฌธ์ ์คํํ๋ค. (โ+1โ ์ ํด๋น).
- ๋ฆฌํด๋ ๋ ์ฝ๋๋ณ๋ก ๊ฐ๊ฐ์ ์์ธ ๋ฐ์ดํฐ๋ฅผ ๋ก๋ํ๊ธฐ ์ํด select ๊ตฌ๋ฌธ์ ์คํํ๋ค. (โNโ ์ ํด๋น). ์ด ๋ฌธ์ ๋ ์๋ฐฑ ๋๋ ์์ฒ์ SQL ๊ตฌ๋ฌธ ์คํ์ด๋ผ๋ ๊ฒฐ๊ณผ๋ฅผ ์ผ๊ธฐํ ์ ์๋ค. ๋ชฉ๋ก์ ๋ก๋ํ๊ณ ๋ดํฌ๋ ๋ฐ์ดํฐ์ ์ ๊ทผํ๊ธฐ ์ํด ์ฆ์ ๋ฐ๋ณต์ ์ผ๋ก ์ฒ๋ฆฌํ๋ค๋ฉด ์ง์ฐ๋ก๋ฉ์ผ๋ก ํธ์ถํ๊ณ ๊ฒ๋ค๊ฐ ์ฑ๋ฅ์ ๋ง์ด ๋๋น ์ง ๊ฒ์ด๋ค. ๊ทธ๋์ ๋ค๋ฅธ ๋ฐฉ๋ฒ์ด ์๋ค.
๊ฐ๋ณ๊ตฌ๋ฌธ์ ์คํํ๋ ๊ฒ ๋์ ์ ํ ์ด๋ธ์ ํจ๊ป ์กฐ์ธ
<resultMap id="blogResult" type="Blog">
<id property="id" column="blog_id" />
<result property="title" column="blog_title"/>
<association property="author" javaType="Author">
<id property="id" column="author_id"/>
<result property="username" column="author_username"/>
<result property="password" column="author_password"/>
<result property="email" column="author_email"/>
<result property="bio" column="author_bio"/>
</association>
</resultMap>
<select id="selectBlog" resultMap="blogResult">
select
B.id as blog_id,
B.title as blog_title,
B.author_id as blog_author_id,
A.id as author_id,
A.username as author_username,
A.password as author_password,
A.email as author_email,
A.bio as author_bio
from Blog B left outer join Author A on B.author_id = A.id
where B.id = #{id}
</select>
- id ์๋ฆฌ๋จผํธ๋ ๋ดํฌ๋ ๊ฒฐ๊ณผ ๋งคํ์์ ๋งค์ฐ ์ค์ํ ์ญํ ์ ๋ด๋นํ๋ค. ๊ฒฐ๊ณผ ์ค ์ ์ผํ ๊ฒ์ ์ฐพ์๋ด๊ธฐ ์ํ ํ๊ฐ ์ด์์ ํ๋กํผํฐ๋ฅผ ๋ช ์ํด์ผ๋ง ํ๋ค. ๊ฐ๋ฅํ๋ฉด ๊ฒฐ๊ณผ ์ค ์ ์ผํ ๊ฒ์ ์ฐพ์๋ผ ์ ์๋ ํ๋กํผํฐ๋ค์ ์ ํํ๋ผ. ๊ธฐ๋ณธํค๊ฐ ๊ฐ์ฅ ์ข์ ์ ํ์ด ๋ ์ ์๋ค.
<resultMap id="blogResult" type="Blog">
<id property="id" column="blog_id" />
<result property="title" column="blog_title"/>
<association property="author"
resultMap="authorResult" />
<association property="coAuthor"
resultMap="authorResult"
columnPrefix="co_" /> <!--๊ฒฐ๊ณผ๋งคํ์ ์ฌ์ฌ์ฉํ๊ธฐ ์ํด columnPrefix๋ฅผ ๋ช
์-->
</resultMap>
<resultMap id="authorResult" type="Author">
<id property="id" column="author_id"/>
<result property="username" column="author_username"/>
<result property="password" column="author_password"/>
<result property="email" column="author_email"/>
<result property="bio" column="author_bio"/>
</resultMap>
โhas manyโํ์ ์ ๊ด๊ณ๋ฅผ ๋ค๋ฃฌ๋ค.
<resultMap id="blogResult" type="Blog">
<id property="id" column="blog_id" />
<result property="title" column="blog_title"/>
<collection property="posts" ofType="Post">
<id property="id" column="post_id"/>
<result property="subject" column="post_subject"/>
<result property="body" column="post_body"/>
</collection>
</resultMap>
- insert statement
<insert id="insertName">
insert into table (col1) values (#{value})
</insert>
- execute a batch in java code
List<String> names = new ArrayList<String>();
names.add("Fred");
names.add("Barney");
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
try{
NameMapper mapper = sqlSession.getMapper(NameMapper.class);
for(String name : names)
mapper.insertName(name);
sqlSession.commit();
}finally{
sqlSession.close();
}
- [https://mybatis.org/mybatis-3/ko/sqlmap-xml.html]
- https://github.com/mybatis/mybatis-3/wiki/FAQ#how-do-i-code-a-batch-insert