待整理。。。

结果映射

一对多

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!-- type:对象类型  orm -->
<resultMap type="dept" id="deptMap">
<!-- 主键字段映射 -->
<id column="deptno" property="deptno"/>
<!-- 非主键字段映射 -->
<result column="dname" property="dname"/>
<result column="loc" property="loc"/>
<!-- 封装结果到集合 property:属性 javaType:每个对象类型 -->
<collection property="emps" ofType="emp" column="deptno">
<id column="empno" property="empno"/>
<result column="ename" property="ename"/>
<result column="job" property="job"/>
<result column="hiredate" property="hiredate"/>
<result column="mgr" property="mgr"/>
<result column="sal" property="sal"/>
<result column="comm" property="comm"/>
<result column="deptno" property="deptno"/>
</collection>
</resultMap>

一对一

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<resultMap type="emp" id="empMap">
<id column="empno" property="empno"/>
<result column="ename" property="ename"/>
<result column="job" property="job"/>
<result column="hiredate" property="hiredate"/>
<result column="mgr" property="mgr"/>
<result column="sal" property="sal"/>
<result column="comm" property="comm"/>
<result column="deptno" property="deptno"/>
<!-- 封装结果到对象中 -->
<association property="dept" column="deptno" javaType="dept">
<id column="deptno" property="deptno"/>
<result column="dname" property="dname"/>
<result column="loc" property="loc"/>
</association>
</resultMap>
<select id="findEmpAndDept" resultMap="empMap">
select * from emp left join dept on emp.deptno = dept.deptno
</select>