Search

resultMap

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
β€’
application.properties
β€’
@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 μ–΄λ…Έν…Œμ΄μ…˜

mybatis-config.xml λ˜λŠ” application properties 에 MyBatisκ°€ μ§€μ •λœ νŒ¨ν‚€μ§€ λ‚΄μ˜ ν΄λž˜μŠ€μ— λŒ€ν•΄ νƒ€μž… 별칭을 μžλ™μœΌλ‘œ μŠ€μΊ”ν•˜λ„λ‘ μ„€μ •ν•΄μ•Ό @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
볡사