본문 바로가기
CODING STUDY/JAVA

[Java] association, collection을 사용하여 데이터 가져오기

by 놀고 쉬고 싶은 개발자 2024. 4. 12.

Mybatis 쓰다가 궁금한 점 얘넨 뭐가 다른데 또..

 

association

✔️ 1:1 매핑

✔️ DTO에 값을 mapping 할 때 사용

 

collection

✔️ 1:N 매핑

✔️ resultMap으로 DTO, VO mapping 할 때 List로 받을 때(여러 행 값을 조회할 때)


association의 예

DTO

 

► FirstDTO.java

public class FirstDTO {

    private Long firstKey;
    private String firstTitle;
    private Timestamp regDt;
    private Timestamp modDt;
    
    private SecondDTO secondDto;
    
}

 

► SecondDTO.java

public class SecondDTO {

    private Long secondKey;
    private String secondTitle;
    private Timestamp regDt;
    private Timestamp modDt;
    
}

 

Mybatis

 

► first.xml의 일부분

<mapper namespace="database.mappers.first.FirstMapper">
	<resultMap id="firstDto" type="database.dto.first.FirstDTO">
		<id property="firstKey"	column="first_key"	jdbcType="BIGINT"	javaType="java.lang.Long"/>
		<result property="firstTitle"	column="first_title"	jdbcType="VARCHAR"	javaType="java.lang.String"/>
		<result property="regDt"	column="reg_dt"	jdbcType="TIMESTAMP"	javaType="java.sql.Timestamp"/>
		<result property="modDt"	column="mod_dt"	jdbcType="TIMESTAMP"	javaType="java.sql.Timestamp"/>

		<association property="secondDto"	resultMap="database.mappers.second.SecondMapper.secondDto"/>
      </resultMap>

✔️ association에 resultMap의 경로는 second.xml 경로

<sql id="firstColumns">
	f1.first_key first_key,
	f1.first_title first_title,
	f1.reg_dt reg_dt,
	f1.mod_dt mod_dt
</sql>

 

► second.xml의 일부분

<mapper namespace="database.mappers.second.SecondMapper">
	<resultMap id="SecondDto" type="database.dto.second.SecondDTO">
		<id property="secondKey"	column="second_key"	jdbcType="BIGINT"	javaType="java.lang.Long"/>
		<result property="secondTitle"	column="second_title"	jdbcType="VARCHAR"	javaType="java.lang.String"/>
		<result property="regDt"	column="reg_dt"	jdbcType="TIMESTAMP"	javaType="java.sql.Timestamp"/>
		<result property="modDt"	column="mod_dt"	jdbcType="TIMESTAMP"	javaType="java.sql.Timestamp"/>
	</resultMap>

 

 

적용 쿼리 예시

 

► first.xml

<select id="selectAllData" resultMap="firstDto">
	SELECT
		t1.first_key first_key,
		t1.first_title first_title,
		t1.second_key second_key,
		t1.second_title second_title,
	FROM(
		SELECT
			<include refid="firstColumns"/>,
			s1.second_key
			s1.second_title,
		FROM tb_first f1
		LEFT JOIN tb_second s1 on f1.first_key = s1.second_key
	) t1
	ORDER by t1.first_key ASC
</select>

 

이렇게 되면 FirstDTO안에 선언란 secondDto에 값을 저장할 수 있다.

 


collection의 예

DTO

 

► FirstDTO.java

public class FirstDTO {

    private Long firstKey;
    private String firstTitle;
    private Timestamp regDt;
    private Timestamp modDt;

    private List<SecondDTO> secondDto;
}

 

► SecondDTO.java

public class SecondDTO {

    private Long secondKey;
    private String secondTitle;
    private Timestamp regDt;
    private Timestamp modDt;
    
}

 

► first.xml

<mapper namespace="database.mappers.first.FirstMapper">
	<resultMap id="firstVo" type="database.dto.first.FirstDTO">
		<id property="firstKey"	column="first_key"	jdbcType="BIGINT"	javaType="java.lang.Long"/>
		<result property="firstTitle"	column="first_title"	jdbcType="VARCHAR"	javaType="java.lang.String"/>
		<result property="regDt"	column="reg_dt"	jdbcType="TIMESTAMP"	javaType="java.sql.Timestamp"/>
		<result property="modDt"	column="mod_dt"	jdbcType="TIMESTAMP"	javaType="java.sql.Timestamp"/>

		<collection property="secondDto" javaType="List" ofType="database.dto.second.SecondDTO">
                    <id property="secondKey"	column="second_key"	jdbcType="BIGINT"	javaType="java.lang.Long"/>
		    <result property="secondTitle"	column="second_title"	jdbcType="VARCHAR"	javaType="java.lang.String"/>
                 </collection>
      </resultMap>

 

collection 안에

✔️ property=" " 은 FirstDto에 선언한 secondDto

✔️ javaType=" " 은 FirstDto에 선언한 secondDto의 타입

✔️ javaType=" " 은 FirstDto에 선언한 SecondDTO의 위치

 

이렇게 선언하고 원하는 쿼리를 만들면 secondDto 값이 List로 들어가게 된다.

 


최종적으로 들어가는 값이 1:1로 들어가는지 1:N으로 들어가는지에 따라 사용하면 될 거 같다.

 

실제로 association만 사용해봐서 추후에 collection도 사용해봐야겠다.