如何检查 myBatis 映射器 XML 中的参数是否存在?

发布于 2025-01-15 02:02:02 字数 909 浏览 5 评论 0原文

我将我的问题简化为以下代码。

Mapper 接口如下所示:

@Mapper
public interface TestMapper {
 public Integer countTable(@Param("tValue1") Integer tValue1);
 public Integer maxTable(@Param("tValue2") Integer tValue2);
}

Mapper Xml 如下所示:

<sql id="sql_where">
   <where>
      <if test="tValue1 != null">tValue1 = #{tValue1}</if>
      <if test="tValue2 != null">AND tValue2 = #{tValue2}</if>
   <where>
</sql>

<select id="countTable" resultType="Integer">
  SELECT COUNT(*) FROM TestTable
  <include refId="sql_where"/>
</select>

<select id="maxTable" resultType="Integer">
  SELECT MAX(*) FROM TestTable
  <include refId="sql_where"/>
</select>

当调用 countTable 或 maxTable 方法时,发生 BindingException。 由于未找到参数之一。

如何检查 XML 中是否存在该参数? 我可以将所有参数插入到方法中,但我不希望程序员感到困惑(它们不会被使用)

I simplifed my problem as following code.

Mapper Interface looks like:

@Mapper
public interface TestMapper {
 public Integer countTable(@Param("tValue1") Integer tValue1);
 public Integer maxTable(@Param("tValue2") Integer tValue2);
}

Mapper Xml looks like:

<sql id="sql_where">
   <where>
      <if test="tValue1 != null">tValue1 = #{tValue1}</if>
      <if test="tValue2 != null">AND tValue2 = #{tValue2}</if>
   <where>
</sql>

<select id="countTable" resultType="Integer">
  SELECT COUNT(*) FROM TestTable
  <include refId="sql_where"/>
</select>

<select id="maxTable" resultType="Integer">
  SELECT MAX(*) FROM TestTable
  <include refId="sql_where"/>
</select>

when countTable or maxTable method is called, BindingException occurs.
Due to one of parameter is not found.

How can I check the parameter exists in XML?
I could've insert all params to method, but I don't want programmers to be confused (they're not going to be used)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文