eg:员工信息以及部门的多对一,表详情如下:
员工信息表:
部门表:
分步查询也就是先查询出员工信息然后通过did来查询其对应的部门:
先在EmpMapper接口中设置方法:
Emp getEmpAndDeptByStepOne(@Param("eid") int eid );
然后在EmpMapper.xml配置文件中设置:
<select id="getEmpAndDeptByStepOne" resultMap="empAndDeptByStepResultMap">
select * from t_emp where eid=#{eid}
</select>
<resultMap id="empAndDeptByStepResultMap" type="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>
<!--此处就是第二步操作,通过did来查询员工所对应的部门-->
<association property="dept"
select="com.chenry.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"
column="did"
></association>
</resultMap>
第二步的具体操作需要设置在dept的相关代码中:
先在dept.mapper接口中声明方法:
Dept getEmpAndDeptByStepTwo(@Param("did") int did);
然后在deptMapper.xml文件中配置
<mapper namespace="com.chenry.mybatis.mapper.DeptMapper">
<select id="getEmpAndDeptByStepTwo" resultType="Dept">
select * from t_dept where did=#{did}
</select>