mybatis中,可以使用collection和association来处理一对多和多对一的关系

其中每个标签中有2中种使用方式,一种是嵌套查询,一种是嵌套结果

嵌套查询会在主查询中,去调用子查询

嵌套结果只需要查询一次,将结果进行封装

一、前提准备

1、数据库表

创建2张表,class和student,class和student是一对多

2张表的数据如下:

class:

student:

2、创建2个实体类对象

TClass

@Data
public class TClass extends Model<TClass> {private static final long serialVersionUID = 1L;private String id;private String name;private String code;@TableField(exist = false)List<TStudent> studentList;
}

TStudent

@Data
public class TStudent extends Model<TStudent> {private static final long serialVersionUID = 1L;private String id;private String name;private String card;private String classid;@TableField(exist = false)private TClass tClass;
}

二、一对多

        一、嵌套结果 方式

1、mapper接口

List<TClass> getClassList();

2、service和serviceImpl省略

3、xml文件

<!-- 嵌套结果 -->
<resultMap id="classMap1" type="com.example.aaa.test.entity.TClass"><id column="id" property="id" /><result column="name" property="name" /><result column="code" property="code" /><collection property="studentList" ofType="com.example.aaa.test.entity.TStudent"><id column="sid" property="id" /><result column="sname" property="name" /><result column="card" property="card" /><result column="classid" property="classid" /></collection>
</resultMap><select id="getClassList" resultMap="classMap1">SELECTc.id,c.name,c.code,s.id as sid,s.name as sname,s.card,s.classidFROMt_class cLEFT JOIN t_student s ON s.classid = c.id
</select>

嵌套结果,在使用的时候需要注意名称相同的字段,需要用别名,在result的column中,需要使用对应的别名

二、嵌套查询 方式

1、mapper接口

List<TClass> getClasses();

2、service接口和serviceImpl省略

3、xml文件

<resultMap id="classMap2" type="com.example.aaa.test.entity.TClass"><id column="id" property="id" /><result column="name" property="name" /><result column="code" property="code" /><collection property="studentList"column="{sid=id}"select="getStudentByCid"ofType="com.example.aaa.test.entity.TStudent"><id column="id" property="id" /><result column="name" property="name" /><result column="card" property="card" /><result column="classid" property="classid" /></collection>
</resultMap><!--主查询-->
<select id="getClasses" resultMap="classMap2">select id,name,code from t_class;
</select><!--子查询-->
<select id="getStudentByCid" resultType="com.example.aaa.test.entity.TStudent">select id,name,card,classid from t_student where classid=#{sid};
</select>

collection标签的解释:

1.property是子查询封装的结果,对应到主查询的哪个属性

2.column是将查询结果中的参数,传递到子查询中{sid=id},id必须出现在父查询的结果集中,子查询中要使用sid

3.select是调用的子查询的语句,不需要在mapper接口中额外添加

4.ofType是封装的List集合中对象的类型

4、controller调用

    @RequestMapping("/getClassList")public Object getStudentList(){List<TClass> list = tClassService.getClassList();return list;}@RequestMapping("/getClasses")public Object getStudens(){List<TClass> list = tClassService.getClasses();return list;}

嵌套结果和嵌套查询,查询结果一样,如图:

三、多对一

一、嵌套结果 方式

1、mapper接口

List<TStudent> getStudentList();

2、service和serviceImpl省略

3、xml文件

<!-- 嵌套结果 -->
<resultMap id="StudentMap1" type="com.example.aaa.test.entity.TStudent"><id column="id" property="id" /><result column="name" property="name" /><result column="card" property="card" /><result column="classid" property="classid" /><association property="tClass" javaType="com.example.aaa.test.entity.TClass"><!-- 含义:cid对应的是TClass类中的id;column=cid是查出结果中的别名,因为id和上面的id重名,所以用别名 --><id column="cid" property="id"></id><!-- 同理,cname也需要用别名,因为和上面的name重复了 --><result column="cname" property="name" /><!-- code不需要别名,因为code是唯一的 --><result column="code" property="code" /></association>
</resultMap><select id="getStudentList" resultMap="StudentMap1">SELECTs.id,s.name,s.classid,s.card,c.id AS cid,c.name AS cname,c.codeFROMt_student sLEFT JOIN t_class c ON s.classid = c.id
</select>

二、嵌套查询 方式

1、mapper接口

List<TClass> getClasses();

2、service和serviceImpl省略

3、xml文件

<!--嵌套查询-->
<resultMap id="StudentMap2" type="com.example.aaa.test.entity.TStudent"><id column="id" property="id" /><result column="name" property="name" /><result column="card" property="card" /><result column="classid" property="classid" /><!--property=tClass是Student中的属性select="getClassBySid"是要调用的子查询column="{cid=classid}"是要传入子查询中的参数,就是结果集中的classid赋值给cid,子查询用cid查询JavaType是子查询结果的类型--><association property="tClass"column="{cid=classid}"javaType="com.example.aaa.test.entity.TClass"select="getClassBySid"><!--不会出现重名,所以不需要改别名--><id column="id" property="id"></id><result column="name" property="name" /><result column="code" property="code" /></association>
</resultMap><select id="getStudents" resultMap="StudentMap2">SELECTid,name,classid,cardFROMt_student
</select><!--子查询不需要在mapper的接口中额外添加-->
<select id="getClassBySid" resultType="com.example.aaa.test.entity.TClass">SELECTid,name,codeFROM t_class<!--cid是从父查询传递过来的-->where id=#{cid}
</select>

