mybatis的一对多和多对一的连接查询
实体类:
package com.entity;
import java.util.List;
public class Dept {
private Integer deptId;
private String deptName;
private List<Employee> employees;
@Override
public String toString() {
return "Dept{" +
"deptId=" + deptId +
", deptName='" + deptName + '\'' +
", employees=" + employees +
'}';
}
public Integer getDeptId() {
return deptId;
}
public void setDeptId(Integer deptId) {
this.deptId = deptId;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public List<Employee> getEmployees() {
return employees;
}
public void setEmployees(List<Employee> employees) {
this.employees = employees;
}
}
package com.entity;
import java.math.BigDecimal;
public class Employee {
private Integer employeeId;
private String employeeName;
private Integer employeeGender;
private BigDecimal employeeSalary;
private Integer deptId;
private Dept dept;
@Override
public String toString() {
return "Employee{" +
"employeeId=" + employeeId +
", employeeName='" + employeeName + '\'' +
", employeeGender=" + employeeGender +
", employeeSalary=" + employeeSalary +
", deptId=" + deptId +
", dept=" + dept +
'}';
}
public Integer getEmployeeId() {
return employeeId;
}
public void setEmployeeId(Integer employeeId) {
this.employeeId = employeeId;
}
public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
public Integer getEmployeeGender() {
return employeeGender;
}
public void setEmployeeGender(Integer employeeGender) {
this.employeeGender = employeeGender;
}
public BigDecimal getEmployeeSalary() {
return employeeSalary;
}
public void setEmployeeSalary(BigDecimal employeeSalary) {
this.employeeSalary = employeeSalary;
}
public Integer getDeptId() {
return deptId;
}
public void setDeptId(Integer deptId) {
this.deptId = deptId;
}
public Dept getDept() {
return dept;
}
public void setDept(Dept dept) {
this.dept = dept;
}
}
多对一的几种方式:
1.
<mapper namespace="com.dao.EmployeeDao">
<!--<insert id="insert">-->
<!--<selectKey order="BEFORE" keyColumn="employeeId,employeeGender" keyProperty="employeeId,employeeGender" resultType="com.entity.Employee">-->
<!--select employee_id employeeId,employee_gender employeeGender from employee where employee_id=13-->
<!--</selectKey>-->
<!--insert into subtable(employee_id,employee_name,employee_gender) values (#{employeeId},"mdzz",#{employeeGender})-->
<!--</insert>-->
<resultMap id="empResultMap" type="com.entity.Employee">
<id property="employeeId" column="employee_id"></id>
<result property="employeeName" column="employee_name"></result>
<result property="employeeGender" column="employee_gender"></result>
<result property="employeeSalary" column="employee_salary"></result>
<result property="deptId" column="dept_id"></result>
<result property="dept.deptId" column="dept_id"></result>
<result property="dept.deptName" column="dept_name"></result>
</resultMap>
<select id="queryAll" resultMap="empResultMap">
select employee_id,employee_name,employee_gender,employee_salary,e.dept_id,dept_name,d.dept_id from employee e left outer join dept d on e.dept_id = d.dept_id;
</select>
</mapper>
2.
<resultMap id="empResultMap" type="com.entity.Employee">
<id property="employeeId" column="employee_id"></id>
<result property="employeeName" column="employee_name"></result>
<result property="employeeGender" column="employee_gender"></result>
<result property="employeeSalary" column="employee_salary"></result>
<result property="deptId" column="dept_id"></result>
<!--<result property="dept.deptId" column="dept_id"></result>-->
<!--<result property="dept.deptName" column="dept_name"></result>-->
<association property="dept" resultMap="deptResultMap"></association>
</resultMap> <resultMap id="deptResultMap" type="com.entity.Dept">
<id property="deptId" column="dept_id"></id>
<result property="deptName" column="dept_name"></result>
</resultMap> <select id="queryAll" resultMap="empResultMap">
select employee_id,employee_name,employee_gender,employee_salary,e.dept_id,dept_name,d.dept_id from employee e left outer join dept d on e.dept_id = d.dept_id;
</select>
2较之1的好处是单独把Dept类的resultMap提出来,可以复用
3.
<resultMap id="empResultMap" type="com.entity.Employee">
<id property="employeeId" column="employee_id"></id>
<result property="employeeName" column="employee_name"></result>
<result property="employeeGender" column="employee_gender"></result>
<result property="employeeSalary" column="employee_salary"></result>
<result property="deptId" column="dept_id"></result>
<!--<result property="dept.deptId" column="dept_id"></result>-->
<!--<result property="dept.deptName" column="dept_name"></result>-->
<association property="dept">
<id property="deptId" column="dept_id"></id>
<result property="deptName" column="dept_name"></result>
</association>
</resultMap> <select id="queryAll" resultMap="empResultMap">
select employee_id,employee_name,employee_gender,employee_salary,e.dept_id,dept_name,d.dept_id from employee e left outer join dept d on e.dept_id = d.dept_id;
</select>
只不过是第2中方法把<association>的resultMap省略,直接在里面配置管理实体的映射配置,无法做到2那样的resultMap复用
4.不推荐,n+1查询,耗费资源。解决方法有:内嵌resultMap或者使用内部缓存(cache)解决一部分
<resultMap id="empResultMap" type="com.entity.Employee">
<id property="employeeId" column="employee_id"></id>
<result property="employeeName" column="employee_name"></result>
<result property="employeeGender" column="employee_gender"></result>
<result property="employeeSalary" column="employee_salary"></result>
<result property="deptId" column="dept_id"></result>
<association property="dept" column="dept_id" select="getDept"></association>
</resultMap> <select id="queryAll" resultMap="empResultMap">
select employee_id,employee_name,employee_gender,employee_salary,dept_id from employee;
</select> <select id="getDept" resultType="com.entity.Dept">
select dept_id,dept_name from dept where dept_id=#{deptId};
</select>
也就是使用<association>的property指定实体类的字段,column是这个字段所代表的表的主键,即子表的外键(employee的外键dept_id),select指定<select>的id。
再一个注意点就是<select id="getDept">中的select语句的where条件。
一对多:写法和多对一类似
1.resultMap+collection
<mapper namespace="com.dao.DeptDao">
<select id="queryAll" resultMap="deptResultMap">
select employee_id,employee_name,employee_gender,employee_salary,e.dept_id,dept_name,d.dept_id from employee e right outer join dept d on e.dept_id = d.dept_id;
</select> <resultMap id="deptResultMap" type="com.entity.Dept">
<id property="deptId" column="dept_id"></id>
<result property="deptName" column="dept_name"></result>
<collection property="employees" resultMap="empResultMap"></association>
</resultMap> <resultMap id="empResultMap" type="com.entity.Employee">
<id property="employeeId" column="employee_id"></id>
<result property="employeeName" column="employee_name"></result>
<result property="employeeGender" column="employee_gender"></result>
<result property="employeeSalary" column="employee_salary"></result>
<result property="deptId" column="dept_id"></result>
</resultMap>
</mapper>
另一种:n+1查询
<select id="queryAll" resultMap="deptResultMap">
select dept_id,dept_name from dept;
</select> <resultMap id="deptResultMap" type="com.entity.Dept">
<id property="deptId" column="dept_id"></id>
<result property="deptName" column="dept_name"></result>
<collection property="employees" select="getEmp" column="dept_id"></collection>
</resultMap> <select id="getEmp" resultMap="basicEmpResultMap">
select employee_id,employee_name,employee_gender,employee_salary,dept_id from employee;
</select> <resultMap id="basicEmpResultMap" type="com.entity.Employee">
<id property="employeeId" column="employee_id"></id>
<result property="employeeName" column="employee_name"></result>
<result property="employeeGender" column="employee_gender"></result>
<result property="employeeSalary" column="employee_salary"></result>
<result property="deptId" column="dept_id"></result>
</resultMap>
public interface DeptDao {
List<Dept> queryAll();
}
public class DeptDaoTest {
@Test
public void testQueryAll() {
DeptDao deptDao = MapperFactory.generateMapper(DeptDao.class);
List<Dept> depts = deptDao.queryAll();
System.out.println(depts);
}
}
mybatis的一对多和多对一的连接查询的更多相关文章
- mybatis 中一对多、多对一、多对多、父子继承关系
mybatis 中处理一对多.多对一.多对多.父子继承关系的有关键词:association .collection .discriminator id – 一个 ID 结果:标记出作为 ID 的结果 ...
- mybatis的一对多,多对一,以及多对对的配置和使用
1.本文章是无意中看见易百教程的Mybatis教程才注意到这个问题,平时都仅仅是在用CRUD,忽略了这方面的问题,真实十分羞愧 2.首先我们开始对mybatis的一对多的探究 根据这个应用场景 ...
- MyBatis:一对多、多对一处理
多对一的处理 多对一的理解: 多个学生对应一个老师 如果对于学生这边,就是一个多对一的现象,即从学生这边关联一个老师! 数据库设计 CREATE TABLE `teacher` ( `id` INT( ...
- MyBatis数据持久化(七)多表连接查询
本节继续以多表连接查询的案例介绍使用resultMap的好处,对于两张以上的表进行关联查询,当我们有选择的从不同表查询所需字段时,使用resultMap是相当方便的.例如我们有两张表,分别为用户表Us ...
- Mybatis 中一对多,多对一的配置
现在有很多电商平台,就拿这个来说吧.顾客跟订单的关系,一个顾客可以有多张订单,但是一个订单只能对应一个顾客. 一对多的顾客 <?xml version="1.0" encod ...
- Mybatis【一对多、多对一、多对多】知识要点
Mybatis[多表连接] 我们在学习Hibernate的时候,如果表涉及到两张的话,那么我们是在映射文件中使用<set>..<many-to-one>等标签将其的映射属性关联 ...
- EF里一对一、一对多、多对多关系的配置和级联删除
本章节开始了解EF的各种关系.如果你对EF里实体间的各种关系还不是很熟悉,可以看看我的思路,能帮你更快的理解. I.实体间一对一的关系 添加一个PersonPhoto类,表示用户照片类 /// < ...
- EF——一对一、一对多、多对多关系的配置和级联删除 04(转)
EF里一对一.一对多.多对多关系的配置和级联删除 本章节开始了解EF的各种关系.如果你对EF里实体间的各种关系还不是很熟悉,可以看看我的思路,能帮你更快的理解. I.实体间一对一的关系 添加一个P ...
- 六 mybatis高级映射(一对一,一对多,多对多)
1 订单商品数据模型 以订单商品数据为模型,来对mybaits高级关系映射进行学习.
随机推荐
- 个人项目发表到GitHub
个人项目源程序代码发送到github上. 因为之前假期已经注册过了账户,所以只是上传了做过的小账本项目. https://www.cnblogs.com/sdcs/p/8270029.html
- 在线关闭 CLOSE_WAIT状态TCP连接
1.查看某个端口的所有TCP连接: [root@Centos projects]# netstat -anp | tcp6 ::: :::* LISTEN /java tcp6 CLOSE_WAIT ...
- dwr??
官方网站:http://directwebremoting.org/dwr/index.html http://m.blog.csdn.net/u013628152/article/details/5 ...
- Day6 - I - Sticks Problem POJ - 2452
Xuanxuan has n sticks of different length. One day, she puts all her sticks in a line, represented b ...
- DirectX9完全面向对象框架
#pragma once #define UNICODE //Direct3D lib #include<d3d9.h> #include<d3dx9.h> #pragma c ...
- 026、Java中改变运算优先级
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- Django(十二)视图--利用jquery从后台发送ajax请求并处理、ajax登录案例
一.Ajax基本概念 [参考]:https://www.runoob.com/jquery/jquery-ajax-intro.html 异步的javascript.在不全部加载某一个页面部的情况下, ...
- 【转帖】影响超 10 亿设备,博通和 Cypress 芯片曝惊天漏洞,苹果、华为、三星等中招
影响超 10 亿设备,博通和 Cypress 芯片曝惊天漏洞,苹果.华为.三星等中招 https://www.infoq.cn/article/lpNEQGrxZL22gHDPBE2z 26 ...
- C# 关于AD域的操作 (首博)
前段时间(因为懒得找具体的时间了)公司说让系统可以进行对AD域的操作,包括创建用户.于是上网查资料,了解何为AD域.还不知道的这边请https://www.cnblogs.com/cnjavahome ...
- MongoDB 3.2 升级至 3.4.6
MongoDB 升级测试步骤:1.MongoDB版本升级顺序3.2->3.4->3.6->4.0 不能跨版本升级2.升级到3.4后,您不能降级到3.2.7或更早版本.您只能降级到3. ...