Spring

07. 조인하기 [Spring]

민돌이 2016. 11. 7. 11:12
반응형

class A

private B b = new B();  

class B

private A a = new A();

--> class A 와 class B가 끝없이 생성됨  (x : 생성시키면 안됨)


class A

private B b;

setB(B b);


class B

private A a;

setA(A a);


외부에서 만들어서 보내--> 상관없다


조인된 결과를 하나의 쿼리로 묶는 방법?


1. resultMap 사용! (객체 가져올때 씀) 

 --> 맵핑되어서 객체만들어 보내줌  (컬럼이 같으면 공통적으로 써도됨)

resultMap   id 에는 Map을 붙인다 

pk는 id column은 PK

result column 은 컬럼

property에 클래스 변수명 쓰고, 알리아스 지운다.


<association property="departments" javaType="DepartmentsVO">

property : EmployeesVO 에 있는 변수명

알리아스 : javaType


<코드>

 <resultMap type="EmployeesVO" id="employeeMap">

            <id column="EMPLOYEE_ID" property="employeeId"/>

            <result column="FIRST_NAME" property="firstName"/>

            <result column="LAST_NAME" property="lastName"/>

            <result column="HIRE_DATE" property="hireDate"/>

            <result column="SALARY" property="salary"/>

            <result column="DEPARTMENT_ID" property="departmentId"/>

            <association property="departments" javaType="DepartmentsVO">

            <id column="DEPARTMENT_ID" property="departmentId"/>

            <result column="DEPARTMENT_NAME" property="departmentName"/>

            <result column="MANAGER_ID" property="managerId"/>

            <result column="LOCATION_ID" property="locationId"/>

            </association>

</resultMap>

           

           

<select id="getEmployeeByEmployeeId" resultMap="employeeMap" parameterType="string">

SELECT /* [indexDao.xml] [getEmployeeByEmployeeId] */

EMP.EMPLOYEE_ID 

, EMP.FIRST_NAME 

, EMP.LAST_NAME 

, EMP.HIRE_DATE 

, EMP.SALARY 

, EMP.DEPARTMENT_ID

, DEPT.DEPARTMENT_NAME

, DEPT.MANAGER_ID

, DEPT.LOCATION_ID

FROM         EMPLOYEES EMP

, DEPARTMENTS DEPT 

WHERE EMP.DEPARTMENT_ID = DEPT.DEPARTMENT_ID

AND EMP.EMPLOYEE_ID = #{employeeId}

</select>

getEmployeeByEmployeeId 와 getDepartmentByEmployeesDepartmentId 묶기 




<단건조인 1:1, 클래스 > 


Row1 = Row1

 --> association으로 처리한다.

(has one 관계)


<association property="departments" javaType="DepartmentsVO">

           <id column="DEPARTMENT_ID" property="departmentId"/>

           <result column="DEPARTMENT_NAME" property="departmentName"/>

           <result column="MANAGER_ID" property="managerId"/>

           <result column="LOCATION_ID" property="locationId"/>

</association>


<조인 1:다 리스트 has many> 


<resultMap type="EmployeesVO" id="getEmployeeByEmployeeIdMap">

<id column="EMPLOYEE_ID" property="employeesId"/>

<result column="FIRST_NAME" property="firstName"/>

<result column="LAST_NAME" property="lastName"/>

<result column="HIRE_DATE" property="hireDate"/>

<result column="SALARY" property="salary"/>

<result column="DEPARTMENT_ID" property="departmentId"/>

<collection property="departments" resultMap="DepartmentsVO"/>

</resultMap>


<resultMap type="DepartmentsVO" id="DepartmentsVO">

<id column="DEPARTMENT_ID" property="departmentId"/>

<result column="DEPARTMENT_NAME" property="departmentName"/>

<result column="MANAGER_ID" property="managerId"/>

<result column="LOCATION_ID" property="locationId"/>

</resultMap>



출처 : http://yoonuuu.tistory.com/68

반응형