简体   繁体   中英

Unable to bind data in Mybatis

I am trying to use <foreach> but facing some issue.Code is as follows:

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>

The query intend to return data in:

    List<Bean> getData(List<Bean> bean);

Although the query is executing successfully but the exception is raised when the data is binded.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
....

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM