0
所以我通過讓用戶創建自己的條件來構建我自己的過濾器。直到我需要添加IN
條款爲止,所有內容都很棒。MyBatis過濾器建築不喜歡IN子句
Mapper.xml
<select id="selectResultCount"
parameterType="Filter"
resultType="long">
SELECT COUNT(rank)
FROM ${view}
<where>
<if test="conditions != null">
<foreach collection="conditions"
item="condition"
open=""
separator=" AND "
close="">
<if test="condition.operator.value.toString() == 'IN'">
${condition.lhs} in
<foreach collection="condition.getRhsCondition()" item="rhs" open="(" close=")" separator=",">
'${rhs}'
</foreach>
</if>
${condition.lhs} ${condition.operator.value} #{condition.rhs}
</foreach>
</if>
</where>
</select>
因此,用戶可以建立一個條件列表,然後我撕下來,重建它們。很簡單,例如,他們可以像myColumn = myParam
創造的東西,過濾器會創建SQL查詢Select * From myTable where myColumn = myParam
然而,當用戶使用IN
,這一切都順心:
org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near 'MY_COLUMN'.
### The error may exist in mybatis-mapper.xml
### The error may involve mapper.selectResultCount-Inline
### The error occurred while setting parameters
### SQL: SELECT COUNT(rank) FROM MY_TABLE WHERE MY_COLUMN in ( 'PARAM1' , 'PARAM2' ) MY_COLUMN IN ?
### Cause: com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near 'MY_COLUMN '.
當然,這個例子只是得到結果的數量回來了,但問題仍然存在。爲什麼Mybatis不喜歡IN
,有沒有辦法繞過這個?
感謝您指出了這一點給我。我爲每個查詢創建了單獨的if語句並使其成功工作。 – Kevin