级联是resultMap中的配置。
级联分为3种
bean:
public class Employee {
private Long id;
private String realName;
private SexEnum sex = null;
private Date birthday;
private String mobile;
private String email;
private String position;
private String note;
//工牌按一对一级联
private WorkCard workCard;
//雇员任务,一对多级联
private List<EmployeeTask> employeeTaskList = null;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getRealName() {
return realName;
}
public void setRealName(String realName) {
this.realName = realName;
}
public SexEnum getSex() {
return sex;
}
public void setSex(SexEnum sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public WorkCard getWorkCard() {
return workCard;
}
public void setWorkCard(WorkCard workCard) {
this.workCard = workCard;
}
public List<EmployeeTask> getEmployeeTaskList() {
return employeeTaskList;
}
public void setEmployeeTaskList(List<EmployeeTask> employeeTaskList) {
this.employeeTaskList = employeeTaskList;
}
}
public class FemaleEmployee extends Employee {
private FemaleHealthForm femaleHealthForm = null;
public FemaleHealthForm getFemaleHealthForm() {
return femaleHealthForm;
}
public void setFemaleHealthForm(FemaleHealthForm femaleHealthForm) {
this.femaleHealthForm = femaleHealthForm;
}
}
public class MaleEmployee extends Employee {
private MaleHealthForm maleHealthForm = null;
public MaleHealthForm getMaleHealthForm() {
return maleHealthForm;
}
public void setMaleHealthForm(MaleHealthForm maleHealthForm) {
this.maleHealthForm = maleHealthForm;
}
}
public class WorkCard {
private Long id;
private Long empId;
private String realName;
private String department;
private String mobile;
private String position;
private String note;
}
public class EmployeeTask {
private Long id;
private Long empId;
private Task task = null;
private String taskName;
private String note;
}
mapper:
public interface EmployeeMapper {
public Employee getEmployee(Long id);
public Employee getEmployee2(Long id);
}
XML:
<collection property="employeeTaskList" column="id"//一对多
fetchType="eager"
select="com.ssm.chapter5.mapper.EmployeeTaskMapper.getEmployeeTaskByEmpId" />
<discriminator javaType="long" column="sex">
<case value="1" resultMap="maleHealthFormMapper" />//鉴别器级联
<case value="2" resultMap="femaleHealthFormMapper" />
</discriminator>
</resultMap>
<resultMap type="com.ssm.chapter5.pojo.FemaleEmployee" id="femaleHealthFormMapper"
extends="employee">
<association property="femaleHealthForm" column="id"
select="com.ssm.chapter5.mapper.FemaleHealthFormMapper.getFemaleHealthForm" />
</resultMap>
<resultMap type="com.ssm.chapter5.pojo.MaleEmployee" id="maleHealthFormMapper"
extends="employee">
<association property="maleHealthForm" column="id"
select="com.ssm.chapter5.mapper.MaleHealthFormMapper.getMaleHealthForm" />
</resultMap>
<select id="getEmployee" parameterType="long" resultMap="employee">
select
id, real\_name as realName, sex, birthday, mobile, email, position,
note from t\_employee where id = #{id}
</select>
<resultMap id="employee2" type="com.ssm.chapter5.pojo.Employee">
<id column="id" property="id" />
<result column="real\_name" property="realName" />
<result column="sex" property="sex"
typeHandler="com.ssm.chapter5.typeHandler.SexTypeHandler" />
<result column="birthday" property="birthday" />
<result column="mobile" property="mobile" />
<result column="email" property="email" />
<result column="position" property="position" />
<association property="workCard" javaType="com.ssm.chapter5.pojo.WorkCard"/一对一级联
column="id">
<id column="wc\_id" property="id" />
<result column="id" property="empId" />
<result column="wc\_real\_name" property="realName" />
<result column="wc\_department" property="department" />
<result column="wc\_mobile" property="mobile" />
<result column="wc\_position" property="position" />
<result column="wc\_note" property="note" />
</association>
<collection property="employeeTaskList" ofType="com.ssm.chapter5.pojo.EmployeeTask"//一对多级联
column="id">
<id column="et\_id" property="id" />
<result column="id" property="empId" />
<result column="task\_name" property="taskName" />
<result column="note" property="note" />
<association property="task" javaType="com.ssm.chapter5.pojo.Task"
column="et\_task\_id">
<id column="t\_id" property="id" />
<result column="t\_title" property="title" />
<result column="t\_context" property="context" />
<result column="t\_note" property="note" />
</association>
</collection>
<discriminator javaType="int" column="sex"> //sex鉴别器级联male/female
<case value="1" resultMap="maleHealthFormMapper2" />
<case value="2" resultMap="femaleHealthFormMapper2" />
</discriminator>
</resultMap>
<resultMap type="com.ssm.chapter5.pojo.MaleEmployee" id="maleHealthFormMapper2"
extends="employee2">
<association property="maleHealthForm" column="id"
javaType="com.ssm.chapter5.pojo.MaleHealthForm">
<id column="h\_id" property="id" />
<result column="h\_heart" property="heart" />
<result column="h\_liver" property="liver" />
<result column="h\_spleen" property="spleen" />
<result column="h\_lung" property="lung" />
<result column="h\_kidney" property="kidney" />
<result column="h\_prostate" property="prostate" />
<result column="h\_note" property="note" />
</association>
</resultMap>
<resultMap type="com.ssm.chapter5.pojo.FemaleEmployee" id="femaleHealthFormMapper2"
extends="employee2">
<association property="femaleHealthForm" column="id"
javaType="com.ssm.chapter5.pojo.FemaleHealthForm">
<id column="h\_id" property="id" />
<result column="h\_heart" property="heart" />
<result column="h\_liver" property="liver" />
<result column="h\_spleen" property="spleen" />
<result column="h\_lung" property="lung" />
<result column="h\_kidney" property="kidney" />
<result column="h\_uterus" property="uterus" />
<result column="h\_note" property="note" />
</association>
</resultMap>
<select id="getEmployee2" parameterType="long" resultMap="employee2">
select
emp.id, emp.real\_name, emp.sex, emp.birthday,
emp.mobile, emp.email,
emp.position, emp.note,
et.id as et\_id, et.task\_id as et\_task\_id,
et.task\_name as et\_task\_name,
et.note as et\_note,
if (emp.sex = 1,
mhf.id, fhf.id) as h\_id,
if (emp.sex = 1, mhf.heart, fhf.heart) as
h\_heart,
if (emp.sex = 1, mhf.liver, fhf.liver) as h\_liver,
if (emp.sex
= 1, mhf.spleen, fhf.spleen) as h\_spleen,
if (emp.sex = 1, mhf.lung,
fhf.lung) as h\_lung,
if (emp.sex = 1, mhf.kidney, fhf.kidney) as
h\_kidney,
if (emp.sex = 1, mhf.note, fhf.note) as h\_note,
mhf.prostate
as h\_prostate, fhf.uterus as h\_uterus,
wc.id wc\_id, wc.real\_name
wc\_real\_name, wc.department wc\_department,
wc.mobile wc\_mobile,
wc.position wc\_position, wc.note as wc\_note,
t.id as t\_id, t.title as
t\_title, t.context as t\_context, t.note as t\_note
from t\_employee emp
left join t\_employee\_task et on emp.id = et.emp\_id
left join
t\_female\_health\_form fhf on emp.id = fhf.emp\_id
left join
t\_male\_health\_form mhf on emp.id = mhf.emp\_id
left join t\_work\_card wc
on emp.id = wc.emp\_id
left join t\_task t on et.task\_id = t.id
where
emp.id = #{id}
</select>
test:
public static void testGetEmployee() {
SqlSession sqlSession = null;
try {
sqlSession = SqlSessionFactoryUtils.openSqlSession();
EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
Employee employee = employeeMapper.getEmployee(1L);
System.out.println(employee.getWorkCard().getPosition());
} catch(Exception ex) {
ex.printStackTrace();
} finally {
if (sqlSession != null) {
sqlSession.close();
}
}
}
public static void testGetEmployee2() {
SqlSession sqlSession = null;
try {
sqlSession = SqlSessionFactoryUtils.openSqlSession();
EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
Employee employee = employeeMapper.getEmployee2(1L);
System.out.println(employee.getWorkCard().getPosition());
} catch(Exception ex) {
ex.printStackTrace();
} finally {
if (sqlSession != null) {
sqlSession.close();
}
}
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章