级联是在resultMap标签中配置。级联不是必须的,级联的好处是获取关联数据十分便捷,但是级联过多会增加系统的复杂度,同事降低系统的性能,次增彼减,所以记录超过3层时,就不要考虑使用级联了,因为这样会造成多个对象的关联,导致系统的耦合、负责和难以维护。

MyBatis中的级联分3种:

1.鉴别器(discriminator)它是一个根据某些条件决定采用具体实现类级联的方案,比如体检根据性别来区分检查项目。

2.一对一(association)

3.一对多(collection)

表模型:

MyBatis 级联查询_mybatis关联查

关系:

以雇员表为中心。

雇员表和工牌表一对一关系。

雇员表和员工任务表是一对多关系。

员工任务表和任务表是一对一关系。

每个雇员都会有一个体检表,随着雇员性别不同,会有不同的关联表。

1、实体类:

忽略getter、setter

(1)体检表-父类

public class HealthForm {
private Long id;
private Long empId;
private String heart;
private String liver;
private String spleen;
private String lung;
private String kidney;
private String note;
}

(2)女性体检表

public class FemaleHealthForm extends HealthForm{
private String uterus;
}

(3) 男性体检表

public class MaleHealthForm extends HealthForm{
private String prostate;
}

(4) 工牌表

public class Employee {
private Long id;
private String realName;
private SexEnum sex=null;
private Date brithday;
private String mobile;
private String email;
private String position;
private String note;
//一对一
private WorkCard workCard;
//一对多
private List<EmployeeTask> employeeTaskList=null;
}

(5) 任务表

public class Task {
private Long id;
private String title;
private String context;
private String note;
}

(6) 雇员任务表

public class EmployeeTask {
private Long id;
private Long empId;
private Task task=null;
private String taskName;
private String note;
}

(7) 雇员父类

public class Employee {
private Long id;
private String realName;
private SexEnum sex=null;
private Date brithday;
private String mobile;
private String email;
private String position;
private String note;
//一对一
private WorkCard workCard;
//一对多
private List<EmployeeTask> employeeTaskList=null;
}

(8) 男雇员

public class MaleEmployee extends Employee{
private MaleHealthForm maleHealthForm=null;
}

(9) 女雇员

public class FemaleEmployee extends Employee{
private FemaleHealthForm femaleHealthForm=null;
}

2、配置映射文件

<mapper namespace="com.bob.analyst.dao.TaskMapper">
<select id="getTask" parameterMap="long" resultType="com.bob.analyst.model.Task">
select id,title,context,note from tbl_task where id=#{id}
</select>
</mapper>
<mapper namespace="com.bob.analyst.model.WorkCard">
<select id="getWorkCardByEmpId" parameterType="long" resultType="com.bob.analyst.model.WorkCard">
select id,emp_id as empId,real_name as realName,
department,mobile,position,note from tbl_work_card where id = #{id}
</select>
</mapper>
<mapper namespace="com.bob.analyst.dao.EmployeeTaskMapper">
<resultMap type="com.bob.analyst.model.EmployeeTask" id="EmployeeTaskMap">
<id column="id" property="id"/>
<result column="emp_id" property="empId"/>
<result column="task_name" property="taskName"/>
<result column="note" property="note"/>
<!-- 一对一关联 -->
<association property="task" column="task_id" select="com.bob.analyst.dao.getTask"/>
</resultMap>

<select id="getEmployeeTaskByEmpId" resultMap="EmployeeTaskMap">
select id,emp_id,task_name,note from tbl_employee_task
where emp_id=#{empId}
</select>
</mapper>
<mapper namespace="com.bob.analyst.dao.EmployeeTaskMapper">
<select id="getMaleHealthForm" parameterType="long" resultMap=" com.bob.analyst.model.MaleHealthForm">
select id,heart,liver,spleen,lung,kidney,prostate,note from tbl_male_health_from
where emp_id=#{id}
</select>
</mapper>
<mapper namespace="com.bob.analyst.dao.FemaleHealthFormMapper">
<select id="getFemaleHealthForm" parameterType="long" resultMap="com.bob.analyst.model.FemaleHealthForm">
select id,heart,liver,spleen,lung,kidney,uterus,note from tbl_female_health_from
where emp_id=#{id}
</select>
</mapper>
<mapper namespace="com.bob.analyst.dao.EmployeeMapper">
<resultMap type="com.bob.analyst.model.Employee" id="employee">
<id column="id" property="id"/>
<result column="real_name" property="realName"/>
<result column="sex" property="sex" typeHandler="com.bob.analyst.util.SexEnum"/>
<result column="brithday" property="brithday"/>
<result column="mobile" property="mobile"/>
<result column="email" property="email"/>
<result column="position" property="position"/>
<result column="note" property="note"/>

<!-- 一对一关联 -->
<association property="workCard" column="id"
select="com.bob.analyst.dao.WorkCardMapper.getWorkCardByEmpId"/>

<!-- 一对多 -->
<collection property="employeeTaskList" column="id"
select="com.bob.analyst.dao.EmployeeTaskMapper.getEmployeeTaskByEmpId"></collection>

<!-- 鉴别器 -->
<discriminator javaType="long" column="sex">
<case value="1" resultMap="maleHealthFormMapper"></case>
<case value="2" resultMap="femaleHealthFormMapper"></case>
</discriminator>
</resultMap>

<resultMap type="com.bob.analyst.model.FemaleEmployee" id="femaleHealthFormMapper" extends="employee">
<association property="femaleHealthForm" column="id"
select="com.bob.analyst.dao.FemaleHealthFormMapper.getFemaleHealthForm"></association>
</resultMap>

<resultMap type="com.bob.analyst.model.MaleEmployee" id="maleHealthFormMapper" extends="employee">
<association property="maleHealthForm" column="id"
select="com.bob.analyst.dao.MaleHealthFormMapper.getMaleHealthForm"></association>
</resultMap>

<select id="getEmployee" parameterType="long" resultMap="employee">
select id,real_name as realName,sex,birthday,mobile,email,position,note from tbl_employee
where id=#{id}
</select>
</mapper>