我正在尝试使用< foreach>但面临一些问题。代码如下:
Bean.java
private class Bean{
private String clientId;
//getter and setter
}
Dao.xml
<mapper namespace="...Dao">
<resultMap id="bean" type="...Bean">
<result property="clientId" column="CLIENT_ID" />
</resultMap>
<select id="getData" resultMap="bean"
parameterType="someBean">
SELECT *
FROM tableClient
WHERE CLIENT_ID IN
<foreach item="item" index="index" collection="list"
open="(" separator="," close=")">
trim(#{clientId})
</foreach>
</select>
</mapper>
查询打算在以下位置返回数据:
List<Bean> getData(List<Bean> bean);
虽然查询正在成功执行,但是当数据绑定时会引发异常.Stacktrace:
2013-03-23 15:11:13,637 DEBUG [getData] ==> Preparing: SELECT * FROM tableClient WHERE CLIENT_ID IN ( trim(?) , trim(?) , trim(?) )
2013-03-23 15:11:13,637 DEBUG [SqlSessionUtils] Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@117c0eb]
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter 'clientId' not found. Available parameters are [list]
2013-03-23 15:11:13,637 DEBUG [DataSourceUtils] Returning JDBC Connection to DataSource
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:73)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:365)
at $Proxy11.selectList(Unknown Source
....
答案 0 :(得分:0)
假设您的列表中有一个带有clientId属性和getter的bean,您必须将trim(#{clientId})
替换为trim(#{item.clientId})
,因为您在foreach
上按项目定义了bean。