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 代码进行对比,你会 ...
随机推荐
- php 乘除法原理
w $wdays = ceil(($wmaxutime-$wminutime)/(24*3600)); $wdays = ceil(($wmaxutime-$wminutime)/243600); 二 ...
- Apache Samza - Reliable Stream Processing atop Apache Kafka and Hadoop YARN
http://engineering.linkedin.com/data-streams/apache-samza-linkedins-real-time-stream-processing-fram ...
- 该死的Kafka,远程连接Kafka超时以及解决办法
关于消息的发布与订阅,之前一直使用的是activeMQ基于JMS的消息队列进行操作的,最近听说有一个更高效的消息的发布与订阅技术,就是Kafka. 关于kafka的介绍,在这里就不做过多讲解了,因为我 ...
- C#、devExpress 的 给bandedGrid加菜单功能 :复制、粘贴的例子(转)
C#.devExpress 的 给bandedGrid加菜单功能 :复制.粘贴的例子 CopyFromGrid PasteToGrid PasteNewRowsToGrid private void ...
- python 中几个层次的中文编码.md
转自:[http://swj.me/] 介绍 一直不太喜欢使用命令行,所以去年年底的技术创新中,使用TkInter来开发小工具.结果花费了大量的时间来学习TkInter ui的使用. 最近想整理该工具 ...
- Zipline入门教程
Zipline Beginner Tutorial Basics 基础 Zipline is an open-source algorithmic trading simulator written ...
- 监控之snmpd 服务
监控离不开数据采集,经常使用的Mrtg ,Cacti,Zabbix,等等监控软件都是通过snmp 协议进行数据采集的! 1 什么是snmp 协议? 简单网络管理协议(SNMP,Simple Netwo ...
- Python 面向对象进阶(二)
1. 垃圾回收 小整数对象池 Python对小整数的定义是 [-5, 257),这些整数对象是提前建立好的; 在一个Python程序中,所有位于这个范围内的整数,使用的都是同一个对象; 单个字符共用对 ...
- Unity3D优化技巧系列七
笔者介绍:姜雪伟,IT公司技术合伙人.IT高级讲师,CSDN社区专家,特邀编辑.畅销书作者,国家专利发明人;已出版书籍:<手把手教你架构3D游戏引擎>电子工业出版社和<Unity3D ...
- air游戏接入小米支付sdk
小米支付sdk要求在Application.onCreate中进行初始化 为了这个初始化搞了半天,最终搞定了.今天将更改的步骤记录下了. 1. 创建ANE.ANE的创建就不罗嗦了,这里须要注意一点,这 ...