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

mybatis嵌套查询子集合只有一条数据

        我们再用mybatis做嵌套查询时,有时会遇到子集合只有1条数据的情况,例如下这样:

数据库查询结果

xml

    <resultMap id="userMap" type="com.springboot.demo.test.entity.User"><id column="uid" property="uid"/><result column="username" property="username"/><result column="tel" property="tel"/><result column="age" property="age"/><collection property="userRoleList" ofType="com.springboot.demo.test.entity.UserRole"><result column="roleid" property="roleid"/></collection></resultMap><select id="selectUser" resultMap="userMap">SELECTa.uid,a.username,a.tel,a.age,b.roleidFROMuser aLEFT JOIN user_role b ON a.uid = b.userid</select>

返回结果

[{"uid": 33,"username": "jon","tel": "123","age": 23,"userRoleList": [{"roleid": 1}]},{"uid": 31,"username": "xiaomi","tel": "110","age": 20,"userRoleList": [{"roleid": 13}]},{"uid": 35,"username": "WANG","tel": "222","age": 33,"userRoleList": [{"roleid": 1}]},{"uid": 34,"username": "xiaocai","tel": "111","age": 32,"userRoleList": [{"roleid": 1}]}
]

很明显,这不是我们期望结果。如果遇到这种情况,可以在子集合的映射里面把id放进去,这样mybatis就会避免上述的情况,如下

    <resultMap id="userMap" type="com.springboot.demo.test.entity.User"><id column="uid" property="uid"/><result column="username" property="username"/><result column="tel" property="tel"/><result column="age" property="age"/><collection property="userRoleList" ofType="com.springboot.demo.test.entity.UserRole"><id column="urid" property="urid"/><result column="roleid" property="roleid"/></collection></resultMap><select id="selectUser" resultMap="userMap">SELECTa.uid,a.username,a.tel,a.age,b.roleid,b.uridFROMuser aLEFT JOIN user_role b ON a.uid = b.userid</select>

结果

[{"uid": 33,"username": "jon","tel": "123","age": 23,"userRoleList": [{"urid": 47,"roleid": 9},{"urid": 48,"roleid": 1}]},{"uid": 31,"username": "xiaomi","tel": "110","age": 20,"userRoleList": [{"urid": 81,"roleid": 9},{"urid": 82,"roleid": 10},{"urid": 83,"roleid": 16},{"urid": 84,"roleid": 1},{"urid": 85,"roleid": 13}]},{"uid": 35,"username": "WANG","tel": "222","age": 33,"userRoleList": [{"urid": 86,"roleid": 11},{"urid": 87,"roleid": 1}]},{"uid": 34,"username": "xiaocai","tel": "111","age": 32,"userRoleList": [{"urid": 92,"roleid": 1}]}
]

http://www.lryc.cn/news/225177.html

相关文章:

  • Github 生成SSH秘钥及相关问题
  • STM32外设系列—MPU6050角度传感器
  • 网站小程序分类目录网源码系统+会员登录注册功能 带完整搭建教程
  • 【Linux网络】手把手实操Linux系统网络服务DHCP
  • Huggingface网页解析和下载爬虫
  • C# Winform 自定义带SWITCH的卡片
  • 我用Devchat开发了公务员报名确认系统自动登录脚本,再也不用担心挤不进去了
  • 如何低门槛开发有趣实用的ZigBee产品?
  • ChatGPT和API发生重大中断!
  • SQL第五次上机实验
  • Matplotlib数据可视化综合应用Matplotlib图形配置在线闯关_头歌实践教学平台
  • CSS实现瀑布流的两种方式
  • Hadoop 视频分析系统
  • Flutter android和ios闪屏页配置
  • 30道高频Vue面试题快问快答
  • vue-前端实现模糊查询
  • QT:tcpSocket 报错The proxy type is invalid for this operation
  • PostgreSQL 技术内幕(十一)位图扫描
  • C# WebSocket 服务器
  • 自动化实战 - 测试个人博客系统
  • TCP/IP详解
  • 2023年的低代码:数字化、人工智能、趋势及未来展望
  • 【gogogo专栏】golang并发编程
  • 深入理解JVM虚拟机第二十二篇:详解JVM当中与操作数栈相关的字节码指令
  • Vue报错解决Error in v-on handler: “Error: 无效的节点选择器:#div1“
  • R | R包安装报错-github连接速度慢或无法访问 | metaboanalystR | Retip | rJava安装
  • 博阳精讯、凡得科技访问上海斯歌:共探BPM流程服务新高地
  • 响应式艺术作品展示前端html网站模板源码
  • 大语言模型(LLM)综述(六):大型语言模型的基准和评估
  • 【Python自学笔记】Flask调教方法Internel Server Error