MyBatis之慎用association

news2025/2/1 13:50:07

这里先总结一下 association 存在的问题。

一、内嵌查询时存在报错Id找不到及内存溢出隐患
二、一对多关系数据重复问题
三、多层嵌套内层 association 查询结果为null 或 非预期的值

一、内嵌查询时存在报错Id找不到及内存溢出隐患

参考: https://www.lmlphp.com/user/57996/article/item/1356741/
错误示例:

<?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 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">
    SELECT
    ii.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上

正确示例:

SELECT
    ii.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避免递归

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/762835.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

DP358/321/323/324运算放大器芯片

DP358、DP321、DP323、DP324是一款低噪声、低压、低 功耗轨到轨输出运放大器&#xff0c;该系列放大器的增益带宽为 11MHz,压摆率为 8.5V/uS,其中DP323 在掉电工作模式下待机电流小于1uA。该系列放大器可以广泛应用于各种电子产品领域。 主要特性&#xff1a; 轨到轨最大输入…

电脑提示msvcr110.dll丢失怎样修复呢?推荐三个修复方法

Windows系统总是不可避免会出现系统报错&#xff0c;提示msvcr110.dll丢失&#xff0c;无法运行启动软件程序&#xff0c;主要就是由于系统的msvcr110.dll丢失或者损坏。msvcr110.dll是Microsoft Visual C Redistributable软件包中的一个文件&#xff0c;它是由Microsoft Visua…

【文生图系列】Stable Diffusion Webui安装部署过程中bug汇总(Linux系统)

文章目录 bugs虚拟环境pythongfpgan和cython bugs 看网上部署stable diffusion webui的教程&#xff0c;很简单。而且我也有部署stable diffusion v1/v2的经验&#xff0c;想着应该会很快部署完stable diffusion webui&#xff0c;但是没想到在部署过程中&#xff0c;遇到各种…

【数据分析 - 基础入门之pandas篇③】- pandas数据结构——DataFrame

文章目录 前言一、DataFrame创建1.1 字典创建1.2 NumPy二维数组创建 二、DataFrame切片2.1 行切片2.2 列切片2.3 行列切片 三、DataFrame运算3.1 DataFrame和标量的运算3.2 DataFrame之间的运算3.3 Series和DataFrame之间的运算 四、DataFrame多层次索引4.1 多层次索引构造1.隐…

AJAX异步请求JSON数据格式

目录 前言 1.AJAX的实现方式 1.1原生的js实现方式 1.2JQuery实现方式 2.1语法 3.JSON数据和Java对象的相互转换 3.1将JSON转换为Java对象 3.2将Java对象转换为JSON 前言 AJAX&#xff1a;ASynchronous JavaScript And XML 异步的JavaScript 和 XML。 Ajax 是一种在…

在安卓里用c++显示骨骼动画

1. 程序模块图 2. 编译第三方库Assimp 2.1 下载 官网下载5.0.0版本,https://codeload.github.com/assimp/assimp/zip/refs/tags/v5.0.0 2.2 生成安卓编译链 解压后在assimp-5.0.0下建文件夹BuildAssimp 放两个脚本make_standalone_toolchain.bat python D:/Android/Sdk/nd…

安达发|各部门实施APS系统前后有哪些变化?

众所周知&#xff0c;生产计划部门是制造企业的重要部门&#xff0c;承担销售、采购、仓储、质量检验和生产的各个部门的协调工作。APS 先进计划排程系统系统通过人工智能算法跟踪所有资源&#xff0c;包括材料、设备、人员、客户需求、订单变更等&#xff0c;自动快速计算出“…

odoo-031 odoo13和odoo16的网站上添加显示变体描述 Website Add Variant Description

文章目录 测试环境需求描述实现步骤实际效果思路说明 测试环境 Odoo 版本&#xff1a; odoo13 和 odoo16 Python 版本&#xff1a;3.6.9 操作系统&#xff1a;Ubuntu 18.04 需求描述 添加变体描述&#xff0c;显示在 form 视图&#xff1b;在网站上动态显示产品变体描述。 …

QT之智能指针

如果没有智能指针&#xff0c;程序员必须保证new对象能在正确的时机delete&#xff0c;四处编写异常捕获代码以释放资源&#xff0c;而智能指针则可以在退出作用域时(不管是正常流程离开或是因异常离开)总调用delete来析构在堆上动态分配的对象。 来看看一个野指针例子 程序将会…

