【问题标题】:MyBatis dynamically including scriptMyBatis 动态包含脚本
【发布时间】:2022-01-03 09:51:05
【问题描述】:

我正在尝试根据一些输入参数动态包含选择查询。 以下是我想要实现的目标。

<sql id="query1">
  SELECT * from 
    table_1
   WHERE a = #{param1}
</sql>

<sql id="query2">
  SELECT * from 
    table_2
   WHERE b = #{param2}
</sql>

<select id = "selectSomethingFromDB">
  <include refid="#{readerIdName}" />
</select>

我打算将 sql id 名称作为参数传递给查询,并尝试根据此参数动态选择选择查询。 (有点像工厂设计实现)。 但是 #{readerIdName} 不会被我作为参数传递的值替换。 这样的事情可能吗?

【问题讨论】:

    标签: mybatis spring-mybatis mybatis-sql


    【解决方案1】:

    mybatis 不会在运行时评估此级别的 xml 属性。

    您可以在其中使用单个 &lt;SQL id="..."&gt; 查询和一个 &lt;if test="..."&gt; dynamic sql

    【讨论】:

      猜你喜欢
      • 2010-11-04
      • 1970-01-01
      • 1970-01-01
      • 2019-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多