当想要查询一个部门下的所有员工时,多个员工对应一个部门
mapper.xml
:
<select id="getEmpAndDept" resultMap="empAndDeptResultMapTwo">
select * from t_emp
left join t_dept on t_emp.did = t_dept.did
where t_emp.eid = #{eid}
select>
pojo
:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Emp {
private Integer eid;
private String empName;
private Integer age;
private String sex;
private String email;
private Dept dept;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Dept {
private Integer did;
private String deptName;
}
在
中直接使用 类.属性
来代表嵌套的pojo
<resultMap id="empAndDeptResultMapOne" type="Emp">
<id property="eid" column="eid"/>
<result property="empName" column="emp_name"/>
<result property="age" column="age"/>
<result property="sex" column="sex"/>
<result property="email" column="email"/>
<result property="dept.did" column="did"/>
<result property="dept.deptName" column="dept_name"/>
在
中直接使用 类.属性
来代表嵌套的pojo
<resultMap id="empAndDeptResultMapOne" type="Emp">
<id property="eid" column="eid"/>
<result property="empName" column="emp_name"/>
<result property="age" column="age"/>
<result property="sex" column="sex"/>
<result property="email" column="email"/>
<result property="dept.did" column="did"/>
<result property="dept.deptName" column="dept_name"/>
<resultMap id="empAndDeptResultMapTwo" type="Emp">
<id property="eid" column="eid"/>
<result property="empName" column="emp_name"/>
<result property="age" column="age"/>
<result property="sex" column="sex"/>
<result property="email" column="email"/>
<association property="dept" javaType="dept">
<id property="did" column="did"/>
<result property="deptName" column="dept_name"/>
association>
resultMap>
Emp getEmpAndDeptByStepOne(Integer eid);
<select id="getEmpAndDeptByStepOne">
select *
from t_emp
left join t_dept on t_emp.did = t_dept.did
where t_emp.eid = #{eid}
select>
Dept getEmpAndDeptByStepTwo(Integer did);
<select id="getEmpAndDeptByStepTwo" resultType="com.zxb.mybatis.pojo.Dept">
select * from t_dept where did = #{did}
select>
<resultMap id="empAndDeptByStepResultMap" type="Emp">
<id property="eid" column="eid"/>
<result property="empName" column="emp_name"/>
<result property="age" column="age"/>
<result property="sex" column="sex"/>
<result property="email" column="email"/>
<association property="dept" select="com.zxb.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo" column="did"/>
resultMap>
<select id="getEmpAndDeptByStepOne" resultMap="empAndDeptByStepResultMap">
select *
from t_emp
left join t_dept on t_emp.did = t_dept.did
where t_emp.eid = #{eid}
select>