使用包含属性的MyBatis动态SQL

时间:2017-04-10 06:47:15

标签: java mybatis spring-mybatis

我有一个常见的sql标记,应根据其包含的位置动态更改。

 <sql id="common">
        from customer_order as co inner join status as st on st.id = co.status_id
        left join status as sst on sst.id = co.sub_status_id
        inner join customer as c on c.id = co.customer_id
        inner join user as u on u.id = co.created_by
        left join customer_account as acc on acc.id = c.account_type_id
        left join customer_type as typ on typ.id = c.party_type
        left join sale_channel as ch on ch.id = co.saleschannel_id
       <choose>
        <when test="${ordernotes} eq 'true'">
            inner join order_note as ordn on ordn.order_id = co.id 
        </when>
        <when test="${vendorordernotes} eq 'true'">
           ... join with other tables
        </when>
        </choose>
</sql>

这就是我在上面包含sql的方法。

  <select id="x" resultType="long">
        select count(distinct ordn.id) 
        <include refid="common">
            <property name="ordernotes" value="true"/>
        </include>
    </select>

<select id="y" resultType="long">
        select count(distinct von.id)
        <include refid="common">
            <property name="vendorordernotes" value="true"/>
        </include>
 </select>

我无法让''代码工作。我该怎么做呢。

例外:

Was expecting one of:
    ":" ...
    "not" ...
    "+" ...
    "-" ...
    "~" ...
    "!" ...
    "(" ...
    "true" ...
    "false" ...
    "null" ...
    "#this" ...
    "#root" ...
    "#" ...
    "[" ...
    "{" ...
    "@" ...
    "new" ...
    <IDENT> ...
    <DYNAMIC_SUBSCRIPT> ...
    "\'" ...
    "`" ...
    "\"" ...
    <INT_LITERAL> ...
    <FLT_LITERAL> ...
    ] 
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.builder.BuilderException: Error evaluating expression '${ordernotes} eq 'true''. Cause: org.apache.ibatis.ognl.ExpressionSyntaxException: Malformed OGNL expression: ${ordernotes} eq 'true' [org.apache.ibatis.ognl.ParseException: Encountered " "$" "$ "" at line 1, column 1.
Was expecting one of:

1 个答案:

答案 0 :(得分:0)

参数不能很好地解释。 带引号的环绕参数:<when test="'${ordernotes}' eq 'true'">,它变成一个简单的字符串比较。