当前位置: 首页 > news >正文

Mybatis使用collection映射一对多查询分页问题

场景:页面展示列表,需要查询多的字段,和一的字段。并且还要分页。
这时候直接想到的是手写sql。
在这里插入图片描述

/*** 标签*/private List<BasicResidentTags> tags;
@Data
@TableName("basic_resident_tags")
public class BasicResidentTags{private static final long serialVersionUID=1L;/*** 标签id*/@TableId(value = "id",type = IdType.AUTO)private Integer id;/*** 名称*/private String name;/*** 颜色*/private String color;/*** 居民id*/private Integer residentId;}

原来的sql 。

<!--一对多映射--><resultMap id="many" type="com.vkl.basic.domain.vo.admin.BasicResidentListVo"><id property="residentId" column="resident_id"/><result property="name" column="name"/><result property="sex" column="sex"/><result property="houseHolder" column="house_holder"/><result property="residentName" column="resident_name"/><result property="outsider" column="outsider"/><result property="room" column="room"/><collection property="tags" ofType="com.vkl.basic.domain.BasicResidentTags"><id column="tagsId" property="id"></id><result column="tagsName" property="name"></result><result column="color" property="color"></result><result column="tid" property="residentId"></result></collection></resultMap>
<select id="selectPageList" resultType="com.vkl.basic.domain.vo.admin.BasicResidentListVo" resultMap="many"parameterType="com.vkl.basic.domain.bo.admin.BasicResidentAdminBo">select b.resident_id,b.`name`,b.sex,b.house_holder,b.resident_name,b.outsider,b.room,t.id as tagsId,t.`name` as tagsName,t.color,t.resident_id as tidfrom basic_resident b LEFT JOIN basic_resident_tags ton b.resident_id = t.resident_idwhere del_flg = '0'<if test="param.name != null and param.name != ''">and b.name like concat('%',#{param.name},'%')orb.resident_name like concat('%',#{param.name},'%')</if><if test="param.tags != null and param.tags != ''">and t.name = #{param.tags}</if>order by b.resident_idlimit #{query.pageNum},#{query.pageSize}</select>

在这里插入图片描述
正常查询tags有两条。加上分页条件,多的一端只有一条数据。
在这里插入图片描述
修改之后的sq。满足分页正常展示多的一端。

<!--一对多映射--><resultMap id="many" type="com.vkl.basic.domain.vo.admin.BasicResidentListVo"><id property="residentId" column="resident_id"/><result property="name" column="name"/><result property="sex" column="sex"/><result property="houseHolder" column="house_holder"/><result property="residentName" column="resident_name"/><result property="outsider" column="outsider"/><result property="room" column="room"/><collection property="tags" ofType="com.vkl.basic.domain.BasicResidentTags"column="tid" select="selectTagsByResidentId"></collection></resultMap><!--主查询条件--><select id="selectPageList" resultType="com.vkl.basic.domain.vo.admin.BasicResidentListVo" resultMap="many"parameterType="com.vkl.basic.domain.bo.admin.BasicResidentAdminBo">select b.resident_id,b.`name`,b.sex,b.house_holder,b.resident_name,b.outsider,b.room,t.id as tagsId,t.`name` as tagsName,t.color,t.resident_id as tidfrom basic_resident b LEFT JOIN basic_resident_tags ton b.resident_id = t.resident_idwhere del_flg = '0'<if test="param.name != null and param.name != ''">and b.name like concat('%',#{param.name},'%')orb.resident_name like concat('%',#{param.name},'%')</if><if test="param.tags != null and param.tags != ''">and t.name = #{param.tags}</if>order by b.resident_idlimit #{query.pageNum},#{query.pageSize}</select><!--子查询--><select id="selectTagsByResidentId" resultType="com.vkl.basic.domain.BasicResidentTags">select * from basic_resident_tags where resident_id=#{tid}</select>
http://www.lryc.cn/news/99220.html

相关文章:

  • Linux/Windows路由管理
  • openpnp - 设备矫正的零碎记录
  • Linux内核中的链表、红黑树和KFIFO
  • 【C++】做一个飞机空战小游戏(二)——利用getch()函数实现键盘控制单个字符移动
  • Android 设备兼容性使用(详细版)
  • React 中的常见 API 和生命周期函数
  • 神经网络中遇到的 python 函数(Pytorch)
  • 分布式事务及解决方案
  • 【宏定义】——编译时校验
  • C#学习系列之System.Windows.Data Error: 40报错
  • 【java安全】RMI
  • rcu链表综合实践
  • odoo16-python框架-动作
  • 微信小程序——同一控件的点击与长按事件共存的解决方案
  • selenium自动化-获取元素属性信息
  • LabVIEW开发小型减阻试验平台
  • 解决分类任务中数据倾斜问题
  • Vue3 word如何转成pdf代码实现
  • fpga--流水灯
  • 51单片机:数码管和矩阵按键
  • Django + Xadmin 数据列表复选框显示为空,怎么修复这个问题?
  • 《向量数据库指南》——Milvus Cloud2.2.12 易用性,可视化,自动化大幅提升
  • Python web实战 | 用 Flask 框架快速构建 Web 应用【实战】
  • 十、数据结构——链式队列
  • Improving Cross-Modal Retrieval with Set of Diverse Embeddings
  • 物联网阀控水表计量准确度如何?
  • 【C语言数据结构】模拟·顺序表·总项目实现
  • 自然语言处理从入门到应用——LangChain:模型(Models)-[文本嵌入模型Ⅰ]
  • 使用Gradio构建生成式AI应用程序; Stability AI推出Stable Diffusion XL 1.0
  • Java 递归计算斐波那契数列指定位置上的数字