2013-03-23 39 views
2
數據綁定

我想使用<的foreach>但面臨着一些issue.Code如下:無法在MyBatis的

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); 

雖然查詢成功執行,但是當數據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 [[email protected]] 
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

假設您的列表中有豆,一個clientId屬性和getter,你必須當您按項目定義您的bean在foreach時,請將trim(#{clientId})替換爲trim(#{item.clientId})

+0

是的..謝謝!我也應該從異常本身也理解相同的 – abc 2013-03-23 11:37:50