MyBatis之慎用association
这里先总结一下 association 存在的问题。
一、内嵌查询时存在报错Id找不到及内存溢出隐患
二、一对多关系数据重复问题
三、多层嵌套内层 association 查询结果为null 或 非预期的值
一、内嵌查询时存在报错Id找不到及内存溢出隐患
参考: https://www.lmlphp.com/user/57996/article/item/1356741/
错误示例:
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.lizhou.dao.StudentDao"><resultMap type="Student" id="studentResult"><id property="id" column="id"/><association property="grade" column="gradeId" select="com.lizhou.dao.GradeDao.getGrade"></association></resultMap><select id="getStudent" resultMap="studentResult">SELECT * FROM student<where><if test="id != null">AND id=#{id}</if></where></select></mapper>
报错:Caused by: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named ‘id’ in ‘class java.lang.Integer’
正确示例:
<mapper namespace="com.lizhou.dao.GradeDao"><resultMap type="Grade" id="gradeResult"><id property="id" column="id"/></resultMap><select id="getGrade" parameterType="Grade" resultMap="gradeResult">SELECT * FROM grade WHERE id=#{id}</select></mapper>
内存溢出隐患示例
错误示例:
StudentMapper.xml:
<mapper namespace="com.lizhou.dao.StudentDao"><resultMap type="Student" id="studentResult"><id property="id" column="id"/><association property="grade" column="gradeId" select="com.lizhou.dao.GradeDao.getGrade"></association></resultMap><select id="getStudent" resultMap="studentResult">SELECT * FROM student WHERE gradeId=#{gradeId}</select></mapper>
GradeMapper.xml:
<mapper namespace="com.lizhou.dao.GradeDao"><resultMap type="Grade" id="gradeResult"><id property="id" column="id"/><collection property="studentList" column="id" ofType="Student" select="com.lizhou.dao.StudentDao.getStudent"></collection></resultMap><select id="getGrade" parameterType="Grade" resultMap="gradeResult">SELECT * FROM grade WHERE id=#{id}</select></mapper>
解决方式:
分离写,分段查询
二、一对多关系数据重复问题
参考:https://blog.csdn.net/wuyezhiyu/article/details/81364974
<resultMap id="commissionRec" type="CommissionRec" ><result property="sourceName" column="cmr_sourceName"/><result property="totalMoney" column="cmr_totalMoney"/><result property="totalCard" column="cmr_totalCard"/><result property="totalCommission" column="cmr_totalCommission"/><result property="commissionTimes" column="cmr_commissionTimes"/><result property="isProvided" column="cmr_isProvided"/><result property="userInfo.userId" column="us_userId"/><result property="userInfo.userName" column="us_userName"/><association property="memberCard" resultMap="memberCardTemplate" /> <!-- <association property="userInfo" resultMap="userInfo" /> --></resultMap>
<resultMap id="userInfo" type="cn.sstech.member.marketing.model.base.BaseUserInfo" ><id property="userId" column="us_userId"/><result property="userName" column="us_userName"/></resultMap>
如上图所示查询结果中,第一条和第二条只有us_userId是不重复的,而剩下的字段都被注入到CommissionRec对象中。这时
1.用association注入userInfo mybatis会因为数据一样而将前两条数据合为一个CommissionRec对象,us_userId则随便调一个注入到userInfo对象中
2.用 <result property=“userInfo.userId” column=“us_userId”/> 这种制定具体字段名的方式注入,mybatis会因为记录数据有所不同而将前两条数据设为2个CommissionRec对象.
三、多层嵌套内层 association 查询结果为null 或 非预期
非预期
本人遇到的就是这个问题
以组织机构树的单张表为例,一般存的有ID及pre_id父级ID,当内嵌查询时,由于父级ID存在多条记录,导致查询结果并非预期
多层嵌套查询结果为null
参考:https://www.zhuxianfei.com/java/57394.html
错误示例:
<resultMap id="BaseResultMap" type="a.b.c.d.e"><id column="id" property="id" /><result property="workTime" column="work_time" /><result property="model" column="model" /><result property="status" column="status" /><association property="interfaceUpstream" javaType="interfaceUpstream" columnPrefix="ui_"><id column="id" property="id" /><result property="interfaceName" column="interface_name" /><result property="interfaceType" column="interface_type" /><result property="frequency" column="frequency" /><result property="address" column="address" /><result property="templateOrSql" column="template_or_sql" /><result property="status" column="status" /><association property="systemInfo" javaType="SystemInfo" columnPrefix="sys_"><id column="id" property="id"/><result property="systemName" column="system_name"/><result property="systemNameEN" column="system_name_en"/><result property="belong" column="belong"/><result property="status" column="status"/></association><association property="serverInfo" javaType="ServerInfo" columnPrefix="ser_"><id column="id" property="id"/><result property="ftpIp" column="ftp_ip"/><result property="ftpPort" column="ftp_port"/><result property="ftpAccount" column="ftp_account"/><result property="ftpPassword" column="ftp_password"/></association></association></resultMap>
<sql id="base_select">SELECTii.Id,ii.model,ii.status,ii.work_time,ui.id AS ui_id,ui.interface_name AS ui_interface_name,ui.interface_type AS ui_interface_type,ui.frequency AS ui_frequency,ui.address AS ui_address,ui.template_or_sql AS ui_template_or_sql,ui.status AS ui_status,sys.id AS sys_id,sys.system_name AS sys_system_name,sys.system_name_en AS sys_system_name_en,sys.belong AS sys_belong,sys.status AS sys_status,ser.id AS ser_id,ser.ftp_ip AS ser_ftp_ip,ser.ftp_port AS ser_ftp_port,ser.ftp_account AS ser_ftp_account,ser.ftp_password AS ser_ftp_password</sql>
结果: 内层的association查询的结果一直为null
原因是association在进行多层嵌套时,mybatis会将外层association的columnPrefix值与内层的进行并合,
如外层columnPrefix值位ui_, 内层为sys_, 那么在SQL中就不能这样 sys.id AS sys_id 了,需要将ui_前缀加上,变成 sys.id AS ui_sys_id ,这样mybatis在匹配的时候才会将数据映射到对应association上
正确示例:
SELECTii.Id,ii.model,ii.status,ii.work_time,ui.id AS ui_id,ui.interface_name AS ui_interface_name,ui.interface_type AS ui_interface_type,ui.frequency AS ui_frequency,ui.address AS ui_address,ui.template_or_sql AS ui_template_or_sql,ui.status AS ui_status,sys.id AS ui_sys_id,sys.system_name AS ui_sys_system_name,sys.system_name_en AS ui_sys_system_name_en,sys.belong AS ui_sys_belong,sys.status AS ui_sys_status,ser.id AS ui_ser_id,ser.ftp_ip AS ui_ser_ftp_ip,ser.ftp_port AS ui_ser_ftp_port,ser.ftp_account AS ui_ser_ftp_account,ser.ftp_password AS ui_ser_ftp_password
总结
1、能不用就别用
2、如果用,请务必指定对应的字段或id
3、多层嵌套时请注意前缀的使用
4、内嵌sql避免递归
相关文章:

