1

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.