mybatis映射文件_select_resultMap
实体类:
Employee.java类:
package com.hand.mybatis.bean;
public class Employee {
private Integer eId;
private String eName;
private Integer gender;
private String email;
private Department dept;
public Employee() {
super();
}
public Employee(Integer eId,String eName, Integer gender, String email) {
super();
this.eId=eId;
this.eName = eName;
this.gender = gender;
this.email = email;
}
public Integer geteId() {
return eId;
}
public void seteId(Integer eId) {
this.eId = eId;
}
public String getEName() {
return eName;
}
public void setEname(String ename) {
this.eName = ename;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Department getDept() {
return dept;
}
public void setDept(Department dept) {
this.dept = dept;
}
@Override
public String toString() {
return "Employee [eId=" + eId + ", ename=" + eName + ", gender=" + gender + ", email=" + email + "]";
}
Department实体类:
package com.hand.mybatis.bean;
import java.util.List;
public class Department {
private Integer id;
private String departName;
private List<Employee> empList;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDepartName() {
return departName;
}
public void setDepartName(String departName) {
this.departName = departName;
}
public List<Employee> getEmpList() {
return empList;
}
public void setEmpList(List<Employee> empList) {
this.empList = empList;
}
@Override
public String toString() {
return "Department [id=" + id + ", departName=" + departName + "]";
}
}
EmployeeMapperPlus.xml 映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hand.mybatis.dao.EmployeeMapperPlus">
<!-- 自定义某个javaBean的封装规则
type:自定义规则的java类型
id:唯一id方便引用 -->
<resultMap type="com.hand.mybatis.bean.Employee" id="myemp">
<!-- 指定主键列的封装规则
id:定义主键底层会有优化
column:指定哪一列
property:指定对应的javabean属性
-->
<id column="eid" property="eId"/>
<result column="ename" property="eName"/>
<result column="gender" property="gender"/>
<result column="email" property="email"/>
</resultMap>
1.简单的 javaBean映射
<!-- Employee selectEmployee(Integer eid); -->mapper接口一
<select id="selectEmployee" resultMap="myemp">
SELECT * FROM emp WHERE eid=#{eid}
</select>
<!-- 查询Employee的同时查询员工对应的部门
Employee==Department
一个员工有与之对应的部门信息
-->
<!-- (1).关联查询 级联属性封装结果集-->
<resultMap type="com.hand.mybatis.bean.Employee" id="myemp1">
<id column="eid" property="eId"/>
<result column="ename" property="eName"/>
<result column="gender" property="gender"/>
<result column="email" property="email"/>
<result column="did" property="dept.id"/>
<result column="deptname" property="dept.departName"/>
</resultMap>
<!-- (2).使用association定义单个对象的封装规则
-->
<resultMap type="com.hand.mybatis.bean.Employee" id="myemp2">
<id column="eid" property="eId"/>
<result column="ename" property="eName"/>
<result column="gender" property="gender"/>
<result column="email" property="email"/>
<!-- assosiation可以指定javaBean对象
property="dept":指定哪一个是关联的对象
javaType:指定这个属性对象的类型[不能省略]-->
<association property="dept" javaType="com.hand.mybatis.bean.Department">
<id column="did" property="id"/>
<result column="deptname" property="departName"/>
</association>
</resultMap>
2.
<!-- Employee getEmpAndDept(Integer eid); -->mapper接口二
<select id="getEmpAndDept" resultMap="myemp2">//根据员工id查询出员工和部门的所有字段
select emp.eid,emp.ename,emp.gender,emp.email,dept.did,dept.deptname
from emp,dept
WHERE emp.did=dept.did AND
emp.eid=#{eid}
</select>
<!-- 3.使用association进行分步查询
1.先按照员工id查询员工信息
2.根据查询员工信息中的did值去部门查询部门信息
3.部门设置到员工中 -->
<resultMap type="com.hand.mybatis.bean.Employee" id="myempStep">//分步查询
<id column="eid" property="eId"/>
<result column="ename" property="eName"/>
<result column="gender" property="gender"/>
<result column="email" property="email"/>
<!-- association定义关联对象的封装规则
select:表明当前属性调用select指定的方法查询结果
column:指定将那一列的值传给这个方法-->
<!-- !!!有问题查不出来结果 -->
<association property="dept"
select="com.hand.mybatis.dao.DepartmentMapper.getDeptById" //使用association中的select标签调动DepartmentMapper.getDeptById接口查询部门信息
column="did">
</association>
</resultMap>
<!--Employee getEmpByIdStep(Integer eid); -->mapper接口三
<select id="getEmpByIdStep" resultMap="myempStep">
select * from emp where eid=#{eid}
</select>
<!--3. discriminator鉴别器 -->
<resultMap type="com.hand.mybatis.bean.Employee" id="mydepDis">
<id column="eid" property="eId"/>
<result column="ename" property="eName"/>
<result column="gender" property="gender"/>
<result column="email" property="email"/>
<!--
column:指定判断的列名
javaType:列值对应的java类型 -->
<discriminator javaType="string" column="gender">
<!-- 女生 resultType:指定封装的结果类型,不能缺少,resultMap -->
<case value="0" resultType="com.hand.mybatis.bean.Employee">//gender为0(女生),则查询部门信息
<association property="dept"
select="com.hand.mybatis.dao.DepartmentMapper.getDeptById" //根据部门id查询部门信息接口
column="did">
</association>
</case>
<!-- 男生 :如果是男生,把ename这一列的值赋值给email-->
<case value="1" resultType="com.hand.mybatis.bean.Employee">//gender为1(男生),将ename这一列的值赋值给emai
<id column="eid" property="eId"/>
<result column="ename" property="eName"/>
<result column="gender" property="gender"/>
<result column="eName" property="email"/>
</case>
</discriminator>
</resultMap>
<!--Employee getEmpByIdDis(Integer eid); -->接口四
<select id="getEmpByIdDis" resultMap="mydepDis">
select * from emp where eid=#{eid}
</select>
接口四结果:
(1) Employee emp= mapper.getEmpByIdDis(101);
若员工101的gender=0(女生),
则结果为:有部门信息
Department [id=1, departName=销售部]
(2)Employee emp= mapper.getEmpByIdDis(103);
则结果为:无部门信息,并且email的为ename的值杨张
null
</mapper>
代码:https://github.com/shuaishuaihand/mybatis.git
mybatis映射文件_select_resultMap的更多相关文章
- Mybatis映射文件完整模板参照
Mybatis映射文件完整模板参照 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE map ...
- Mybatis映射文件中#取值时指定参数相关规则
Mybatis映射文件中#取值时指定参数相关规则 在#{}中,除了需要的数值外,还可以规定参数的一些其他规则. 例如:javaType,jdbcType,mode(存储过程),numericScale ...
- SSM实战——秒杀系统之DAO层实体定义、接口设计、mybatis映射文件编写、整合Spring与Mybatis
一:DAO实体编码 1:首先,在src目录下,新建org.myseckill.entity包,用于存放实体类: 2:实体类设计 根据前面创建的数据库表以及映射关系,创建实体类. 表一:秒杀商品表 对应 ...
- MyBatis 映射文件详解
1. MyBatis 映射文件之<select>标签 <select>用来定义查询操作; "id": 唯一标识符,需要和接口中的方法名一致; paramet ...
- MyBatis映射文件中用#和$传递参数的特点
在MyBatis映射文件中用#和$传递参数的特点, #是以占位符的形式来传递对应变量的参数值的,框架会对传入的参数做预编译的动作, 用$时会将传入的变量的参数值原样的传递过去,并且用$传递传递参数的时 ...
- MyBatis映射文件 相关操作
一.MyBatis映射文件 1.简介 MyBatis 的真正强大在于它的映射语句,也是它的魔力所在.由于它的异常强大,映射器的 XML 文件就显得相对简单.如果拿它跟具有相同功能的 JDBC 代码进行 ...
- Mybatis映射文件标签(关于sql)
Mybatis映射文件 1.接口的全限定名和映射文件的namespace一致 <mapper namespace="com.offcn.dao.UserDao"> 2. ...
- MyBatis 映射文件
Mybatis映射文件简介 1) MyBatis 的真正强大在于它的映射语句.由于它的异常强大,映射器的 XML 文件就显得相对简单.如果拿它跟具有相同功能的 JDBC 代码进行对比,你会立即发现省掉 ...
- Mybatis映射文件
Mapper XML 文件 MyBatis 的真正强大在于它的映射语句,也是它的魔力所在.由于它的异常强大,映射器的 XML 文件就显得相对简单.如果拿它跟具有相同功能的 JDBC 代码进行对比,你会 ...
随机推荐
- [报错]Unable to simultaneously satisfy constraints
项目中自定义Cell,控件使用autoLayout来设置约束,发现运行页面表现正常,但是控制台报如下错误: Unable to simultaneously satisfy constraints. ...
- Spark源码分析 – DAGScheduler
DAGScheduler的架构其实非常简单, 1. eventQueue, 所有需要DAGScheduler处理的事情都需要往eventQueue中发送event 2. eventLoop Threa ...
- 【opencv】cv::Mat转std::vector<cv::Point2d> (注意两容器中数据类型的一致性)
获取cv::Mat大小: mymat.size() 获取cv::Mat指定位置的值:需指定数据类型,且注意数据类型应与存入时的数据类型一致,否则会导致不抛出异常的数据错误 mymat.at<,i ...
- git学习------> Gitlab如何进行备份恢复与迁移?
前段时间,在某台CenterOS服务器上搭建了Gitlab环境,并且大家陆陆续续的都把代码从svn迁移到了gitlab,但是之前的CenterOS服务器并不是搭建在公司的机房环境,而是搭建在办公室的某 ...
- 教你编译PHP7 (nginx+mysql+php7)
# 安装编译工具: yum install gcc automake autoconf libtool gcc-c++ # 安装基础库 yum install gd zlib zlib-devel o ...
- CentOS 6.4中yum安装配置LAMP服务器(Apache+MySQL+PHP5)
准备篇: 1.配置防火墙,开启80端口.3306端口 vim /etc/sysconfig/iptables -A INPUT -m state --state NEW -m tcp -p tcp ...
- git pull和git merge区别&&Git冲突:commit your changes or stash them before you can merge. 解决办法
http://blog.csdn.net/sidely/article/details/40143441 原文: http://www.tech126.com/git-fetch-pull/ Git中 ...
- 【1】Kali Linux的安装及配置
爱生活就得够GEEK. ---------------------------------------------------------------完美的分割线------------------- ...
- C++切割字符串
std::string text = "2001_1;2005_5;"; std::stringstream ss(text); std::string sub_str; std: ...
- 【Linux学习】1.Linux基础知识
记录学习Linux 系统的相关知识点,欢迎大家拍砖交流,一起成长:QQ:2712192471 作者背景:前端开发工程师 | Python | web安全爱好者 一,Windows系统下 Linux 的 ...