<?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"> <!-- namespace表示命名空间 保证它是唯一 cn.itsource.mybatis.dao.impl.ProductDaoImpl + id="getUserById"--> <mapper namespace="_04_onetomany.DeptMapper"> <!-- 一对多 嵌套结果 --> <select id="query01" resultMap="deptMp"> select d.id,d.name,e.id eid,e.name ename from dept d left join employee e on d.id = e.dept_id </select> <resultMap id="deptMp" type="_04_onetomany.Dept"> <id column="id" property="id"></id> <result column="name" property="name"></result> <!--property 类里面属性 javaType集合属性 ofType集合里面类型--> <collection property="employees" javaType="arraylist" ofType="_04_onetomany.Employee"> <id column="eid" property="id"></id> <result column="ename" property="name"></result> </collection> </resultMap> <!-- 一对多嵌套查询 --> <select id="query02" resultMap="deptMp1"> select * from dept </select> <resultMap id="deptMp1" type="_04_onetomany.Dept"> <id column="id" property="id"></id> <result column="name" property="name"></result> <!--property 类里面属性 javaType集合属性 ofType集合里面类型--> <collection property="employees" column="id" select="getEmployeeByDeptId"> </collection> </resultMap> <select id="getEmployeeByDeptId" parameterType="long" resultType="_04_onetomany.Employee"> select * from employee where dept_id =#{id} </select> </mapper>