1、DepartmentMapper.java添加代码
public Department getDeptByIdStep(Integer id);
2、EmployeeMapperPlus.java添加代码
public Employee getEmpByIdDeptId(Integer deptId);
3、DepartmentMapper.xml添加代码
<!-- public Department getDeptByIdStep(Integer id); --> <resultMap type="com.atguigu.mybatis.bean.Department" id="MyDeptStep"> <id column="id" property="id"/> <result column="departmentName" property="departmentName"/> <collection property="emps" select="com.atguigu.mybatis.dao.EmployeeMapperPlus.getEmpByIdDeptId" column="id"> </collection> </resultMap> <select id="getDeptByIdStep" resultMap="MyDeptStep"> select id,dept_name departmentName from tbl_dept where id=#{id} </select>
注:collection类似于association,先调用getDeptByIdStep方法获得tbl_dept中id的值,在用获取的id值做为参数传入到com.atguigu.mybatis.dao.EmployeeMapperPlus接口中getDeptByIdStep方法得到Employee 的所有参数值,最后封装到Department对象中
4、EmployeeMapperPlus.xml添加代码
<!-- public Employee getEmpByIdDeptId(Integer deptId); --> <resultMap type="com.atguigu.mybatis.bean.Employee" id="emps22"> <id column="id" property="id"/> <result column="last_name" property="lastName"/> </resultMap> <select id="getEmpByIdDeptId" resultMap="emps22"> select * from tbl_employee where d_id=#{deptId} </select>