resultMap
SQL 쿼리 κ²°κ³Όλ₯Ό Java κ°μ²΄λ‘ 맀ννλ νκ·Έμ
λλ€.
resultMapμ μ¬μ©νλ©΄ 볡μ‘ν SQL 쿼리 κ²°κ³Όλ₯Ό μ½κ² 맀νν μ μμΌλ©°, νΉν JOIN 쿼리 κ²°κ³Όλ₯Ό μ¬λ¬ κ°μ²΄λ‘ 맀νν λ μ μ©ν©λλ€.Β
resultMap μ μ¬μ©νλ©΄, μ€μ μ ν΅ν΄μ 1:1, 1:N JOIN 쿼리λ₯Ό μ μ©ν μ μμ΅λλ€.
κ°μ
β’
기본 ꡬ쑰
β¦
ꡬ쑰 μ€λͺ
β¦
κΈ°λ³Έ μ½λ
β’
μ£Όμ μμ
β’
μ£Όμ κΈ°λ₯
β’
μ£Όμ μμ
기본 ꡬ쑰
ꡬ쑰 μ€λͺ
<resultMap id="맀νID" type="ν¨ν€μ§.ν΄λμ€λͺ
">
<!-- κΈ°λ³Έ ν€λ₯Ό 맀ννλ μμ -->
<id property="μλ° κ°μ²΄μ λ³μλͺ
" column="DB ν
μ΄λΈμ 컬λΌλͺ
"/>
<!-- μΌλ° μμ±μ 맀ννλ μμ -->
<result property="μλ° κ°μ²΄μ λ³μλͺ
" column="DB ν
μ΄λΈμ 컬λΌλͺ
"/>
<!-- 1:1 κ΄κ³λ₯Ό 맀ννλ μμ -->
<association property="κ°μ²΄ λ³μλͺ
" javaType="ν¨ν€μ§.ν΄λμ€λͺ
(μ 2μ 맀νκ°μ²΄)">
<!-- μ€μ²©λ resultMap μμ -->
</association>
<!-- 1:N κ΄κ³λ₯Ό 맀ννλ μμ -->
<collection property="컬λ μ
λ³μλͺ
" ofType="μλ° μ»¬λ μ
νμ
">
<!-- μ€μ²©λ resultMap μμ -->
</collection>
</resultMap>
XML
볡μ¬
1.
<resultMap>:Β resultMapΒ μμλ 맀νμ μ μνλ λ£¨νΈ μμμ
λλ€.
β’
id:Β resultMapμ κ³ μ μλ³μμ
λλ€.
β’
type: 맀νν Java ν΄λμ€μ μ 체 μ΄λ¦μ
λλ€.
2.
<id>: κΈ°λ³Έ ν€λ₯Ό 맀ννλ λ° μ¬μ©λ©λλ€.
β’
property: Java κ°μ²΄μ μμ± μ΄λ¦μ
λλ€.
β’
column: SQL 쿼리 κ²°κ³Όμ μ΄ μ΄λ¦μ
λλ€.
3.
<result>: μΌλ° μμ±μ 맀ννλ λ° μ¬μ©λ©λλ€.
β’
property: Java κ°μ²΄μ μμ± μ΄λ¦μ
λλ€.
β’
column: SQL 쿼리 κ²°κ³Όμ μ΄ μ΄λ¦μ
λλ€.
4.
<association>: 1:1 κ΄κ³λ₯Ό 맀ννλ λ° μ¬μ©λ©λλ€.
β’
property: Java κ°μ²΄μ μμ± μ΄λ¦μ
λλ€.
β’
javaType: μ°κ΄λ Java ν΄λμ€μ μ 체 μ΄λ¦μ
λλ€.
β’
associationΒ μμ λ΄μ λ€λ₯ΈΒ resultMapΒ μμλ₯Ό μ€μ²©ν μ μμ΅λλ€.
5.
<collection>: 1:N κ΄κ³λ₯Ό 맀ννλ λ° μ¬μ©λ©λλ€.
β’
property: Java κ°μ²΄μ μμ± μ΄λ¦μ
λλ€.
β’
ofType: μ°κ΄λ Java ν΄λμ€μ μ 체 μ΄λ¦μ
λλ€.
β’
collectionΒ μμ λ΄μ λ€λ₯ΈΒ resultMapΒ μμλ₯Ό μ€μ²©ν μ μμ΅λλ€.
κΈ°λ³Έ μ½λ
<?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="com.aloha.thymeleaf_security.mapper.UserMapper">
<resultMap type="Users" id="UserMap">
<id property="no" column="no" />
<result property="no" column="no" />
<result property="username" column="username" />
<result property="password" column="password" />
<result property="name" column="name" />
<result property="email" column="email" />
<result property="enabled" column="enabled" />
<result property="createdAt" column="created_at" />
<result property="updatedAt" column="updated_at" />
</resultMap>
</mapper>
XML
볡μ¬
@Data
public class Users {
private Long no;
private String username;
private String password;
private String name;
private String email;
private Date createdAt;
private Date updatedAt;
private int enabled;
}
Java
볡μ¬
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`NO` bigint NOT NULL AUTO_INCREMENT,
`ID` varchar(255) NOT NULL,
`USERNAME` varchar(100) NOT NULL,
`PASSWORD` varchar(200) NOT NULL,
`NAME` varchar(100) NOT NULL,
`EMAIL` varchar(200) DEFAULT NULL,
`CREATED_AT` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`UPDATED_AT` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`ENABLED` int DEFAULT 1,
PRIMARY KEY (`NO`)
) COMMENT='νμ';
SQL
볡μ¬
μ£Όμ μμ
β’
<resultMap>
β¦
<id>
β¦
<result>
β¦
<association>
β¦
<collection>
μμ | μμ± | μ€λͺ
|
<resultMap> | id | resultMapμ κ³ μ μλ³μμ
λλ€. |
type | 맀νν Java ν΄λμ€μ μ 체 μ΄λ¦μ
λλ€. | |
<id> | property | Java κ°μ²΄μ μμ± μ΄λ¦μ
λλ€. |
column | SQL 쿼리 κ²°κ³Όμ μ΄ μ΄λ¦μ
λλ€. | |
javaType | (μ ν μ¬ν) 맀νν Java νμ
μ
λλ€. | |
jdbcType | (μ ν μ¬ν) 맀νν JDBC νμ
μ
λλ€. | |
typeHandler | (μ ν μ¬ν) μ¬μ©μ μ μ νμ
νΈλ€λ¬μ
λλ€. | |
<result> | property | Java κ°μ²΄μ μμ± μ΄λ¦μ
λλ€. |
column | SQL 쿼리 κ²°κ³Όμ μ΄ μ΄λ¦μ
λλ€. | |
javaType | (μ ν μ¬ν) 맀νν Java νμ
μ
λλ€. | |
jdbcType | (μ ν μ¬ν) 맀νν JDBC νμ
μ
λλ€. | |
typeHandler | (μ ν μ¬ν) μ¬μ©μ μ μ νμ
νΈλ€λ¬μ
λλ€. | |
<association> | property | Java κ°μ²΄μ μμ± μ΄λ¦μ
λλ€. |
javaType | μ°κ΄λ Java ν΄λμ€μ μ 체 μ΄λ¦μ
λλ€. | |
columnPrefix | (μ ν μ¬ν) μ΄ μ΄λ¦μ μ λμ¬μ
λλ€. | |
select | (μ ν μ¬ν) λ³λμ select λ¬Έμ μ§μ ν©λλ€. | |
resultMap | (μ ν μ¬ν) μ€μ²©λ resultMapμ IDμ
λλ€. | |
notNullColumn | (μ ν μ¬ν) nullμ΄ μλ μ΄μ μ§μ ν©λλ€. | |
fetchType | (μ ν μ¬ν) lazy λλ eager λ‘λ©μ μ§μ ν©λλ€. | |
<collection> | property | Java κ°μ²΄μ μμ± μ΄λ¦μ
λλ€. |
ofType | μ°κ΄λ Java ν΄λμ€μ μ 체 μ΄λ¦μ
λλ€. | |
columnPrefix | (μ ν μ¬ν) μ΄ μ΄λ¦μ μ λμ¬μ
λλ€. | |
select | (μ ν μ¬ν) λ³λμ select λ¬Έμ μ§μ ν©λλ€. | |
resultMap | (μ ν μ¬ν) μ€μ²©λ resultMapμ IDμ
λλ€. | |
notNullColumn | (μ ν μ¬ν) nullμ΄ μλ μ΄μ μ§μ ν©λλ€. | |
fetchType | (μ ν μ¬ν) lazy λλ eager λ‘λ©μ μ§μ ν©λλ€. |
<resultMap>
SQL 쿼리 κ²°κ³Όλ₯Ό Java κ°μ²΄λ‘ 맀ννλ λ£¨νΈ μμμ
λλ€.
μμ± | μ€λͺ
|
id | resultMapμ κ³ μ μλ³μμ
λλ€. |
type | 맀νν Java ν΄λμ€μ μ 체 μ΄λ¦μ
λλ€. |
<resultMap id="맡id" type="ν¨ν€μ§λͺ
.ν΄λμ€λͺ
">
</resultMap>
XML
볡μ¬
type μμ±μ κ°μ Java ν΄λμ€μ μ 체 μ΄λ¦ (ν¨ν€μ§λͺ
.ν΄λμ€λͺ
)μ μ§μ ν΄μΌν©λλ€.
typeAliases λ³μΉ μ€μ μ΄ λμ΄μμΌλ©΄ type μμ±κ°μΌλ‘ λ³μΉμ μ§μ ν μλ μμ΅λλ€.
typeAliases μ€μ λ°©λ²
mybatis μμ Java ν΄λμ€ μ 체μ΄λ¦(ν¨ν€μ§λͺ
.ν΄λμ€λͺ
)μ μ€μ νλ λ°©λ²μ μλ λ°©λ² μ€μ νλλ‘ μ μ©ν μ μμ΅λλ€.
β’
mybatis-config.xml
β’
@Alias μ΄λ
Έν
μ΄μ
mybatis-config.xml
β’
μ€μ νμΌ μμΉ
β¦
~/src/main/resources
βͺ
mybatis-config.xml
β’
mybatis-config.xml
<?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>
<!--
β typeAlias : λ³μΉ μ€μ
- alias : λ³μΉ
- type : λ³μΉμ μ¬μ©ν ν΄λμ€ κ²½λ‘
-->
<typeAlias alias="User" type="com.aloha.mybatis.dto.User"/>
<!--
β package : ν¨ν€μ§ κ²½λ‘ μ§μ
- name : ν¨ν€μ§ κ²½λ‘
-->
<package name="com.aloha.mybatis.dto"/>
</typeAliases>
</configuration>
XML
볡μ¬
β’
application.properties
# Mybatis μ€μ
# Mybatis μ€μ κ²½λ‘ : ~/resources/mybatis-config.xml
mybatis.config-location=classpath:mybatis-config.xml
# Mybatis λ§€νΌ νμΌ κ²½λ‘ : ~/λ©μΈν¨ν€μ§/mapper/**Mapper.xml
mybatis.mapper-locations=classpath:mybatis/mapper/**/**.xml
Java
볡μ¬
νμ
λ³μΉ package κ²½λ‘λ₯Ό μ€μ νλ©΄, ν΄λΉ ν¨ν€μ§ μλμ λͺ¨λ ν΄λμ€λ€μ ν΄λμ€ μ΄λ¦λ§μΌλ‘ type μ μ§μ ν μ μμ΅λλ€.
β’
typeAliases - package μ€μ
<resultMap id="맡id" type="ν¨ν€μ§λͺ
.ν΄λμ€λͺ
">
</resultMap>
XML
볡μ¬
β’
typeAliases - package μ€μ
<!-- <resultMap id="맡id" type="ν¨ν€μ§λͺ
.ν΄λμ€λͺ
"> -->
<resultMap id="맡id" type="ν΄λμ€λͺ
">
</resultMap>
XML
볡μ¬
mybatis-config.xml λ§μ΄λ°ν°μ€ μ€μ νμΌμ λ°λ‘ λ§λ€μ§ μκ³ ,
application.properties μ μμ±μΌλ‘ λ°λ‘ μ μ©ν μλ μμ΅λλ€.
# Mybatis μ€μ
# 컬λΌλͺ
μ λ³μλͺ
μΌλ‘ μλ맀ν : board_no β‘ boardNo
mybatis.configuration.map-underscore-to-camel-case=true
# resultType μμ±μμ dto κ°μ²΄ (ν¨ν€μ§.ν΄λμ€λͺ
) β‘ (ν΄λμ€λͺ
)
mybatis.type-aliases-package=com.aloha.mybatis.dto
# Mybatis λ§€νΌ νμΌ κ²½λ‘ : ~/λ©μΈν¨ν€μ§/mapper/**Mapper.xml
mybatis.mapper-locations=classpath:mybatis/mapper/**/**.xml
Java
볡μ¬
@Alias μ΄λ Έν μ΄μ
쿼리 κ²°κ³Όλ₯Ό 맀νν κ° κ°μ²΄μμ, @Alias μ΄λ
Έν
μ΄μ
μΌλ‘ λ³μΉμ μ€μ νλ λ°©λ²
package com.aloha.mybaits.dto;
...
@Data
@Alias("Users")
public class Users {
private Long no;
private String username;
private String password;
private String name;
private String email;
private Date createdAt;
private Date updatedAt;
private int enabled;
}
Java
볡μ¬
β’
@Alias("Users") μ€μ
<!-- β typeAliases μ€μ νμ§ μμ κ²½μ° - ν¨ν€μ§λͺ
.ν΄λμ€λͺ
-->
<select id="select" resultType="com.aloha.mybatis.dto.Users">
SELECT *
FROM user
WHERE username = #{username}
</select>
XML
볡μ¬
β’
@Alias("Users") μ€μ
<!-- β typeAliases μ€μ ν κ²½μ° - λ³μΉ (Users) -->
<select id="select" resultType="Users">
SELECT *
FROM user
WHERE username = #{username}
</select>
XML
볡μ¬
<id>
κΈ°λ³Έ ν€λ₯Ό 맀ννλ λ° μ¬μ©λλ μμμ
λλ€.
μμ± | μ€λͺ
|
property | Java κ°μ²΄μ μμ± μ΄λ¦μ
λλ€. |
column | SQL 쿼리 κ²°κ³Όμ μ΄ μ΄λ¦μ
λλ€. |
javaType | (μ ν μ¬ν) 맀νν Java νμ
μ
λλ€. |
jdbcType | (μ ν μ¬ν) 맀νν JDBC νμ
μ
λλ€. |
typeHandler | (μ ν μ¬ν) μ¬μ©μ μ μ νμ
νΈλ€λ¬μ
λλ€. |
<result>
μΌλ° μμ±μ 맀ννλ λ° μ¬μ©λλ μμμ
λλ€.
μμ± | μ€λͺ
|
property | Java κ°μ²΄μ μμ± μ΄λ¦μ
λλ€. |
column | SQL 쿼리 κ²°κ³Όμ μ΄ μ΄λ¦μ
λλ€. |
javaType | (μ ν μ¬ν) 맀νν Java νμ
μ
λλ€. |
jdbcType | (μ ν μ¬ν) 맀νν JDBC νμ
μ
λλ€. |
typeHandler | (μ ν μ¬ν) μ¬μ©μ μ μ νμ
νΈλ€λ¬μ
λλ€. |
<association>
1:1 κ΄κ³λ₯Ό 맀ννλ λ° μ¬μ©λλ μμμ
λλ€.
μμ± | μ€λͺ
|
property | Java κ°μ²΄μ μμ± μ΄λ¦μ
λλ€. |
javaType | μ°κ΄λ Java ν΄λμ€μ μ 체 μ΄λ¦μ
λλ€. |
columnPrefix | (μ ν μ¬ν) μ΄ μ΄λ¦μ μ λμ¬μ
λλ€. |
select | (μ ν μ¬ν) λ³λμ select λ¬Έμ μ§μ ν©λλ€. |
resultMap | (μ ν μ¬ν) μ€μ²©λ resultMapμ IDμ
λλ€. |
notNullColumn | (μ ν μ¬ν) nullμ΄ μλ μ΄μ μ§μ ν©λλ€. |
fetchType | (μ ν μ¬ν) lazy λλ eager λ‘λ©μ μ§μ ν©λλ€. |
<collection>
1:N κ΄κ³λ₯Ό 맀ννλ λ° μ¬μ©λλ μμμ
λλ€.
μμ± | μ€λͺ
|
property | Java κ°μ²΄μ μμ± μ΄λ¦μ
λλ€. |
ofType | μ°κ΄λ Java ν΄λμ€μ μ 체 μ΄λ¦μ
λλ€. |
columnPrefix | (μ ν μ¬ν) μ΄ μ΄λ¦μ μ λμ¬μ
λλ€. |
select | (μ ν μ¬ν) λ³λμ select λ¬Έμ μ§μ ν©λλ€. |
resultMap | (μ ν μ¬ν) μ€μ²©λ resultMapμ IDμ
λλ€. |
notNullColumn | (μ ν μ¬ν) nullμ΄ μλ μ΄μ μ§μ ν©λλ€. |
fetchType | (μ ν μ¬ν) lazy λλ eager λ‘λ©μ μ§μ ν©λλ€. |
μ£Όμ κΈ°λ₯
1.
κ°μ²΄ 맀ν (Object Mapping)
2.
1:1 μ‘°μΈ (One-to-One Join)
3.
1:N μ‘°μΈ (One-to-Many Join)
4.
λ€μ€ 맀ν (Multiple Mapping)
5.
μμ 맀ν (Inheritance Mapping)
6.
λ³΅ν© ν€ λ§€ν (Composite Key Mapping)
7.
λ€μμ€νμ΄μ€ μ¬μ© (Namespace Usage)
8.
μλ 맀ν (Auto Mapping)
resultMapμ MyBatisμμ SQL 쿼리 κ²°κ³Όλ₯Ό Java κ°μ²΄λ‘ 맀ννλ λ° μ¬μ©λ©λλ€.
μ΄λ₯Ό ν΅ν΄ λ€μν 맀ν μμ
μ μνν μ μμ΅λλ€.Β
1. κ°μ²΄ 맀ν (Object Mapping)
SQL 쿼리 κ²°κ³Όλ₯Ό Java κ°μ²΄μ μμ±μ 맀νν©λλ€. κ° μ»¬λΌμ κ°μ²΄μ μμ±μ 맀νν μ μμ΅λλ€.
<resultMap id="UserMap" type="User">
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="password" column="password"/>
<result property="name" column="name"/>
<result property="email" column="email"/>
<result property="enabled" column="enabled"/>
<result property="createdAt" column="created_at"/>
<result property="updatedAt" column="updated_at"/>
</resultMap>
XML
볡μ¬
2. 1:1 μ‘°μΈ (One-to-One Join)
λ€λ₯Έ ν
μ΄λΈκ³Όμ 1:1 κ΄κ³λ₯Ό 맀νν©λλ€.Β associationΒ μμλ₯Ό μ¬μ©νμ¬ λ€λ₯Έ κ°μ²΄μμ κ΄κ³λ₯Ό μ μν μ μμ΅λλ€.
<resultMap id="UserMap" type="User">
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="password" column="password"/>
<result property="name" column="name"/>
<result property="email" column="email"/>
<result property="enabled" column="enabled"/>
<result property="createdAt" column="created_at"/>
<result property="updatedAt" column="updated_at"/>
<association property="profile" javaType="Profile">
<id property="id" column="profile_id"/>
<result property="bio" column="bio"/>
<result property="website" column="website"/>
</association>
</resultMap>
XML
볡μ¬
3. 1:N μ‘°μΈ (One-to-Many Join)
λ€λ₯Έ ν
μ΄λΈκ³Όμ 1:N κ΄κ³λ₯Ό 맀νν©λλ€.Β collectionΒ μμλ₯Ό μ¬μ©νμ¬ μ»¬λ μ
μμ±μ μ μν μ μμ΅λλ€.
<resultMap id="UserMap" type="User">
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="password" column="password"/>
<result property="name" column="name"/>
<result property="email" column="email"/>
<result property="enabled" column="enabled"/>
<result property="createdAt" column="created_at"/>
<result property="updatedAt" column="updated_at"/>
<collection property="roles" ofType="Role">
<id property="id" column="role_id"/>
<result property="name" column="role_name"/>
</collection>
</resultMap>
XML
볡μ¬
4. λ€μ€ 맀ν (Multiple Mapping)
μ¬λ¬ ν
μ΄λΈμ κ²°κ³Όλ₯Ό νλμ κ°μ²΄μ 맀νν μ μμ΅λλ€.Β associationκ³ΌΒ collectionμ μ‘°ν©νμ¬ λ³΅μ‘ν 맀νμ μνν μ μμ΅λλ€.
<resultMap id="UserMap" type="User">
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="password" column="password"/>
<result property="name" column="name"/>
<result property="email" column="email"/>
<result property="enabled" column="enabled"/>
<result property="createdAt" column="created_at"/>
<result property="updatedAt" column="updated_at"/>
<association property="profile" javaType="Profile">
<id property="id" column="profile_id"/>
<result property="bio" column="bio"/>
<result property="website" column="website"/>
</association>
<collection property="roles" ofType="Role">
<id property="id" column="role_id"/>
<result property="name" column="role_name"/>
</collection>
</resultMap>
XML
볡μ¬
5. μμ 맀ν (Inheritance Mapping)
μμ κ΄κ³λ₯Ό 맀νν μ μμ΅λλ€.Β discriminatorΒ μμλ₯Ό μ¬μ©νμ¬ μμ κ΄κ³λ₯Ό μ μν μ μμ΅λλ€.
<resultMap id="vehicleMap" type="Vehicle">
<id property="id" column="id"/>
<result property="make" column="make"/>
<result property="model" column="model"/>
<discriminator javaType="string" column="type">
<case value="car" resultType="Car"/>
<case value="truck" resultType="Truck"/>
</discriminator>
</resultMap>
XML
볡μ¬
6. λ³΅ν© ν€ λ§€ν (Composite Key Mapping)
λ³΅ν© ν€λ₯Ό 맀νν μ μμ΅λλ€. μ¬λ¬ 컬λΌμ νλμ ν€λ‘ 맀νν μ μμ΅λλ€.
<resultMap id="orderMap" type="Order">
<id property="orderId" column="order_id"/>
<id property="productId" column="product_id"/>
<result property="quantity" column="quantity"/>
<result property="price" column="price"/>
</resultMap>
XML
볡μ¬
7. λ€μμ€νμ΄μ€ μ¬μ© (Namespace Usage)
λ€λ₯ΈΒ resultMapμ μ¬μ¬μ©ν μ μμ΅λλ€.Β extendsΒ μμ±μ μ¬μ©νμ¬ λ€λ₯ΈΒ resultMapμ νμ₯ν μ μμ΅λλ€.
<resultMap id="baseUserMap" type="User">
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="password" column="password"/>
</resultMap>
<resultMap id="extendedUserMap" extends="baseUserMap">
<result property="name" column="name"/>
<result property="email" column="email"/>
</resultMap>
XML
볡μ¬
8. μλ 맀ν (Auto Mapping)
MyBatisλ μλ 맀ν κΈ°λ₯μ μ 곡νμ¬ SQL κ²°κ³Όλ₯Ό μλμΌλ‘ Java κ°μ²΄μ 맀νν μ μμ΅λλ€.Β autoMappingΒ μμ±μ μ¬μ©νμ¬ μλ 맀νμ νμ±νν μ μμ΅λλ€.
<resultMap id="UserMap" type="User" autoMapping="true">
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="password" column="password"/>
<result property="name" column="name"/>
<result property="email" column="email"/>
<result property="enabled" column="enabled"/>
<result property="createdAt" column="created_at"/>
<result property="updatedAt" column="updated_at"/>
</resultMap>
XML
볡μ¬
resultMapμ μ¬μ©νλ©΄ MyBatisμμ λ€μν 맀ν μμ
μ μνν μ μμ΅λλ€. κ°μ²΄ 맀ν, 1:1 μ‘°μΈ, 1:N μ‘°μΈ, λ€μ€ 맀ν, μμ 맀ν, λ³΅ν© ν€ λ§€ν, λ€μμ€νμ΄μ€ μ¬μ©, μλ 맀ν λ± λ€μν κΈ°λ₯μ νμ©νμ¬ λ³΅μ‘ν SQL κ²°κ³Όλ₯Ό Java κ°μ²΄λ‘ 맀νν μ μμ΅λλ€. μ΄λ₯Ό ν΅ν΄ λ°μ΄ν°λ² μ΄μ€μ μ ν리μΌμ΄μ
κ°μ λ°μ΄ν° μ μ‘μ ν¨μ¨μ μΌλ‘ μ²λ¦¬ν μ μμ΅λλ€.
μ£Όμ μμ
β’
μ‘°μΈ
β¦
1:1 μ‘°μΈ - association
β¦
1:N μ‘°μΈ - collection
μ‘°μΈ
β’
1:1 μ‘°μΈ - association
β’
1:N μ‘°μΈ - collection
μμ κ°μ²΄ λ° ν μ΄λΈ
β’
user.sql
β’
user_auth.sql
β’
board.sql
β’
Users.java
β’
UserAuth.java
user.sql
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`NO` bigint NOT NULL AUTO_INCREMENT,
`ID` VARCHAR(255) NOT NULL COMMENT 'UK',
`USERNAME` varchar(100) NOT NULL,
`PASSWORD` varchar(200) NOT NULL,
`NAME` varchar(100) NOT NULL,
`EMAIL` varchar(200) DEFAULT NULL,
`CREATED_AT` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`UPDATED_AT` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`ENABLED` int DEFAULT 1,
PRIMARY KEY (`NO`)
) COMMENT='νμ';
SQL
볡μ¬
user_auth.sql
DROP TABLE IF EXISTS `user_auth`;
CREATE TABLE `user_auth` (
no bigint NOT NULL AUTO_INCREMENT -- κΆνλ²νΈ
`id` VARCHAR(255) NOT NULL COMMENT 'UK',
, username varchar(100) NOT NULL -- μμ΄λ
, auth varchar(100) NOT NULL -- κΆν (ROLE_USER, ROLE_ADMIN, ...)
, PRIMARY KEY(no)
);
SQL
볡μ¬
board.sql
DROP TABLE IF EXISTS `board`;
CREATE TABLE `board` (
`no` BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT 'PK',
`id` VARCHAR(255) NOT NULL COMMENT 'UK',
`title` VARCHAR(100) NOT NULL COMMENT 'μ λͺ©',
`user_no` BIGINT NOT NULL COMMENT 'νμPK',
`content` TEXT NULL COMMENT 'λ΄μ©',
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'λ±λ‘μΌμ',
`updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'μμ μΌμ'
);
ALTER TABLE `board` ADD CONSTRAINT `FK_users_TO_board_1` FOREIGN KEY (
`user_no`
)
REFERENCES `users` (
`no`
)
ON DELETE CASCADE
;
SQL
볡μ¬
Users.java
@Data
public class Users {
private Long no;
private String id;
private String username;
private String password;
private String name;
private String email;
private Date createdAt;
private Date updatedAt;
private int enabled;
private List<UserAuth> authList;
public Users() {
this.id = UUID.randomUUID().toString();
}
}
Java
볡μ¬
UserAuth.java
@Data
public class UserAuth {
private Long no;
private String id;
private String username;
private String auth;
private Date createdAt;
private Date updatedAt;
public UserAuth() {
this.id = UUID.randomUUID().toString();
}
}
Java
볡μ¬
Board.java
@Data
@AllArgsConstructor
@Builder
public class Board {
private Long no;
private String id;
private String title;
private Long userNo;
private String content;
private Date createdAt;
private Date updatedAt;
// μ μ
Users user;
public Board() {
this.id = UUID.randomUUID().toString();
}
}
Java
볡μ¬
1:1 μ‘°μΈ - association
association
MyBatisμμ 1:1 κ΄κ³λ₯Ό 맀ννκΈ° μν΄ μ¬μ©λλ μμμ
λλ€. μ΄λ SQL 쿼리 κ²°κ³Όλ₯Ό Java κ°μ²΄μ μμ±μΌλ‘ 맀νν λ, λ€λ₯Έ κ°μ²΄μμ 1:1 κ΄κ³λ₯Ό μ μνλ λ° μ¬μ©λ©λλ€.
λ°©λ²
β’
λ°©λ² 1 - JOIN 쿼리 μ§μ μμ±
β’
λ°©λ² 2 - 2κ°μ 쿼리λ₯Ό μ°κ²°
board ν
μ΄λΈκ³Ό user ν
μ΄λΈμ 1:1 λ‘ μ‘°μΈνμ¬,
board ν
μ΄λΈμ user_no λ‘ user ν
μ΄λΈμ no (PK) μ°Έμ‘°νμ¬ μ‘°νν©λλ€.
μ΄ λ, Board κ°μ²΄ μμ νλ(λ©€λ²λ³μ)μΈ Users κ°μ²΄μ μ‘°μΈ κ²°κ³Όλ₯Ό 맀νν©λλ€.
μ¬κΈ°μλ, κ²μκΈ μ 보 μ‘°ν μ, μ¬μ©μ μ 보(μ΄λ¦)μ μ°Έμ‘°νμ¬ μΆλ ₯νκΈ° μν΄ μ¬μ©ν©λλ€.
λ°©λ² 1 - JOIN 쿼리 μ§μ μμ±
<resultMap id="BoardMap" type="Board">
<!-- Board νλ 맀ν -->
<id property="no" column="no" />
<result property="id" column="id" />
<result property="title" column="title" />
<result property="content" column="content" />
<result property="userNo" column="user_no" />
<result property="createdAt" column="created_at" />
<result property="updatedAt" column="updated_at" />
<!-- Users κ°μ²΄ 맀ν -->
<association property="user" javaType="Users">
<id property="no" column="user_no" />
<result property="name" column="name" />
<result property="username" column="username"/>
</association>
</resultMap>
XML
볡μ¬
<select id="list" resultMap="BoardMap">
SELECT b.*
,u.no user_no
,u.name
,u.username
FROM board b JOIN user u ON b.user_no = u.no
</select>
XML
볡μ¬
λ°©λ² 2 - 2κ°μ 쿼리λ₯Ό μ°κ²°
<resultMap id="BoardMap" type="Board">
<!-- Board νλ 맀ν -->
<id property="no" column="no" />
<result property="id" column="id" />
<result property="title" column="title" />
<result property="content" column="content" />
<result property="userNo" column="user_no" />
<result property="createdAt" column="created_at" />
<result property="updatedAt" column="updated_at" />
<!-- Users κ°μ²΄ 맀ν -->
<association property="user" select="selectUser" column="user_no" ofType="com.aloha.security_method.domain.Users">
<id property="no" column="user_no" />
<result property="name" column="name" />
<result property="username" column="username"/>
</association>
</resultMap>
XML
볡μ¬
association
β’
property:
β¦
μ£Ό κ°μ²΄μ μμ± μ΄λ¦μ μ§μ ν©λλ€.
β¦
μ΄ μμ±μ μ°κ΄λ κ°μ²΄λ₯Ό μ μ₯ν μμ±μ
λλ€.
β¦
μ¬κΈ°μλ, Board κ°μ²΄μ Users user; λ³μλ‘ μ μΈλμ΄ μμ΅λλ€.
β’
select
β¦
μ°κ΄λ κ°μ²΄λ₯Ό μ‘°ννκΈ° μν SQL 쿼리λ₯Ό μ§μ ν©λλ€.
β¦
μ΄ μΏΌλ¦¬λ λ€λ₯Έ λ§€νΌ λ©μλλ‘ μ μλμ΄ μμ΄μΌ ν©λλ€.
β¦
μ¬κΈ°μλ, selectUser select νκ·Έ id λ‘ μΏΌλ¦¬κ° μ§μ λμ΄ μμ΅λλ€.
β’
column
β¦
μ£Ό κ°μ²΄μ μ»¬λΌ μ΄λ¦μ μ§μ ν©λλ€.
β¦
μ΄ μ»¬λΌμ κ°μ΄ μ°κ΄λ κ°μ²΄λ₯Ό μ‘°ννλ λ° μ¬μ©λ©λλ€.
β¦
μ¬κΈ°μλ, board ν
μ΄λΈμ user_no λ₯Ό μ§μ νμ¬, μλΈμΏΌλ¦¬μ μ λ¬νμ¬ μ‘°μΈν©λλ€.
β’
ofType
β¦
μ°κ΄λ κ°μ²΄μ νμ
μ μ§μ ν©λλ€.
β¦
μ΄ μμ±μ μ°κ΄λ κ°μ²΄μ Java ν΄λμ€ νμ
μ μ§μ ν©λλ€.
β¦
μ¬κΈ°μλ, Users κ°μ²΄λ‘ 맀νλλλ‘ μ§μ ν©λλ€.
β’
λ©μΈ 쿼리
<select id="list" resultMap="BoardMap">
SELECT *
FROM board
</select>
XML
볡μ¬
β’
μλΈ μΏΌλ¦¬
β¦
resultType μΌλ‘ μ§μ
β¦
resultMap μΌλ‘ μ§μ
β’
resultType μΌλ‘ μ§μ
<select id="selectUser" resultType="Users">
SELECT u.no user_no
,u.*
FROM user
WHERE user_no= #{user_no}
</select>
XML
볡μ¬
β’
resultMap μΌλ‘ μ§μ
<select id="selectUser" resultMap="UserMap">
SELECT u.no user_no
,u.*
FROM user
WHERE user_no= #{user_no}
</select>
XML
볡μ¬
(UserMap μ μμ§ λ―Έκ΅¬ν λ€μ 1:N μ‘°μΈ - collection μμ ꡬν)
resultMap μΌλ‘, JOIN μ, νμ 맀ν νμ
λ resultMap μ΄κ³ κ·Έ 맀νμμλ μ‘°μΈμ νκ³ μλ€λ©΄, μλμ κ°μ΄ κ³μΈ΅μ μΌλ‘ μ‘°νκ° κ°λ₯νλ€.
μ¦, κ²μκΈ μ 보μ νμ μ 보λ₯Ό μ‘°μΈνλ©΄μ, νμμ 보μ κΆν μ λ³΄κ° μ‘°μΈλμ΄ μ‘°νν μ μλ€.
β’
BoardMap
β¦
UserMap
βͺ
AuthMap
1:N μ‘°μΈ - collection
collection
MyBatisμμΒ collectionΒ μμλ 1:N κ΄κ³λ₯Ό 맀ννλ λ° μ¬μ©λ©λλ€. μ΄λ SQL 쿼리 κ²°κ³Όλ₯Ό Java κ°μ²΄μ 컬λ μ
μμ±μΌλ‘ 맀νν λ μ¬μ©λ©λλ€.
μμ
1.
user - user_auth JOIN
2.
board - user JOIN
user - user_auth JOIN
λ°©λ²
β’
λ°©λ² 1 - JOIN 쿼리 μ§μ μμ±
β’
λ°©λ² 2 - 2κ°μ 쿼리λ₯Ό μ°κ²°
user ν
μ΄λΈκ³Ό user_auth ν
μ΄λΈμ 1:N λ‘ μ‘°μΈνμ¬,
user ν
μ΄λΈμ username κ³Ό user_auth ν
μ΄λΈμ username λ₯Ό μ‘°μΈ μ‘°κ±΄μΌλ‘ μ‘°νν©λλ€.
μ΄ λ, Users κ°μ²΄ μμ νλ(λ©€λ²λ³μ)μΈ List<UserAuth> 컬λ μ
μ μ‘°μΈ κ²°κ³Όλ₯Ό 맀νν©λλ€.
μ¬κΈ°μλ, νμ μ 보 μ‘°ν μ, νμ κΆν λͺ©λ‘μ ν¨κ» μ‘°ννκΈ° μν΄ μ¬μ©ν©λλ€.
λ°©λ² 1 - JOIN 쿼리 μ§μ μμ±
<resultMap id="UserMap" type="com.aloha.security_method.domain.Users">
<id property="no" column="no" />
<result property="username" column="username" />
<result property="password" column="password" />
<result property="name" column="name" />
<result property="email" column="email" />
<result property="enabled" column="enabled" />
<result property="createdAt" column="created_at" />
<result property="updatedAt" column="updated_at" />
<collection property="authList" resultMap="authMap"></collection>
</resultMap>
<resultMap id="authMap" type="UserAuth">
<id property="no" column="auth_no" />
<result property="id" column="auth_id" />
<result property="username" column="username" />
<result property="userNo" column="user_no" />
<result property="auth" column="auth" />
<result property="createdAt" column="created_at" />
<result property="updatedAt" column="updated_at" />
</resultMap>
XML
볡μ¬
<select id="select" resultMap="UserMap">
SELECT u.no
,u.username
,password
,name
,email
,enabled
,u.created_at
,u.updated_at
,auth.no auth_no
,auth.id auth_id
,auth.user_no
,auth.auth auth
FROM user u LEFT OUTER JOIN user_auth auth ON u.username = auth.username
WHERE u.username = #{username}
</select>
XML
볡μ¬
λ°©λ² 2 - 2κ°μ 쿼리λ₯Ό μ°κ²°
<resultMap id="UserMap" type="com.aloha.security_method.domain.Users">
<id property="no" column="no" />
<result property="username" column="username" />
<result property="password" column="password" />
<result property="name" column="name" />
<result property="email" column="email" />
<result property="enabled" column="enabled" />
<result property="createdAt" column="created_at" />
<result property="updatedAt" column="updated_at" />
<collection property="authList" select="selectAuthList" column="username" javaType="list" ofType="com.car.ckauto.domain.users.UserAuth" ></collection>
</resultMap>
XML
볡μ¬
collection
β’
property:
β¦
μ£Ό κ°μ²΄μ μμ± μ΄λ¦μ μ§μ ν©λλ€.
β¦
μ΄ μμ±μ μ°κ΄λ κ°μ²΄λ₯Ό μ μ₯ν μμ±μ
λλ€.
β¦
μ¬κΈ°μλ, Users κ°μ²΄μ List<UserAuth> authList; λ³μλ‘ μ μΈλμ΄ μμ΅λλ€.
β’
select
β¦
μ°κ΄λ κ°μ²΄λ₯Ό μ‘°ννκΈ° μν SQL 쿼리λ₯Ό μ§μ ν©λλ€.
β¦
μ΄ μΏΌλ¦¬λ λ€λ₯Έ λ§€νΌ λ©μλλ‘ μ μλμ΄ μμ΄μΌ ν©λλ€.
β¦
μ¬κΈ°μλ, selectAuthList select νκ·Έ id λ‘ μΏΌλ¦¬κ° μ§μ λμ΄ μμ΅λλ€.
β’
column
β¦
μ£Ό κ°μ²΄μ μ»¬λΌ μ΄λ¦μ μ§μ ν©λλ€.
β¦
μ΄ μ»¬λΌμ κ°μ΄ μ°κ΄λ κ°μ²΄λ₯Ό μ‘°ννλ λ° μ¬μ©λ©λλ€.
β¦
μ¬κΈ°μλ, user ν
μ΄λΈμ username λ₯Ό μ§μ νμ¬, μλΈμΏΌλ¦¬μ μ λ¬νμ¬ μ‘°μΈν©λλ€.
β’
ofType
β¦
μ°κ΄λ κ°μ²΄μ νμ
μ μ§μ ν©λλ€.
β¦
μ΄ μμ±μ μ°κ΄λ κ°μ²΄μ Java ν΄λμ€ νμ
μ μ§μ ν©λλ€.
β¦
μ¬κΈ°μλ, UserAuth κ°μ²΄λ‘ 맀νλλλ‘ μ§μ ν©λλ€.
β’
λ©μΈ 쿼리
<select id="select" resultMap="UserMap">
SELECT *
FROM user u
WHERE u.username = #{username}
</select>
XML
볡μ¬
β’
μλΈ μΏΌλ¦¬
<select id="selectAuthList" resultType="UserAuth">
SELECT *
FROM user_auth
WHERE username = #{username}
</select>
XML
볡μ¬
board - user JOIN
β’
BoardMapper.xml
β’
UserMapper.xml
β’
JOIN ν
μ΄λΈ κ΄κ³
boad : user = 1 : 1
user : user_auth = 1 : N
BoardMapper.xml
<resultMap id="BoardMap" type="Board">
<!-- Board νλ 맀ν -->
<id property="no" column="no" />
<result property="id" column="id" />
<result property="title" column="title" />
<result property="content" column="content" />
<result property="userNo" column="user_no" />
<result property="createdAt" column="created_at" />
<result property="updatedAt" column="updated_at" />
<!-- Users κ°μ²΄ 맀ν -->
<association property="user" select="selectUser" column="user_no" javaType="com.aloha.security_method.domain.Users"></association>
</resultMap>
XML
볡μ¬
<select id="list" resultMap="BoardMap">
SELECT b.*
FROM board b
</select>
XML
볡μ¬
<select id="selectUser" resultMap="com.aloha.security_method.mapper.UserMapper.UserMap">
SELECT *
FROM user u
WHERE u.no = #{user_no}
</select>
XML
볡μ¬
UserMapper.xml
<resultMap id="UserMap" type="com.aloha.security_method.domain.Users">
<id property="no" column="no" />
<result property="username" column="username" />
<result property="password" column="password" />
<result property="name" column="name" />
<result property="email" column="email" />
<result property="enabled" column="enabled" />
<result property="createdAt" column="created_at" />
<result property="updatedAt" column="updated_at" />
<collection property="authList" select="selectAuthList" column="username" javaType="list" ofType="com.car.ckauto.domain.users.UserAuth" ></collection>
</resultMap>
XML
볡μ¬
<select id="select" resultMap="UserMap">
SELECT *
FROM user u
WHERE u.username = #{username}
</select>
XML
볡μ¬
<select id="selectAuthList" resultType="UserAuth">
SELECT *
FROM user_auth
WHERE username = #{username}
</select>
XML
볡μ¬
μ΄λ κ² resultMap μΌλ‘ μ°Έμ‘°νμ¬ JOIN μ νλ©΄, λ€μ€μ JOIN 쿼리λ₯Ό μ½κ² μμ±ν μ μλ€.
μ¬κΈ°μλ, κ²μκΈ λͺ©λ‘ μ‘°ν μ
β’
board : user = 1 : 1
β’
user : user_auth = 1 : N
μ΄λ κ² λ€μ€μΌλ‘ κ³μΈ΅μ μΌλ‘ κ²μκΈ λͺ©λ‘μ μ‘°νν μ μλ€.
- BoardMap
- UserMap
- AuthMap (or selectAuthList)
Plain Text
볡μ¬
Board
βββ no: 1
βββ id: 67610565-a89b-11ef-a47a-a8a1596f255e
βββ title: μ λͺ©1
βββ userNo: 3
βββ content: λ΄μ©1
βββ createdAt: Fri Nov 22 15:31:27 KST 2024
βββ updatedAt: Fri Nov 22 15:31:27 KST 2024
βββ user: Users
β βββ no: 3
β βββ id: 60b8c64d-a89b-11ef-a47a-a8a1596f255e
β βββ username: test
β βββ password: $2a$12$TrN..KcVjciCiz.5Vj96YOBljeVTTGJ9AUKmtfbGpgc9hmC7BxQ92
β βββ name: ν
μ€νΈ
β βββ email: test@mail.com
β βββ createdAt: Fri Nov 22 15:31:16 KST 2024
β βββ updatedAt: Fri Nov 22 15:31:16 KST 2024
β βββ enabled: 1
β βββ authList: List<UserAuth>
β βββ UserAuth
β βββ no: 4
β βββ id: 64816228-a89b-11ef-a47a-a8a1596f255e
β βββ userNo: 1
β βββ username: test
β βββ auth: ROLE_USER
β βββ createdAt: Fri Nov 22 15:31:23 KST 2024
β βββ updatedAt: Fri Nov 22 15:31:23 KST 2024
βββ fileList: null
βββ deleteFiles: null
Plain Text
볡μ¬