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 ...
随机推荐
- Whether to hide the cookie from JavaScript
w禁用js访问特定cookie. https://codeigniter.com/userguide3/helpers/cookie_helper.html $this->load->he ...
- Ckeditor事件绑定
最近有个需求是要在点击CKeditor的时候触发某个判断的事件.试了一些方法都不可行,自己写的onclick时间都会被编辑器屏蔽.可以对对象加载完成绑定事件代码如下. CKEDITOR.instanc ...
- 饭卡---hdu2546(01背包)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2546 这是一个变形的01背包问题,首先如果金额小于5元,剩余金额不变,为已有金额.如果大于等于5元 我 ...
- UVA Team Queue
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u013840081/article/details/26180081 题目例如以下: Team Qu ...
- UVA10763:Foreign Exchange&&UVA10340: All in All(水题)
10763:水题不解释直接贴代码. #include <iostream> #include <string.h> #include <stdio.h> #incl ...
- Delphi APP 開發入門(二)Android/iOS設定,Hello World
Delphi APP 開發入門(二)Android/iOS設定,Hello World 分享: Share on facebookShare on twitterShare on google_plu ...
- Maven打包部署
Maven打Jar包 问题一 Eclipse突然SB告诉我类不存在还让我导包进来O__O "…,但是我明明有这个类.这是不要慌,通过在网上搜索得知:由于eclipse的编译是基于时间戳的判断 ...
- [转]linux内核分析笔记----内存管理
转自:http://blog.csdn.net/Baiduluckyboy/article/details/9667933 内存管理,不用多说,言简意赅.在内核里分配内存还真不是件容易的事情,根本上是 ...
- centos7 最小化安装后的配置优化
echo #CENTOS7echo #1.最小化安装之后需要做的事echo 2.配置echo 2.1 安装网络yum install net-tools -y echo 2.2 更新机器名echo h ...
- quic协议实践
QUIC实践 环境Ubuntu14.x86_64 gcc4.8 python2.7 编译QUIC服务器和客户端 下载 git clone https://github.com/google/proto ...