0

I want to use a common query template to do filtering in myBatis-3. My template look something like this:

<sql id="filter">
    SELECT * FROM (${subquery}) LIMIT ${page}, 20
</sql>

The subquery placeholder can accept any query. I tried to pass the subquery as a parameter. But unfortunately this does not work, the subquery is empty. I wonder it’s possible the property’s value accept dynamic value?

<select
    id="find"
    parameterType="some.page"
    resultMap="map">
    <include refid="some.namespace.filter">
        <property name="subquery" value="${subquery}"/>
        <property name="page" value="#{page}"/>
    </include>
</select>

<sql id="subquery">
    SELECT * FROM test_table ORDER BY id
</sql>
user3502676
  • 317
  • 3
  • 7

1 Answers1

0

Why don't you just change another way to do this? I don't think the feature you've said is supported by MyBatis, maybe you can do it like following;

<select
    id="find"
    parameterType="some.page"
    resultMap="map">
    SELECT * FROM (<include refid="subquery">) t LIMIT ${page}, 20
</select>

<sql id="subquery">
    SELECT * FROM test_table ORDER BY id
</sql>
Blank
  • 12,308
  • 1
  • 14
  • 32
  • Ya, I know this way can achieve the same result. I wanted to use a common template because I don't want to keep repeating this `SELECT * FROM (${subquery}) LIMIT ${page}, 20` – user3502676 Jul 04 '16 at 05:43
  • @user3502676 With my limited knowledge, `Mybatis` does not support the feature you mentioned as so far; and even if `Mybatis` support this, you're no need to repeat this quite simple query `SELECT * FROM (${subquery}) LIMIT ${page}, 20`, but you have to repeat this not too short inlclude``, doesn't it? – Blank Jul 04 '16 at 06:05