MyBatis之慎用association
这里先总结一下 association 存在的问题。 一、内嵌查询时存在报错Id找不到及内存溢出隐患 二、一对多关系数据重复问题 三、多层嵌套内层 association 查询结果为null 或 非预期的值 一、内嵌查询时存在报错Id找不到及内存溢出隐患 参考: https://www.lmlphp.co…...

【Java/大数据】Kafka简介
Kafka简介 Kafka概念关键功能应用场景 Kafka的原理Kafka 的消息模型早期的队列模型发布-订阅模型Producer、Consumer、Broker、Topic、PartitionPartitionoffsetISR Consumer Groupleader选举Controller leaderPartition leader producer 的写入流程 多副本机制replicas的同步时…...
【动手学深度学习】读写文件
【动手学深度学习】读写文件 加载和保存张量 对于单个张量 我么可以直接调用load和save函数分别读写,这两个函数要求我们提供一个名称,save要求保存的变量作为输入 import torch from torch import nn from torch.nn import functional as F# 创建一个…...

http-server 的安装与使用
文章目录 问题背景http-server简介安装nodejs安装http-server开启http服务http-server参数 问题背景 打开一个文档默认使用file协议打开,不能发送ajax请求,只能使用http协议才能请求资源,所以此时我们需要在本地建立一个http服务,…...

SQL高级教程
SQL TOP 子句 TOP 子句 TOP 子句用于规定要返回的记录的数目。 对于拥有数千条记录的大型表来说,TOP 子句是非常有用的。 注释:并非所有的数据库系统都支持 TOP 子句。 SQL Server 的语法: SELECT TOP number|percent column_name(s) F…...

