mybatis 表与表 关联查询 (一)
@Mapper public interface DeptMapper { /** * 分步查询员工 * 及员工所对应的部门 * 分步查询第二步: * 通过部门查询员工所对应的部门 */ Dept getEmpAndDeptByTwo(@Param("did") Integer did); /** * 查询部门以及部门下面所有的员工信息 */ Dept getDeptAndEmp(@Param("did") Integer did); /** * 分步查询查询部门及部门中对应所有的员工信息 * 分步查询第一步:查询部门信息 */ Dept getDeptAndEmpByOne(@Param("did") Integer did);
mapper
<!--Dept getEmpAndDeptByTwo(@Param("did") Integer did);--> <select id="getEmpAndDeptByTwo" resultType="com.example.bootdemo.pojo.Dept"> select * from m_dept where did = #{did} </select> <resultMap id="deptAndEmpResultMap" type="com.example.bootdemo.pojo.Dept"> <id property="did" column="did"></id> <result property="deptName" column="dept_name"></result> <!-- collection:处理一对多的映射关系 ofType:表示该属性所对应的集合中存储数据的类型 --> <collection property="emps" ofType="com.example.bootdemo.pojo.Emp"> <id property="eid" column="eid"></id> <result property="empName" column="emp_name"></result> <result property="age" column="age"></result> <result property="sex" column="sex"></result> <result property="email" column="email"></result> </collection> </resultMap> <select id="getDeptAndEmp" resultMap="deptAndEmpResultMap"> select * from m_dept left join m_emp on m_dept.did = m_emp.did where m_dept.did = #{did} </select> <resultMap id="deptAndEmpByStepResultMap" type="com.example.bootdemo.pojo.Dept"> <id property="did" column="did"></id> <result property="deptName" column="dept_name"></result> <collection property="emps" select="com.example.bootdemo.mapper.EmpMapper.getDeptAndEmpByStepTwo" column="did" fetchType="eager"></collection> </resultMap> <select id="getDeptAndEmpByOne" resultMap="deptAndEmpByStepResultMap"> select * from m_dept where did = #{did} </select>
pojo
public class Emp { private Integer eid; private String empName; private Integer age; private String sex; private String email; private Dept dept;}
pojo
public class Dept { private Integer did; private String deptName; private List<Emp> emps;}
本文来自博客园,作者:格,转载请注明原文链接:https://www.cnblogs.com/yuangyuan/p/16451562.html