表如下:

create table student(sid int(11) primary key not null,
sname varchar(20),
cardid int(11));create table studentclass(cid int primary key not null,cname varchar(20)
);
alter table student add column cid int;alter table student add constraint fk_student_studentclass
foreign key(cid) references studentclass(cid);

以student和studentClass为例

package entity;import java.util.List;/*** @ClassName:StudentClass* @Description:* @author:zgy19* @data:2020/1/14 16:36* @History:* @UpdateDate:* @author:* @UpdateContent:*/
public class StudentClass {private Integer cid;private String cname;//增加学生属性List<Student> students;public List<Student> getStudents() {return students;}public void setStudents(List<Student> students) {this.students = students;}public Integer getCid() {return cid;}public void setCid(Integer cid) {this.cid = cid;}public String getCname() {return cname;}public void setCname(String cname) {this.cname = cname;}public StudentClass(Integer cid, String cname, List<Student> students) {this.cid = cid;this.cname = cname;this.students = students;}public StudentClass() {}}

StudentMapper.xml

<!--一对多--><select id="TestOneForMore" resultMap="studentclass_student_map">SELECT c.*,s.* from studentclass c inner join student son c.cid=s.cidwhere c.cid=#{cid}</select><resultMap id="studentclass_student_map" type="entity.StudentClass"><id property="cid" column="cid"></id><result property="cname" column="cname"></result><collection property="students" ofType="Student"><id property="sid" column="sid"></id><result property="sname" column="sname"></result><result property="cardid" column="cardid"></result><result property="cid" column="cid"></result><association property="studentCard" javaType="entity.StudentCard"><id property="cardid" column="cardid"></id><result property="cardinfo" column="cardinfo"></result></association></collection></resultMap>

这里一个班级对应多个学生,所以将学生以List<Student>作为属性。将两个类相关联。
测试结果:

班级编号:1,班级名:软件161,
学生姓名:王富贵,编号:1,班级编号:1
学生姓名:曾小贤,编号:3,班级编号:1

如果再加上studentCard的话
StudentMapper.xml
改了一下select语句

<!--一对多--><select id="TestOneForMore" resultMap="studentclass_student_map">SELECT c.*,s.* ,r.* from ((studentclass c inner join student s on c.cid=s.cid) LEFT JOIN studentcard ron s.cardid=r.cardid)where c.cid=#{cid}</select><resultMap id="studentclass_student_map" type="entity.StudentClass"><id property="cid" column="cid"></id><result property="cname" column="cname"></result><collection property="students" ofType="Student"><id property="sid" column="sid"></id><result property="sname" column="sname"></result><result property="cardid" column="cardid"></result><result property="cid" column="cid"></result><association property="studentCard" javaType="entity.StudentCard"><id property="cardid" column="cardid"></id><result property="cardinfo" column="cardinfo"></result></association></collection></resultMap>

测试结果:

班级编号:1,班级名:软件161,
学生姓名:王富贵,编号:1,班级编号:1,cardid:1,cardinfo:学生公交卡
学生姓名:曾小贤,编号:3,班级编号:1,cardid:3,cardinfo:学生购物卡

延迟加载
如果不采用延迟加载,在加载时,会将所有信息全部查询出来,无论你是否需要。这种情况在数据量很大时,会大大增加查询时间。降低效率。

使用延迟加载,先查询需要的数据,其他数据在需要时再加载。
设置延迟加载:
StudentMapper.xml

<!--一对多以及延迟加载--><select id="TestOneForMoreAndLazyLoad" resultMap="studentclass_student_lazyload_map">SELECT * from studentclasswhere cid=#{cid}</select><resultMap id="studentclass_student_lazyload_map" type="entity.StudentClass"><id property="cid" column="cid"></id><result property="cname" column="cname"></result><collection property="students" ofType="Student" select="selectStudentByCid" column="cid"></collection></resultMap><select id="selectStudentByCid" resultType="Student">SELECT * from student where cid=#{cid}</select>

解释:重点标签

<collection property="students" ofType="Student" select="selectStudentByCid" column="cid"></collection>

这里的select中是需要延迟加载的语句,column是关联外键
此外还需要在config.xml中配置开启懒加载
config.xml

<settings><setting name="lazyLoadingEnabled" value="true"/><setting name="aggressiveLazyLoading" value="false"/><!--开启日志,并指定使用日志的类型--><setting name="logImpl" value="LOG4J"></setting></settings>

测试结果:

Mybatis的一对多查询相关推荐

  1. mybatis教程--一对多查询

    一. 一对多查询 1.1 需求 查询所有订单信息及订单下的订单明细信息. 1.2 sql语句 这里我们需要查询的表有订单表和订单详情表 主查询表:订单表 关联查询表:订单明细 SELECTorders ...

