You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MyBatis无参数Mapper方法中when test表达式报错,求修复方案

问题修复方案

报错根源

你遇到的source is null for getProperty(null, "maxRetry")错误,核心原因是:

  • 当前Mapper方法getAllErrorNotifications()没有传入任何参数,MyBatis的OGNL表达式上下文里不存在名为notification的对象;
  • 你混淆了查询参数和返回结果对象:NotificationEmail类中的notification属性是查询返回的结果对象字段,不是查询时可用的参数,OGNL无法在查询阶段访问它。

修复步骤

根据你提到的「SQL中的n.NOT_MAX_RETRY不为空」这个前提,提供两种修复方式:

方式一:直接移除动态判断逻辑

既然已知数据库字段n.NOT_MAX_RETRY不为空,且无需传入参数控制SQL逻辑,直接删除<choose>标签,把原本<when>里的SQL逻辑直接写入主SQL即可。

示例修改后的XML:

<select id="getAllErrorNotifications" resultType="com.yourpackage.NotificationEmail">
  SELECT 
    -- 你的字段映射,注意关联Notification对象的字段
    n.NOT_MAX_RETRY AS "notification.maxRetry",
    -- 其他字段...
  FROM your_table n
  WHERE 
    -- 保留原本的查询条件,加上已知的非空判断
    n.NOT_MAX_RETRY IS NOT NULL
    -- 其他过滤条件...
</select>

方式二:保留动态标签但调整判断逻辑

如果需要保留<choose>结构(比如后续可能扩展参数),但当前无参,可将<when>的test条件改为恒真,直接执行对应分支:

<select id="getAllErrorNotifications" resultType="com.yourpackage.NotificationEmail">
  SELECT ...
  <choose>
    <!-- 无参时直接执行该分支 -->
    <when test="true">
      AND n.NOT_MAX_RETRY IS NOT NULL
    </when>
    <otherwise>
      <!-- 其他备用逻辑 -->
    </otherwise>
  </choose>
</select>

关键注意点

  • MyBatis XML中的test表达式只能访问传入的方法参数,不能访问返回结果类的属性;
  • 如果后续需要重新传入参数,可修改Mapper方法为List<NotificationEmail> getAllErrorNotifications(@Param("notification") Notification notification);,此时XML中的notification.maxRetry != null就能正常工作。

内容的提问来源于stack exchange,提问作者LiefLayer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 13:26:21