<resultMapid="BaseResultMap"type="Person"><idcolumn="p_id"property="id"></id><resultcolumn="p_name"property="name"></result></resultMap><resultMapid="PersonResultMap1"type="Person"extends="BaseResultMap"><associationproperty="idcard"javaType="Idcard"><idcolumn="c_id"property="id"></id><resultcolumn="c_cardno"property="cardno"></result><resultcolumn="c_uselife"property="uselife"></result></association></resultMap><!-- 根据部门id查找部门 以及所在部门所有员工的所有信息【嵌套结果查询,联合查询】 --><selectid="getPersonById1"parameterType="int"resultType="Person"resultMap="PersonResultMap1">
select p.*,c.* from person p,idcard c where p.p_id=c.c_person_id and p.p_id=#{id}
</select>
<!-- 根据人的id查询人与信息 和 身份证信息 【嵌套查询,分表查询】 --><selectid="getPersonById2"parameterType="int"resultType="Person"resultMap="PersonResultMap2">
select * from person where p_id=#{id}
</select><resultMapid="BaseResultMap"type="Person"><idcolumn="p_id"property="id"></id><resultcolumn="p_name"property="name"></result></resultMap><resultMapid="PersonResultMap2"type="Person"extends="BaseResultMap"><associationproperty="idcard"javaType="Idcard"column="p_id"select="com.chenshuang.mapper.IdcardMapper.getIdcardById"></association></resultMap>
<mappernamespace="com.chenshuang.mapper.IdcardMapper"><resultMapid="BaseResultMap"type="Idcard"><idcolumn="c_id"property="id"></id><resultcolumn="c_cardno"property="cardno"></result><resultcolumn="c_uselife"property="uselife"></result></resultMap><selectid="getIdcardById"parameterType="int"resultType="Idcard"resultMap="BaseResultMap">
select * from idcard where c_id=#{id}
</select></mapper>
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPEmapperPUBLIC"-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mappernamespace="cn.offcn.mapper.DepartmentDao"><selectid="getDepartmentById"resultMap="DepartemntResultMap2">
SELECT d.*,e.* FROM department d, employee e WHERE
e.e_depart_id=d.d_id AND d.d_id=#{id}
</select><resultMapid="DepartemntResultMap"type="Department"><idcolumn="d_id"property="id"></id><resultcolumn="d_name"property="name"></result><collectionproperty="emps"ofType="Employee"><idcolumn="e_id"property="id"></id><resultcolumn="e_name"property="name"></result><resultcolumn="e_gender"property="gender"></result><resultcolumn="e_age"property="age"></result></collection></resultMap>
collection:当属性为集合时,使用collection标签进行映射。
<select id="getEmployeeByDepartId" resultType="Employee">
select e_id id,e_name name,e_gender gender,e_age age
from employee where e_depart_id=#{id}</select>
3)定义DepartmentMapper.xml配置文件
<selectid="getDepartmentById"resultMap="DepartemntResultMap">
select * from department where d_id=#{id}
</select><resultMapid="DepartemntResultMap"type="Department"><idcolumn="d_id"property="id"></id><resultcolumn="d_name"property="name"></result><collectionproperty="emps"ofType="Employee"column="d_id"select="cn.offcn.mapper.EmployeeMapper.getEmployeeByDepartId"></collection></resultMap>
此处变为单表查询,使分表查询方式进行查询。
<resultMapid="BaseResultMap"type="Department"><idcolumn="d_id"property="id"></id><resultcolumn="d_name"property="name"></result></resultMap><selectid="getDepartmentById"parameterType="int"resultType="Department"resultMap="BaseResultMap">
select * from department where d_id=#{id}
</select>
<!-- 根据学生id查 学生信息 和老师的信息 【嵌套结果查询,联合查询】 --><selectid="getStudentById1"parameterType="int"resultType="Student"resultMap="StudentResultMap1">
SELECT s.*,t.*,st.*
FROM student s,teacher t,student_teacher st
where s.id=st.st_sid AND t.t_id=st.st_tid AND s.id=#{id}
</select><resultMapid="StudentResultMap1"type="Student"><idcolumn="id"property="id"></id><resultcolumn="name"property="name"></result><resultcolumn="gender"property="gender"></result><resultcolumn="age"property="age"></result><resultcolumn="s_birthday"property="birthday"></result><collectionproperty="studentTeachers"ofType="StudentTeacher"><resultcolumn="st_sid"property="sid"></result><resultcolumn="st_tid"property="tid"></result><associationproperty="teacher"javaType="Teacher"><idcolumn="t_id"property="id"></id><resultcolumn="t_name"property="name"></result></association></collection></resultMap>
<resultMapid="BaseResultMap"type="StudentTeacher"><resultcolumn="st_sid"property="sid"></result><resultcolumn="st_tid"property="tid"></result></resultMap><resultMapid="StudentTeacherResultMap"type="StudentTeacher"extends="BaseResultMap"><associationproperty="teacher"javaType="Teacher"column="st_tid"select="com.chenshuang.mapper.TeacherMapper.getTeacherById"></association></resultMap><selectid="getStudentTeacherBySid"parameterType="int"resultType="StudentTeacher"resultMap="StudentTeacherResultMap">
select * from student_teacher where st_sid=#{id}
</select>
<resultMapid="BaseResultMap"type="Teacher"><idcolumn="t_id"property="id"></id><resultcolumn="t_name"property="name"></result></resultMap><selectid="getTeacherById"resultType="Teacher"parameterType="int"resultMap="BaseResultMap">
select * from teacher where t_id=#{id}
</select>