mybatis映射文件select_resultMap_关联查询_collection定义关联集合
知识点:查询一个实体类,并查出这个类下面的集合
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.java实体类
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 + "]";
}
}
DepartmentMapper.java接口
package com.hand.mybatis.dao;
import com.hand.mybatis.bean.Department;
public interface DepartmentMapper {
Department getDeptById(Integer did);
//根据部门id,查询部门信息,同时将部门下的所有员工信息查询出来
Department getDeptByIdPlus(Integer did);
//分步查询部门信息
Department getDeptByIdStep(Integer did);
}
DepartmentMapper.xm l映射文件
<?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.DepartmentMapper">
<!-- Department getDeptById(Integer did); -->
<select id="getDeptById" resultType="com.hand.mybatis.bean.Department">
SELECT did id,deptname departName FROM dept WHERE did=#{did}
</select>
<!--1.查询部门的时候将部门对应的所有员工信息也查询出来
-->
<resultMap type="com.hand.mybatis.bean.Department" id="departmentMap">
<result property="id" column="did"/>
<result property="departName" column="deptname"/>
<!--collection定义关联集合类型的属性的封装规则
ofType:指定集合里面元素的类型 -->
<collection property="empList" ofType="com.hand.mybatis.bean.Employee">
<!-- 定义这个集合元素的封装规则 -->
<id column="eid" property="eId"/>
<result column="ename" property="eName"/>
<result column="email" property="email"/>
<result column="gender" property="gender"/>
</collection>
</resultMap>
<!-- Department getDeptByIdStep(Integer did); --> 接口一:根据部门did查询部门信息,员工信息
<select id="getDeptByIdPlus" resultMap="departmentMap">
SELECT d.did,d.deptname,e.eid,e.ename,e.email,e.gender
FROM dept d
LEFT JOIN emp e
ON d.did=e.did
WHERE d.did=#{did}
</select>
<!--2. select_resultMap关联查询collection分步查询 -->
<resultMap type="com.hand.mybatis.bean.Department" id="departmentMapPlus">
<id column="did" property="id"/>
<result column="deptname" property="departName"/>
<collection property="empList"
select="com.hand.mybatis.dao.EmployeeMapper.getEmpByDeptId"
column="{dId=did}" fetchType="lazy">//可以多列传值
</collection>
</resultMap>
<!--Department getDeptByIdStep(Integer did); -->接口二:根据部门id分步查询员工信息
<select id="getDeptByIdStep" resultMap="departmentMapPlus">
SELECT did,deptname
from dept
WHERE did=#{did}
</select>
<!-- 扩展,多列的值传递过去,将多列的值封装map传递
column="{key1=column1,key2=column2}"
fetchType="lazy":表示使用延迟加载
- lazy:延迟
- eager:立即-->
</mapper>
代码:https://github.com/shuaishuaihand/mybatis.git
mybatis映射文件select_resultMap_关联查询_collection定义关联集合的更多相关文章
- Mybatis3.1-[tp_34-35]-_映射文件_select_resultMap关联查询_collection定义关联集合封装规则_collection分步查询_延迟加载
笔记要点出错分析与总结工程组织 1.定义接口 interface DepartmentMapper package com.dao; import com.bean.Department; publi ...
- MyBatis映射文件的resultMap如何做表关联
MyBatis的核心是其映射文件,SqlMap文件,里面配置了项目中用到了什么SQL语句,和数据库相关的逻辑都在这个映射文件里.顾名思义,映射文件就是对Java对象和SQL的映射.这里简单介绍一下映射 ...
- SSM实战——秒杀系统之DAO层实体定义、接口设计、mybatis映射文件编写、整合Spring与Mybatis
一:DAO实体编码 1:首先,在src目录下,新建org.myseckill.entity包,用于存放实体类: 2:实体类设计 根据前面创建的数据库表以及映射关系,创建实体类. 表一:秒杀商品表 对应 ...
- MyBatis 映射文件详解
1. MyBatis 映射文件之<select>标签 <select>用来定义查询操作; "id": 唯一标识符,需要和接口中的方法名一致; paramet ...
- MyBatis映射文件 相关操作
一.MyBatis映射文件 1.简介 MyBatis 的真正强大在于它的映射语句,也是它的魔力所在.由于它的异常强大,映射器的 XML 文件就显得相对简单.如果拿它跟具有相同功能的 JDBC 代码进行 ...
- MyBatis 映射文件
Mybatis映射文件简介 1) MyBatis 的真正强大在于它的映射语句.由于它的异常强大,映射器的 XML 文件就显得相对简单.如果拿它跟具有相同功能的 JDBC 代码进行对比,你会立即发现省掉 ...
- Mybatis映射文件标签(关于sql)
Mybatis映射文件 1.接口的全限定名和映射文件的namespace一致 <mapper namespace="com.offcn.dao.UserDao"> 2. ...
- Mybatis映射文件完整模板参照
Mybatis映射文件完整模板参照 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE map ...
- Mybatis映射文件中#取值时指定参数相关规则
Mybatis映射文件中#取值时指定参数相关规则 在#{}中,除了需要的数值外,还可以规定参数的一些其他规则. 例如:javaType,jdbcType,mode(存储过程),numericScale ...
随机推荐
- Open 常用开源
msdn:https://msdn.microsoft.com/zh-cn codeplex:https://www.codeplex.com/ gotdotnet:http://www.gotdot ...
- Service Mesh服务网格:是什么和为什么
Service Mesh服务网格:是什么和为什么 - 好雨云帮 CSDN 博客 - CSDN博客 https://blog.csdn.net/zyqduron/article/details/8043 ...
- Windows的445端口(文件共享)
周鸿祎:教育网大量电脑445端口暴露,导致中招_科技_腾讯网 http://tech.qq.com/a/20170513/016133.htm 互联网周鸿祎2017-05-13 12:04 据36 ...
- ELK basic---http://udn.yyuap.com/doc/logstash-best-practice-cn/filter/grok.html
http://blog.csdn.net/lgnlgn/article/details/8053626 elasticsearch学习入门 input {stdin{}}filter { grok { ...
- Restful and 前后端分离---AutoTest newman--postman
http://www.cnblogs.com/zuoshaowei/p/6192863.html https://www.getpostman.com/docs/newman_intro swagge ...
- GNU Screen使用入门
前些天开始学习使用GNU Screen程序,发现这个工具在管理服务器时候确实挺方便的,于是写一篇文章总结一下,顺便介绍Screen的基本使用方法. 简介 GNU Screen是 一个基于文本的全屏窗口 ...
- http://echarts.baidu.com/demo.html#effectScatter-map
http://echarts.baidu.com/demo.html#effectScatter-map
- HBase在HDFS上的目录树
众所周知,HBase 是天生就是架设在 HDFS 上,在这个分布式文件系统中,HBase 是怎么去构建自己的目录树的呢? 这里只介绍系统级别的目录树: 一.0.94-cdh4.2.1版本 系统级别的一 ...
- PAT 1091 Acute Stroke [难][bfs]
1091 Acute Stroke (30 分) One important factor to identify acute stroke (急性脑卒中) is the volume of the ...
- 剑指offer-基本思想学习(未包括代码)
转自:https://blog.csdn.net/Together_CZ/article/details/74906427 1.面试7:使用两个栈实现一个队列. //猛一看有点晕,实际上很简单. 使用 ...