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 代码进行对比,你会 ...
随机推荐
- 8个Javascript小技巧
1. 使用 + 字符可以转换成数字 比如要把一个字符串数字转换成数字,你可能会这样做: var one = '1'; var two = '2'; var numberOne = Number(one ...
- 品友互动大数据平台的技术演化 https://www.sohu.com/a/191202836_99982360
品友互动大数据平台的技术演化
- linux 命令行 执行 php
w为监控响应功能做准备. ubuntu@VM-52-248-ubuntu:~$ php -f /var/www/html/wlinux.phpwwubuntu@VM-52-248-ubuntu:~$ ...
- [译]关于JavaScript 作用域你想知道的一切
原文连接 在学习js的过程对闭包什么的,理解不好,偶然搜到这篇文章.豁然开朗,随翻译. Javacript 中有一系列作用域的概念.对于新的JS的开发人员无法理解这些概念,甚至一些经验丰富的开发者也未 ...
- C++之贪吃蛇
#include<iostream> #include<cstdio> #include<cstdlib> #include<ctime> #inclu ...
- 现有mysql加入redis
spring-dao.xml(注意这里必须加上ignore-unresolvedable): redis.properties: redis.hostname=192.168.1.3 redis.po ...
- Secure Sockets Layer(安全套接层)
SSL SSL(Secure Sockets Layer安全套接层)及其继任者传输层安全(Transport Layer Security,TLS)是为网络通信提供安全及数据完整性的一种安全协议.TL ...
- TensorFlow学习笔记(四)图像识别与卷积神经网络
一.卷积神经网络简介 卷积神经网络(Convolutional Neural Network,CNN)是一种前馈神经网络,它的人工神经元可以响应一部分覆盖范围内的周围单元,对于大型图像处理有出色表现. ...
- 12.MySQL必知必会之分组数据
本文将介绍如何分组数据,以便能汇总表内容的子集,这涉及两个新SELECT语句子句,分别是 GROUP BY 子句和HAVING子句. 1.1 创建分组 分组是在SELECT语句的GROUP BY子句中 ...
- POJ - 1966 Cable TV Network (最大流求点连通度)
题意:求一个无向图的点连通度.点联通度是指,一张图最少删掉几个点使该图不连通:若本身是非连通图,则点连通度为0. 分析:无向图的点连通度可以转化为最大流解决.方法是:1.任意选择一个点作为源点:2.枚 ...