  2. mybatis plus一对多查询(经典案例)

    一.条件 查询班级表 返回所有学生信息  (一对多问题) 二.数据库 班级class_info 学生student 二.代码实现 <!-- 多对一 或者 一对一 --> <!-- & ...

  3. mybatis高级映射一对多查询(一)

    最近一直在研究mybatis,查询是并不可少的研究内容.mybatis的一对多的查询,个人觉得比hibernate简单的很多.好了,废话不多说了,下面以一个简单的例子解释一下mybatis的一对多的查 ...

  4. MyBatis研习录(09)——MyBatis一对多查询

    C语言自学完备手册(33篇) Android多分辨率适配框架 JavaWeb核心技术系列教程 HTML5前端开发实战系列教程 MySQL数据库实操教程(35篇图文版) 推翻自己和过往--自定义View ...

  5. Mybatis一对多查询(附带多条件查询)

    场景:现有两张表 s_class(班级表) s_student(学生表) 需求:查询出班级和班级下的学生 分析:首先班级和学生是一对多的关系,一个班级对应多个学生 实现方法: 方法一:通过查出班级,再 ...

  6. mybatis plus 多表查询_Mybatis 多表查询之一对多

    本次案例主要以最为简单的用户和账户的模型来分析Mybatis多表关系.用户为User 表,账户为Account 表.一个用户(User)可以有多个账户(Account).具体关系如下: 3.1 一对一 ...

  7. mybatis 一对多查询 按结果嵌套处理、按查询嵌套处理,以及两者之间的区别

    mybatis 一对多查询 按结果嵌套处理.按查询嵌套处理 最近用到一对多查询,记录一下 实体类 public class RegionEntity implements Serializable { ...

  8. 【MyBatis框架】高级映射-一对多查询

    前面学习了一对一的查询,现在我们在刚才的数据模型基础上进行一对多的查询. 一对多查询 1.需求 查询订单及订单明细的信息. 2.sql语句 确定主查询表:订单表 确定关联查询表:订单明细表 在一对一查 ...

  9. mybatis多条件批量查询_Mybatis【14】 Mybatis如何实现一对多查询?

    注:代码已托管在GitHub上,地址是:https://github.com/Damaer/Mybatis-Learning ,项目是mybatis-10-one2many,需要自取,需要配置mave ...

最新文章

  1. 程序员 专属的新年祝福原来是这样的! (附中奖名单)
  2. Spring-Boot:写出来的网站访问不到静态资源?怎样通过url访问SpringBoot项目中的静态资源?localhost:8989/favicon.ico访问不了工程中的图标资源?
  3. ffbe攻略站_最终幻想勇气启示录ffbe兵员强化攻略
  4. Fedora 22 Linux 系统将于 7月 19日停止支持
  5. 线性瘤是良性吗_良性聚会:露营者如何构建开放源代码工具来解决时区
  6. js操作json对象
  7. ansys19.2安装教程
  8. 互联网公司的三高问题
  9. 鼠标不能动怎么选择计算机,鼠标动不了怎么办 电脑鼠标不动了按什么键
  10. 史上最全BigDecimal的5种进位方式:ROUND_UP,ROUND_DOWN,ROUND_CEILING,ROUND_FLOOR,ROUND_HALF_UP,ROUND_HALF_DOWN的比较
  11. 12道Java高级面试题:java时间差计算
  12. 第二篇学会感谢身边的所有人!
  13. 软件测试职业规划:发展方向多元化
  14. 十分钟搞定SSD1963液晶屏驱动
  15. 京东文件存储服务器,紫晶存储智能家庭云服务器登录中国移动和彩云 首个京东官方旗舰店助力消费者业务...
  16. PHP API 接口文档生成 简单版本 基于一位大哥的代码改的
  17. ZOJ 2095 Divisor Summation
  18. android 所有的服务,Android NSD未发现所有服务
  19. 华为电脑wrtw29安装Linux,华为MateBook13WRT-W29对比苹果 MacBook Air
  20. 51单片机_11-2 蜂鸣器播放音乐

热门文章

  1. 网络慢是带宽不足?—Vecloud微云
  2. C++ primer 详解(第三章)
  3. [转]DICOM医学图像处理:Deconstructed PACS之Orthanc
  4. CSS之 :before :after的用法,伪类和伪元素的区别
  5. 定义变量时一定要初始化
  6. 关于枚举概念的理解以及存在意义
  7. Fedora 30系统下,用g++编译opencv项目
  8. FPGA实验四——时间基准电路和带使能的多周期计数器
  9. c++ linux 线程等待与唤醒_Linux线程同步(互斥量、信号量、条件变量、生产消费者模型)...
  10. opencv 图像 抠图 算法_人人可用的在线抠图,AI自动化的那种!北大校友算法玩出新高度...