2017-04-10 24 views
0

我有一个共同的sql标签,应根据它包含在哪里动态更改。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: 

回答

0

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