因为一个员工(staff)可以属于多个部门(department),一个部门(department)会很多员工(staff),所以staff_department是一个 staff_id 与 department_id的一张多对多的关系表。
现在我需要在mybatis 查询一条staff的时候获得当前这个staff关联的所有department,于是用到collection。
staff 实体类
package com.asclepius.slhdt.serv.dao.domain.staff; /** * @author chensg * */ import java.io.Serializable; import java.util.Date; import java.util.List; import org.springframework.format.annotation.DateTimeFormat; import com.asclepius.slhdt.serv.dao.domain.department.Department; import com.fasterxml.jackson.annotation.JsonFormat; public class Staff implements Serializable { private static final long serialVersionUID = 1L; // 自动增长id private Integer staffIncId; // uuid private String staffId; // 姓名 private String staffName; // 性别 private Integer staffGender; // 所属部门 private List<Department> departmentList; public Integer getStaffIncId() { return staffIncId; } public void setStaffIncId(Integer staffIncId) { this.staffIncId = staffIncId; } public String getStaffId() { return staffId; } public void setStaffId(String staffId) { this.staffId = staffId; } public String getStaffName() { return staffName; } public void setStaffName(String staffName) { this.staffName = staffName; } public Integer getStaffGender() { return staffGender; } public void setStaffGender(Integer staffGender) { this.staffGender = staffGender; } public List<Department> getDepartmentList() { return departmentList; } public void setDepartmentList(List<Department> departmentList) { this.departmentList = departmentList; } }
staff与department是一个一对多关系,所以这里定义List<Department>
在xml中
<mapper namespace="com.asclepius.slhdt.serv.dao.mapper.staff.StaffMapper"> <resultMap type="staff" id="staffResult"> <id property="staffIncId" column="staff_inc_id" /> <result property="staffId" column="staff_id" /> <result property="staffName" column="staff_name" /> <result property="staffGender" column="staff_gender" /> <collection property="departmentList" column="staff_id" select="selectDepartment" ofType="department" javaType="ArrayList" /> </resultMap> <select id="selectDepartment" parameterType="String" resultType="department" resultMap="departmentResult" > select SD.department_id, D.department_parent_id, DP.department_name_alias from shulan_staff_department SD left join shulan_department_prop DP on DP.department_id = SD.department_id left join shulan_department D on D.department_id = SD.department_id where SD.staff_id=#{staffId} </select> <resultMap type="department" id="departmentResult"> <id property="departmentIncId" column="department_inc_id" /> <result property="departmentId" column="department_id" /> <result property="departmentParentId" column="department_parent_id" /> <result property="departmentNameAlias" column="department_name_alias" /> </resultMap> <!-- 查询员工数据 --> <select id="getStaffById" parameterType="String" resultMap="staffResult"> select S.staff_id,S.staff_name,S.staff_gender, from staff S where S.staff_id = #{param1} </select>
这里省去了很多staff中无关的字段。看collection标签 property="departmentList" 对应的就是实体类中的List<Department> departmentList属性。column与select要连在一起看,select=selectDepartment指的就是 下面id="selectDepartment的select映射SQL,column=staff_id指的就是关联主键的id。调用getstaffById就可以获得当前这个staff以及其关联的所有department