Mybatis--级联(一)
级联是resultMap中的配置。
级联分为3种
- 鉴别器(discrimination):根据某些条件采用具体实现具体实现类级联,如体检表根据性别去区分
- 一对一:学生和学生证
- 一对多:班主任和学生。
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:
<mapper namespace="com.ssm.chapter5.mapper.EmployeeMapper">
<resultMap type="com.ssm.chapter5.pojo.Employee" id="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" />
<result column="note" property="note" />
<association property="workCard" column="id" //一对一
select="com.ssm.chapter5.mapper.WorkCardMapper.getWorkCardByEmpId" /> //应用另外一个 xml map中 id为"getWorkCardByEmpId"的select
<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>
</mapper>
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();
}
}
}
Mybatis--级联(一)的更多相关文章
- (三)mybatis级联的实现
mybatis级联的实现 开篇 级联有三种对应关系: 1.一对一(association):如学号与学生 2.一对多(collection):如角色与用户 3.多对多(discri ...
- Mybatis 级联查询 (一对多 )
后台系统中 涉及到添加试卷 问题 答案的一个模块的.我需要通过试卷 查询出所有的试题,以及试题的答案.这个主要要使用到Mybatis的级联查询. 通过试卷 查询出与该试卷相关的试题(一对多),查询出试 ...
- Mybatis级联:关联、集合和鉴别器的使用
Mybatis中级联有关联(association).集合(collection).鉴别器(discriminator)三种.其中,association对应一对一关系.collection对应一对多 ...
- mybatis级联
mybatis中有时候表不能都分成单表进行查询,表之间会有联系,这时候需要将表进行级联 下面讲一下如何将mybatis中 的表进行级联.映射表关系如下 1:创建数据表 DROP TABLE IF EX ...
- Mybatis级联,使用JOIN和Associa,以及一些ID覆盖和自动变换。
先说下坑,比如数据库的字段是 DW_ID ,用generator讲mybatis自动转换的时候,会省略下表_变成dwId,所以我们之后自己手动设计的时候也尽量换成dwId: generate的myb ...
- mybatis 级联
级联是一个数据库实体的概念.一对多的级联,一对多的级联,在MyBatis中还有一种被称为鉴别器的级联,它是一种可以选择具体实现类的级联. 级联不是必须的,级联的好处是获取关联数据十分便捷,但是级联过多 ...
- mybatis级联查询,多对一查询问题
在使用Mybatis进行多表级联查询时遇到了一个问题:查询结果只有一项,但正确结果是两项.经测试,SQL语句本身没有问题. 在SQL映射文件(XML)中: <!-- 级联查询数据 --> ...
- Mybaits(9)MyBatis级联-2
一.鉴别器和一对多级联 1.完善体检表,分为男雇员体检和女雇员体检表 (1)持久层dao编写 package com.xhbjava.dao; import com.xhbjava.domain.Ma ...
- mybatis ---- 级联查询 一对多 (集合映射)
关联有嵌套查询和嵌套结果两种方式,本文是按照嵌套结果这种方式来说明的 上一章介绍了多对一的关系,用到了<association></association>,这是一个复杂类型的 ...
- mybatis级联查询
1.定义四个实体.User Role Privilege Resource,他们之间的对于关系为 2.需求:我通过用户名username查找出该用户对应的角色以及角色对应的权限和资源 3 ...
随机推荐
- [刘阳Java]_美团点评2018届校招面试总结_Java后台开发【转载】
美团喜欢一口气把三轮技术面和HR面一起面完,虽然身心比较累(每一面差不多一个小时),不过也算是一个好事,不像某些公司一天就一面然后让回去等消息,等面试通知也等得让人很焦虑,而且还容易出现面试时间冲突. ...
- 学习总结 NCRE二级和三级
NCRE二级C语言 证书 考试感想 2016年考的认证,5年过去了,"光阴荏苒真容易".趁着心有余力有余的时候,把一些个人的体会分享给大家,希望后来人能平稳前行. Windows ...
- React 组件间通信 总结
组件间通信 5.1.1. 方式一: 通过props传递 1) 共同的数据放在父组件上, 特有的数据放在自己组件内部(state) 2) 通过props可以传递一般数据和 ...
- 前端基础EL表达式(八)
一.什么是EL表达式? 1.什么是EL表达式? EL(Expression Language) 是为了使JSP写起来更加简单.表达式语言的灵感来自于 ECMAScript 和 XPath 表达式语言, ...
- js精确到指定位数的小数
将数字四舍五入到指定的小数位数.使用 Math.round() 和模板字面量将数字四舍五入为指定的小数位数. 省略第二个参数 decimals ,数字将被四舍五入到一个整数. const round ...
- APICloud的真机wifi连接问题
APICloud的真机wifi连接问题 在APICloud的真机wifi连接时需要注意事项与解决问题. 1.首先将项目拉取到本地,用APICloud Studio 2打开(也可以用webStorm配置 ...
- 大数据学习(15)—— B+树和LSM
这一节介绍数据库存储引擎常用的两种数据结构.作为关系型数据库的代表,MySql的InnoDB使用B+树来存储索引.作为NoSQL的代表,HBase使用的LSM树,我们来看看两者有什么区别. B+树 B ...
- Netty入门(一):ByteBuf
网络数据的基本单位总是字节.Java NIO 提供了 ByteBuffer 作为它的字节容器,但是这个类使用起来过于复杂,而且也有些繁琐.Netty 的 ByteBuffer 替代品是 ByteBuf ...
- 还在用Postman?来,花2分钟体验下ApiPost的魅力
2分钟玩转APIPOST 本文通过简单介绍如何利用ApiPost调试接口和快速的生成接口文档,让您初步体验ApiPost的魅力! 1. API写完想要测试?试试模拟发送一次请求 新建接口,我想模拟发送 ...
- vue日记之可展开的消息气泡
项目小需求之聊天气泡可展开内容 因为某些信息内容太长或者某种原因必须分行输出,这就导致了有时候一个气泡占据了一整个聊天区域 所以我打算实现一个在该气泡加载的时候判断其气泡长度,并在长度过长的情况下进行 ...