在使用ibatis isEmpty 和 iterate 嵌套查询时,查询语句如下:
<select id="findUserId" resultClass="int"
parameterClass="map">
SELECT UserID
FROM DP_SubscriptionList
<dynamic prepend="WHERE">
SubType=#type#
<isNotEmpty prepend="AND" property="cityIdList">
CityID IN
<iterate property="cityIdList" open="(" close=")" conjunction=",">
#cityIdList[]#
</iterate>
</isNotEmpty>
</dynamic>
</select>
报错:
--- Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CityID IN ( 1 , 2 )' at line
1]]
修改以后,不再报错:
<select id="findUserId" resultClass="int"
parameterClass="map">SELECT UserID
FROM DP_SubscriptionList
<dynamic prepend="WHERE">
SubType=#type#
<isNotEmpty property="cityIdList">
AND CityID IN
<iterate property="cityIdList" open="(" close=")" conjunction=",">
#cityIdList[]#
</iterate>
</isNotEmpty>
</dynamic></select>
主要是“AND”的位置,如果不注意的话就会运行不成功。