嵌套映射
1. 多对一嵌套查询映射使用案例
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.zixue.dao; import com.zixue.annotation.MyBatisRepository; import com.zixue.entity.Emp; /** * 员工表的DAO组件 * */ @MyBatisRepository public interface EmpDao { void save(Emp emp); Emp findById(int id); Emp findById2(int id); }
<!-- 使用嵌套查询 在查询一条员工数据时 关联查询出对应的部门 --> <select id="findById" parameterType="java.lang.Integer" resultMap="empMap"> select * from t_emp where empno=#{id} </select> <select id="findDept" parameterType="java.lang.Integer" resultType="com.zixue.entity.Dept"> select * from t_dept where deptno=#{deptno} </select> <resultMap type="com.zixue.entity.Emp" id="empMap"> <association property="dept" column="deptno" javaType="com.zixue.entity.Dept" select="findDept"> </association> </resultMap>
2.多对一嵌套结果映射使用案例
<!-- 使用嵌套结果 在查询一条员工数据时 关联查询出对应的部门 --> <select id="findById2" parameterType="java.lang.Integer" resultMap="empMap"> select e.*,d.* from t_emp e inner join t_dept d on e.deptno=d.deptno where e.empno=#{id} </select> <resultMap type="com.zixue.entity.Emp" id="empMap2"> <id property="empno" column="empno"/> <result property="ename" column="ename"/> <result property="job" column="job"/> <result property="mgr" column="mgr"/> <result property="hiredate" column="hiredate"/> <result property="sal" column="sal"/> <result property="comm" column="comm"/> <result property="deptno" column="deptno"/> <association property="dept" column="deptno" javaType="com.zixue.entity.Dept"> <id property="deptno" column="deptno"/> <result property="dname" column="dname"/> <result property="loc" column="loc"/> </association> </resultMap>
@Test public void testFindById2(){ ApplicationContext ac =new ClassPathXmlApplicationContext("applicationContext.xml"); EmpDao dao =ac.getBean(EmpDao.class); Emp e =dao.findById(100); System.out.println( e.getEname()+" "+e.getJob()+" "+e.getEmpno()+" "+e.getDept().getDeptno()+" "+e.getDept().getDname() );
3.一对多嵌套查询映射
/** * 关联属性 用于封装部门对应的员工信息 * */ private List<Emp> emps;
package com.zixue.dao; import com.zixue.annotation.MyBatisRepository; import com.zixue.entity.Dept; @MyBatisRepository public interface DeptDao { Dept findById(int id); Dept findById2(int id); }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<mapper namespace="com.zixue.dao.DeptDao"> <!-- 嵌套查询 --> <select id="findById" parameterType="java.lang.Integer" resultMap="deptMap"> select * from t_dept where deptno=#{id} </select> <select id="findEmps" parameterType="java.lang.Integer" resultType="com.zixue.entity.Emp"> select * from t_emp where deptno=#{deptno} </select> <resultMap type="com.zixue.entity.Dept" id="deptMap"> <id property="deptno" column="deptno"/> <collection property="emps" column="deptno" javaType="java.util.ArrayList" select="findEmps" ofType="com.zixue.entity.Emp"> </collection> </resultMap>
4.一对多嵌套结果查询
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<!-- 嵌套结果 --> <select id="findById2" parameterType="java.lang.Integer" resultMap="deptMap2"> select d.*,e.* from t_dept d inner join t_emp e on d.deptno=e.deptno where d.deptno=#{id} </select> <resultMap type="com.zixue.entity.Dept" id="deptMap2"> <id property="deptno" column="deptno"/> <result property="dname" column="dname" jdbcType="VARCHAR" javaType="string"/> <result property="loc" column="loc" jdbcType="VARCHAR" javaType="string"/> <collection property="emps" ofType="com.zixue.entity.Emp" javaType="java.util.ArrayList" column="deptno"> <id property="empno" column="empno"/> <result property="ename" column="ename"/> <result property="job" column="job"/> <result property="mgr" column="mgr"/> <result property="hiredate" column="hiredate"/> <result property="sal" column="sal"/> <result property="comm" column="comm"/> <result property="deptno" column="deptno"/> </collection> </resultMap>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/** * 一对多嵌套结果映射 * 查询部门及部门下所有的员工 * */ @Test public void testFindEmps2(){ ApplicationContext ac =new ClassPathXmlApplicationContext("applicationContext.xml"); DeptDao dao =ac.getBean(DeptDao.class); Dept d =dao.findById2(10); System.out.println( d.getDeptno()+" "+d.getDname()+" "+d.getLoc() ); List<Emp> emps =d.getEmps(); for(Emp e: emps){ System.out.println(e.getEname()+" "+e.getJob()+" "+e.getEmpno()); } }