9.pixi.js编写的塔防游戏(类似保卫萝卜)-群炮弹发射逻辑
游戏说明 一个用pixi.js编写的h5塔防游戏,可以用electron打包为exe,支持移动端,也可以用webview控件打包为app在移动端使用 环境说明 cnpm6.2.0 npm6.14.13 node12.22.7 npminstall3.28.0 yarn1.22.10 npm config list electron_mirr…...

分布式链路追踪
文章目录 1、背景2、微服务架构下的问题3、链路追踪4、核心概念5、技术选型对比6、zipkin 1、背景 随着互联网业务快速扩展,软件架构也日益变得复杂,为了适应海量用户高并发请求,系统中越来越多的组件开始走向分布式化,如单体架构…...

计算机网络————网络层
文章目录 网络层设计思路IP地址IP地址分类IP地址与硬件地址 协议ARP和RARPIP划分子网和构造超网划分子网构造超网(无分类编址CIDR) ICMP 虚拟专用网VPN和网络地址转换NATVPNNAT 网络层设计思路 网络层向上只提供简单灵活的、无连接的、尽最大努力交付的数…...

el-table刷新后保持高亮并改变状态字段
一、需求: 1、点击左侧右边显示具体内容 2、点击右边确认 左侧依旧高亮并且改变启动状态颜色 3、点击刷新、重置、高级搜索等不高亮 右边也不显示具体内容 二、效果图: 三、具体实施 1、定义highlight-current-row 是否高亮行 <el-table ref&quo…...
ARM Ubuntu内核更新记录
1,系统版本说明:ARM 鲲鹏920 cat /etc/lsb-release DISTRIB_IDUbuntu DISTRIB_RELEASE18.04 DISTRIB_CODENAMEbionic DISTRIB_DESCRIPTION"Ubuntu 18.04.5 LTS" 2, 将source.list中的deb-src打开 # 默认注释了源码镜像以提高 apt…...
【sgUploadTray】上传托盘自定义组件,可实时查看上传列表进度
【sgUploadTray】上传托盘自定义组件,可实时查看上传列表进度 特性: 可以全屏可以还原尺寸可以最小化可以回到右下角默认位置支持删除队列数据 sgUploadTray源码 <template><div :class"$options.name" :show"show" :size…...

改进二进制粒子群算法在配电网重构中的应用(Matlab实现)【论文复现】
目录 0 概述 1 配电网重构的目标函数 2 算例 3 matlab代码实现 0 概述 配电系统中存在大量的分段开关和联络开关,配电网重构正是通过调整分段开关和联络升大的组合状态来变换网络结构,用于优化配电网某些指标,使其达到最优状态。正常运行时,则通…...

【文章系列解读】Nerf
1. Nerf NeRF: Representing Scenes as Neural Radiance Fields for View Synthesis 2020年8月3日 (0)总结 NeRF工作的过程可以分成两部分:三维重建和渲染。(1)三维重建部分本质上是一个2D到3D的建模过程ÿ…...

基于springboot,vue网上订餐系统
开发工具:IDEA 服务器:Tomcat9.0, jdk1.8 项目构建:maven 数据库:mysql5.7 前端技术 :VueElementUI 服务端技术:springbootmybatisredis 本系统分用户前台和管理后台两部分,项…...
Nautilus Chain 更换全新测试网,主网即将在不久上线
目前,Nautilus Chain 正在为主网上线前的最后阶段做准备,据悉该链更新了全新的测试网,在此前版本的测试网的基础上进行了全新的技术升级,最新测试网版本与生态发展的技术规划更为贴近。本次测试网升级将会是最后一次测试网版本的迭…...
攻防世界web:Web_php_wrong_nginx_config,python3后门
网上的wp中关于Web_php_wrong_nginx_config的后门代码都是python2的(源码来自:Weevely:一个 PHP 混淆后门的代码分析 - Phukers Blog) 以下是转换成python3的版本 # encoding: utf-8from random import randint, choice from ha…...

【VUE】解决图片视频加载缓慢/首屏加载白屏的问题
1 问题描述 在 Vue3 项目中,有时候会出现图片视频加载缓慢、首屏加载白屏的问题 2 原因分析 通常是由以下原因导致的: 图片或视频格式不当:如果图片或视频格式选择不当,比如选择了无损压缩格式,可能会导致文件大小过大…...