4、controller调用

    @RequestMapping("/getStudentList")public Object getStudentList(){List<TStudent> list = tStudentService.getStudentList();return list;}@RequestMapping("/getStudents")public Object getStudens(){List<TStudent> list = tStudentService.getStudents();return list;}

嵌套结果和嵌套查询,查询结果相同,如图:

mybatis一对多,多对一映射,collection和association标签的使用相关推荐

  1. Mybatis - 一对多/多对一映射

    文章目录 前言 项目结构 一.数据库表 1. 员工表 t_emp 2. 部门表 t_dept 二.实体类 1. Emp 员工实体类(添加多对一属性) 2. dept 部门实体类(添加一对多属性) 三. ...

  2. mybatis一对多多对多

    转:http://www.cnblogs.com/selene/p/4627446.html 一:订单商品数据模型 1.数据库执行脚本 创建数据库表代码: View Code 测试数据代码: View ...

  3. MyBatis从入门到精通(十):使用association标签实现嵌套查询

    最近在读刘增辉老师所著的<MyBatis从入门到精通>一书,很有收获,于是将自己学习的过程以博客形式输出,如有错误,欢迎指正,如帮助到你,不胜荣幸! 本篇博客主要讲解使用associati ...

  4. mybatis一对多 多对一

    一.一对多: 实体类 package com.ming.entity;import java.util.List;public class Station {private String Sname; ...

  5. Hibernate关联关系映射-----双向一对多/多对一映射配置

    转自:http://blog.csdn.net/yifei12315/article/details/6985194 /// Hibernate: /// 双向关联就是有"一对多" ...

  6. 关于mybatis中mapper文件resultMap中collection和association的使用

    mybatis mapper配置文件结果集映射resultMap中collection属性(一对多关系结果集映射)和association属性(多对一关系结果集映射)理解: collection的使用 ...

  7. Mybatis(四) 高级映射,一对一,一对多,多对多映射

    天气甚好,怎能不学习? 一.单向和双向 包括一对一,一对多,多对多这三种情况,但是每一种又分为单向和双向,在hibernate中我们就详细解析过这单向和双向是啥意思,在这里,在重复一遍,就拿一对多这种 ...

  8. Mybatis中的关系映射(一对一,一对多,多对多)

    在网上寻了很久,大多数讲关系性的文章都是大篇幅的去将表照搬上来,本来就很生硬,此文就不在讲述关系性映射的具体实现,转而从浅层来讲讲其概念性. 1.1 关联关系概述 在关系型数据库中,多表之间存在着三种 ...

  9. Mybatis一对多及多对多映射查询

    1.数据库准备工作 --删除表 drop table SelectiveInfo; drop table StudentInfo; drop table ClassInfo; drop table T ...

最新文章

  1. 每日一题 -- 11-1
  2. 建立linux两用户之间的信任关系
  3. VTK修炼之道37:图像平滑_高斯滤波器
  4. php-fpm:No pool defined解决方法
  5. POSIX 消息队列相关问题
  6. mysql 两个查询结果合并去重_《MySQL 入门教程》第 21 篇 集合操作符
  7. python删除txt指定内容_使用Python删除文本文件中的部分内容 | 学步园
  8. NGUI_2.6.3_(3D视图ScrollView)
  9. CMake的简单使用
  10. 关于VC2013自动补全问题
  11. winpe装双系统linux_在Winpe下安装Ubuntu
  12. 王阳明的心学精髓是什么?
  13. 真正的英雄(罗纳德·里根在“挑战者号”航天飞机失事后的演讲词)
  14. taptap服务器要维护多久,TapTap发布游戏事故保护功能 解决游戏炸服问题
  15. 测试键盘是否灵敏的软件,u盘启动大师pe检测键盘灵敏度图文教程
  16. Linux解压zip格式压缩包
  17. 2021-5-11 atcoder C - Replacing Integer
  18. 女性内分泌失调的8种症状
  19. CSS样式内联选择器选择器优先级伪类顺序
  20. 职高内蒙古计算机系高考最高分,内蒙古一考生高考668分,数学拿到满分,查分时还在淡定吃烧烤...

热门文章

  1. 高效学习真的有秘籍吗?
  2. 《Adobe Flash CS5 ActionScript 3.0中文版经典教程》——1.3 使用代码片断添加ActionScript...
  3. SQL 常用高级函数大全
  4. java导出excel如何设置单元格样式为文本样式
  5. 数字音频总线A2B开发详解十二(A2B一Master板做音效处理-31段EQ,高中低音分频等)
  6. AngularJS进阶(十九)在AngularJS应用中集成百度地图实现定位功能
  7. 百度云8秒视频,怎么破?
  8. 【视觉算法】霍夫变换(Hough Transform)
  9. T2080RDB: booting vxworks using uboot
  10. C/C++杂志订阅管理系统[2022-12-31]