在 3ds Max 中创建逼真的玻璃材质

推荐&#xff1a; NSDT场景编辑器助你快速搭建可二次开发的3D应用场景 尽管本教程基于 3ds Max&#xff0c;但相同的设置适用于许多其他 3D 产品。 注意&#xff1a;单击每个步骤中的缩略图可查看更大的屏幕截图&#xff0c;其中包括视口和用户界面的相关部分。 步骤 1由于本教…

Linux的权限管理精细总结

&#xff08;该图由AI绘制 关注我 学习AI画图&#xff09; 目录 一、权限概述 1、权限的基本概念 2、为什么要设置权限 3、Linux用户身份类别 4、user文件拥有者 5、group文件所属组内用户 6、other其他用户 7、特殊用户root 二、普通权限管理 1、ls -l命令查看文件…

LED显示屏的8个常见信号干扰因素及解决方法

LED显示屏在使用过程中可能会受到多种信号干扰因素的影响&#xff0c;导致显示效果不理想或出现问题。以下是LED显示屏常见的信号干扰因素以及对应的解决方法&#xff1a; 1&#xff0c;电源干扰&#xff1a; 干扰因素&#xff1a;电源波动、电源噪声等。 解决方法&#xff1a…

jenkins发布使用邮件添加审批

首先安装好Email Extension Plugin插件并在 system下配置好邮件 然后配置流水线需要的参数 ![在这里插入图片描述](https://img-blog.csdnimg.cn/418fc89bfa89429783a1eb37d3e4ee26.png#pic_center pipeline如下&#xff1a; def skipRemainingStages false //是否跳过生…

【原创】实现GPT中Transformer模型之框架概念

作者&#xff1a;黑夜路人 时间&#xff1a;2023年7月 GPT是什么意思 GPT的全称是 Generative Pre-trained Transformer&#xff08;生成型预训练变换模型&#xff09;&#xff0c;它是基于大量语料数据上训练&#xff0c;以生成类似于人类自然语言的文本。其名称中的“预训练”…

Knife4j-的使用(详细教程)

文章目录 前言一、简介二、版本参考三、基本使用1. 导入相关依赖2. 比对效果3. 增强特性应用 四、Spring-Cloud 整合1. 项目准备2. 实现步骤2.1 依赖引入2.2 编写配置类2.2.1基础信息配置2.2.2 配置接口信息2.2.3 安全认证配置 2.3 常用注解的使用2.3.1 Api2.3.2 ApiOperation2…

计算机组成2——存储系统

1. 硬件构成、工作原理、评价指标 2.主存储器 基本构成为DRAM&SRAMROM&#xff0c;工作原理为串行访问特点是采用多体交叉提高读取速度 RAM工作原理 SRAM的读写过程如下&#xff1a; WE控制读写&#xff1b; CS为片选信号&#xff1b; VCC为接地端&#xff1b; GND为…

GitLab 16.1创建新成员

GitLab 16.1界面有明显的变化,Admin Area在界面中不容易找到。 下面是找到Admin Area的方法。 本来是可以通过红框的链接找到Admin Area,但是这链接是http://127.0.0.1:8000/root,无法直接访问。 这里有两个方法进入。 方法1 将链接直接改成服务器ip,例如服务器ip是19…

【技术指南】3D转换工具HOOPS Exchange的功能特征和典型使用场景全解析(二)

什么是 HOOPS Exchange&#xff1f;HOOPS Exchange 是一组软件库&#xff0c;可以帮助开发人员在开发应用程序时读取和写入主流的 2D 和 3D 格式。HOOPS Exchange 支持 在主流的3D 文件格式中读取 CAD 数据&#xff0c;并支持将 3D 数据转换为 PRC 数据格式&#xff0c;这是一种…

Godot实用代码-存取存档的程序设计

1. Settings.gd 全局变量 用于保存玩家设置 对应Settings.json 2. Data.gd 全局变量 用于保存玩具数据 对应Data.json 实践逻辑指南 1.在游戏开始的时候&#xff08;游戏场景入口的_ready()处&#xff0c; Settings.gd

ceph对象存储和安装dashborad

一、ceph–RadosGW对象存储 数据不需要放置在目录层次结构中&#xff0c;而是存在于平面地址空间内的同一级别&#xff1b; 应用通过唯一地址来识别每个单独的数据对象; 每个对象可包含有助于检索的元数据&#xff1b; 在Ceph中的对象存储网关中&#xff0c;通过RESTful API在…