spring复习:(35)在getBean时,在哪里根据普通bean和工厂bean进行区分处理来返回的?
在AbstractBeanFactory的doGetBean方法: 调用的getObjectForBeanInstance方法部分代码如下: 如果不是工厂bean,则直接将实例返回,否则调用getObjectFromFactoryBean方法获取工厂bean的getObject方法返回的对象 protected Object getObjectF…...

Jenkins全栈体系(二)
Jenkins 第三章 Jenkins Git Maven 自动化部署配置 十、几种构建方式 快照依赖构建/Build whenever a SNAPSHOT dependency is built 当依赖的快照被构建时执行本job 触发远程构建 (例如,使用脚本) 远程调用本job的restapi时执行本job job依赖构建/Build after other proj…...

c++11 标准模板(STL)(std::basic_istream)(九)
定义于头文件 <istream> template< class CharT, class Traits std::char_traits<CharT> > class basic_istream : virtual public std::basic_ios<CharT, Traits> 类模板 basic_istream 提供字符流上的高层输入支持。受支持操作包含带格式的…...

第二十八章 字符串与数字
第二十八章 字符串与数字 计算机程序完全就是和数据打交道。很多编程问题需要使用字符串和数字这种更小的数据来解决。 参数扩展 第七章,已经接触过参数扩展,但未进行详细说明,大多数参数扩展并不用于命令行,而是出现在脚本文件中。 如果没有什么特殊原因,把参数扩展放…...
【C/C++】EBO空基类优化介绍
空对象优化(Empty Base Optimization,简称 EBO)是 C 编译器的一种 优化技术,用于消除空类作为基类时占用的内存空间,从而避免浪费空间、提升结构体或类的存储效率。 1 什么是“空对象”? 一个**空类&#…...

开源大模型网关:One API实现主流AI模型API的统一管理与分发
以下是对One API的简单介绍: One API是一个使用go语言开发的大语言模型 API 管理与分发系统支持Docker一键快速部署,且资源占用小,高性能开箱支持多平台大模型快速接入,包括OpenAI、Gemini、xAI、Grop、Anthropic Claude、Ollama…...

智能网卡之hinic3 WQE(Work Queue Element)结构梳理
hinic3 WQE(Work Queue Element)结构详解 本文基于 hinic3 驱动源码,对 WQE(Work Queue Element)做详细讲解。如需查阅完整源码和结构体定义可参考hinic3_nic_qp.h等文件。 1. WQE 的作用 WQE(Work Queue…...
网页端 VUE+C#/FastAPI获取客户端IP和hostname
1 IP可以获取,但是发现获取到的是服务端的IP,如何解决呢。 如果采用nginx反向代理,那么可以在conf/nginx.conf文件中配置 location /WebApi/ { proxy_pass http://localhost:5000/; #这个/会替换location种的WebApi路径 #关键,加…...

编程基础:执行流
能帮到你的话,就给个赞吧 😘 文章目录 执行流同步:顺序执行,只有一个执行流异步:新开后台(次)执行流,后台执行流要确保不能影响主执行流。共有两个执行流。 阻塞:任务阻塞执行流,导致…...

网络爬虫一课一得
网页爬虫(Web Crawler)是一种自动化程序,通过模拟人类浏览行为,从互联网上抓取、解析和存储网页数据。其核心作用是高效获取并结构化网络信息,为后续分析和应用提供数据基础。以下是其详细作用和用途方向: …...
SQL进阶之旅 Day 18:数据分区与查询性能
【SQL进阶之旅 Day 18】数据分区与查询性能 文章简述 在现代数据库系统中,随着数据量的快速增长,如何高效地管理和查询大规模数据成为开发人员和数据分析师面临的重要挑战。本文深入探讨了数据分区的概念及其对查询性能的提升作用,结合理论…...
使用Python和Scikit-Learn实现机器学习模型调优
在机器学习项目中,模型的性能往往取决于多个因素,其中模型的超参数(hyperparameters)起着关键作用。超参数是模型在训练之前需要设置的参数,例如决策树的深度、KNN的邻居数等。合理地选择超参数可以显著提升模型的性能…...

Deepseek基座:Deepseek-v2核心内容解析
DeepSeek原创文章1 DeepSeek-v3:基于MLA的高效kv缓存压缩与位置编码优化技术 2 Deepseek基座:DeepSeek LLM核心内容解析 3 Deepseek基座:Deepseek MOE核心内容解析 4 Deepseek基座:Deepseek-v2核心内容解析 5Deepseek基座…...