我是springboot整合mybatis测试的
涉及到的表:
sql: 1.select * from tbl_employee where id=#{id}(用户表的主键id)
2.select *from tbl_dept where id=#{id}(id是部门表的主键id,#{id}随便写)
要用到两条sql,现根据用户id查出用户,再然后根据用户的外键也就是部门id用户的部门
员工实体类中要有部门的实体属性:
private Department dept;
EmployeeDao这样写:
Employee selectEmployeeDept(Integer id);
EmployeeMapper.xml这样写:
<select id="selectEmployeeDept" resultMap="MyDifEmp">
<!--1.先查的用户表,根据员工id -->
select * from tbl_employee where id=#{id}
</select>
<resultMap type="com.cn.entity.Employee" id="MyDifEmp">
<!-- property:在员工实体中部门实体的属性
column: 员工表的外键字段,待会儿会传给第二个sql当条件
select: 第二次sql的查询的id,指向了id=StepSelectDepts
的查询
-->
<association property="dept" column=