<resultMapid="empAndDeptResultMapOne"type="Emp"><idproperty="eid"column="eid"/><idproperty="empName"column="emp_name"/><idproperty="age"column="age"/><idproperty="sex"column="sex"/><idproperty="email"column="email"/><idproperty="dept.did"column="did"/><idproperty="dept.deptName"column="dept_name"/></resultMap><!--Emp getEmpAndDept(@Param("eid") Integer eid);--><selectid="getEmpAndDept"resultMap="empAndDeptResultMapOne">
select * from t_emp left join t_dept on t_emp.did = t_dept.did where t_emp.eid=#{eid}
</select>
②使用association
<resultMapid="empAndDeptResultMapTwo"type="Emp"><idproperty="eid"column="eid"/><idproperty="empName"column="emp_name"/><idproperty="age"column="age"/><idproperty="sex"column="sex"/><idproperty="email"column="email"/><!--
association:处理多对一的映射关系
javaType:该属性的类型
--><associationproperty="dept"javaType="Dept"><idproperty="did"column="did"/><idproperty="deptName"column="dept_name"/></association></resultMap><!--Emp getEmpAndDept(@Param("eid") Integer eid);--><selectid="getEmpAndDept"resultMap="empAndDeptResultMapTwo">
select * from t_emp left join t_dept on t_emp.did = t_dept.did where t_emp.eid=#{eid}
</select><mappernamespace="com.lotus.mybatis.mapper.DeptMapper"><!--Dept getEmpAndDeptByStepTwo(@Param("did") Integer did);--><selectid="getEmpAndDeptByStepTwo"resultType="Dept">
select * from t_dept where did=#{did}
</select></mapper>
③分步查询
<resultMapid="empAndDeptByStepResultMap"type="Emp"><idproperty="eid"column="eid"/><idproperty="empName"column="emp_name"/><idproperty="age"column="age"/><idproperty="sex"column="sex"/><idproperty="email"column="email"/><!--
select:设置分步查询的SQL唯一标识(namespace,SQLID或mapper接口的全类名.方法名)
column:设置分步查询的条件
fetchType(eager|lazy):当开启全局延迟加载后,通过此属性手动控制延迟加载的效果,eager表示立即加载
--><associationproperty="dept"select="com.lotus.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"column="did"fetchType="eager"></association></resultMap><!-- Emp getEmpAndDeptByStepOne(); --><selectid="getEmpAndDeptByStepOne"resultMap="empAndDeptByStepResultMap">
select * from t_emp where eid=#{eid}
</select>
<resultMapid="deptAndEmpResultMap"type="Dept"><idproperty="did"column="did"/><idproperty="deptName"column="dept_name"/><collectionproperty="emps"ofType="Emp"><idproperty="eid"column="eid"/><idproperty="empName"column="emp_name"/><idproperty="age"column="age"/><idproperty="sex"column="sex"/><idproperty="email"column="email"/></collection></resultMap><!--Dept getDeptAndEmp(@Param("did") Integer did);--><selectid="getDeptAndEmp"resultMap="deptAndEmpResultMap">
select * from t_dept left join t_emp on t_dept.did = t_emp.did where t_dept.did=#{did}
</select>