Sooraj
Sooraj

Reputation: 83

MyBatis dynamically including script

I am trying to dynamically include select query based on some input parameters. The following is what I am trying to achieve.

<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>

I am planning to pass the sql id name as parameter to the query and trying to dynamically choose the select query based on this param. (Sort of like a factory design implementation). However #{readerIdName} is not getting replaced with the value I am passing as param. Is something like this possible?

Upvotes: 1

Views: 780

Answers (1)

Dariusz
Dariusz

Reputation: 22251

The xml attributes at this level are not evaluated in runtime by mybatis.

You can use a single <SQL id="..."> query and an <if test="..."> dynamic sql in it.

Upvotes: 1

Related Questions