一、条件

  • 查询班级表 返回所有学生信息  (一对多问题)

二、数据库

班级class_info

学生student

二、代码实现

<!--        多对一  或者 一对一   -->
<!--        <association property=""--><!--        一对多 返回集合-->
<!- -  <collection  property=""- ->

实体类ClassInfo.java

@Data
public class ClassInfo {private Long id;private String name;private String nameTest;private List<Student> studentList;
}

ClassInfoMapper.xml

<?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层某个接口的包的全名称-->
<mapper namespace="com.example.demo.mapper.ClassInfoMapper"><!--    查询班级 返回所有学生的信息   一对多--><!--    自定义映射规则--><resultMap id="OneToMany" type="com.qcby.zsgc.entity.ClassInfo"><result column="name" jdbcType="VARCHAR" property="nameTest" /><collection  column="{id1=id,name=name}"property="studentList"select="com.example.demo.mapper.StudentMapper.listByClassInfoId">                 </collection></resultMap><select id="listAllWithStudent" resultMap="OneToMany">select * from class_info</select>

关联StudentMapper.xml中的子查询

 <select id="listByClassInfoId" resultType="com.example.demo.entity.Student">SELECT*FROMstudent swhere class_info_id = #{id1} or name = #{name}</select>

ClassInfoMapper.java

public interface ClassInfoMapper extends BaseMapper<ClassInfo> {IPage<ClassInfo> listAllWithStudent(IPage<ClassInfo> page);}

ClassInfoService.java

public interface ClassInfoService extends IService<ClassInfo> {IPage<ClassInfo> listAllWithStudent(IPage<ClassInfo> page);}

ClassInfoServiceImpl.java

@Service
public class ClassInfoServiceImpl extends ServiceImpl<ClassInfoMapper, ClassInfo> implements ClassInfoService {@Autowiredprivate StudentService studentService;@Overridepublic IPage<ClassInfo> listAllWithStudent(IPage<ClassInfo> page) {return this.baseMapper.listAllWithStudent(page);}
}

ClassInfoController.java

@Controller
@RequestMapping("classInfo")
public class ClassInfoController {@Autowiredprivate ClassInfoService classInfoService;@RequestMapping("listAllWithStudent")@ResponseBodypublic IPage<ClassInfo> listAllWithStudent(Integer pageNo,Integer pageSize){Page<ClassInfo> page = new Page<>(pageNo,pageSize);return classInfoService.listAllWithStudent(page);}}

mybatis plus一对多查询(经典案例)相关推荐

  1. 【SQL查询系列】子查询经典案例

    文章目录 前言 数据库 查询案例 1. 查询工资最低的员工信息: last_name, salary 2. 查询平均工资最低的部门信息 3. 查询平均工资最低的部门信息和该部门的平均工资 4. 查询平 ...

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

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

  3. Mybatis的一对多查询

    表如下: create table student(sid int(11) primary key not null, sname varchar(20), cardid int(11));creat ...

  4. Mybatis+mysql动态分页查询数据案例——Mybatis的配置文件(mybatis-config.xml)

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC & ...

  5. Mybatis+mysql动态分页查询数据案例——配置映射文件(HouseDaoMapper.xml)

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "- ...

  6. Mybatis+mysql动态分页查询数据案例——房屋信息的接口(IHouseDao)

    package cn.bdqn.mhouse.dao;import java.util.List;import cn.bdqn.mhouse.entity.House; import cn.bdqn. ...

  7. Mybatis+mysql动态分页查询数据案例——条件类(HouseCondition)

    package cn.bdqn.mhouse.entity; /*** * * 项目名称:house * 类名称:HouseCondition * 类描述: 动态查询房屋信息的条件类 * 创建人:Mu ...

  8. Mybatis+mysql动态分页查询数据案例——分页工具类(Page.java)

    package cn.bdqn.mhouse.util;import java.util.ArrayList; import java.util.List;import cn.bdqn.mhouse. ...

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

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

最新文章

  1. 上传Android或Java库到Maven central repository(转载)
  2. 10小时,这回一次搞定 Kafka 源码!
  3. 2017年我国SAP行业三大利好
  4. 20201205 旋转矩阵导数的推导过程
  5. Go语言JSON与Byte[]转化
  6. XCTF-高手进阶区:Web_php_include
  7. cvAdd()和 cvAddS()函数的使用
  8. springmvc log4j配置
  9. 语义分割论文阅读:FCN、PSPNet、DDRNet、BiseNet、BiseNetV2、deeplabv3
  10. 对 5G “迟钝”的苹果,该如何后来居上?| 极客头条
  11. 【图像处理】基于matlab GUI数字图像处理平台【含Matlab源码 381期】
  12. 我理解的17种C#写的Hello World程序
  13. 关于Vivado Simulator-ERROR: Cannot find design unit work. in library work located at xsim.dir /work
  14. TPC,TPCC,TPMC(数据库性能衡量指标)
  15. 应用加密大师计算机,Windows文件夹加密大师
  16. 「OceanBase 4.1 体验」|快速安装部署
  17. 输入子系统--按键驱动
  18. 大电流输出信号隔离转换模块
  19. poj 3084(最小割)
  20. AI_综述:3D目标检测于RGB-D(Object detection in RGB-D images)

热门文章

  1. 红外光谱分析不翻车,红外三要素你真的学会(废)了吗?(二)
  2. ubuntu中文输入法设置
  3. 联想台式计算机功率,【联想台式机】联想扬天6100台式机改装nas主机,联想台式机怎么进入bios_什么值得买...
  4. 100多种清除木马方法
  5. 机床数据采集之Mitsubishi三菱M70/M80数控IP地址参数设定
  6. 【光学】基于matlab光纤布拉格光栅FBG反射谱和透射谱仿真【含Matlab源码 2654期】
  7. CDLINUX 破解wifi最完整教程
  8. 求大数的n次方对m取模(欧拉降幂)
  9. curl进行命令post formdata json file
  10. echarts 百度地图,城市区域场景标识参数意义配置分析