2

我正在尝试使用 <foreach> 但遇到一些问题。代码如下:

Bean.java

private class Bean{
 private String clientId;
//getter and setter 
}

道.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
....
4

1 回答 1

0

假设在您的列表中有具有 clientId 属性和 getter 的 bean,您必须替换trim(#{clientId})trim(#{item.clientId})您在foreachby 项目上定义的 bean。

于 2013-03-23T11:07:54.167 回答