2014-05-20 21 views
1

如何在mybatis映射器中使用foreach?我的意思是我应该发送什么参数?mybatis使用动态sql的foreach

比如我有这个select语句

<select id="findObjectsWithIds" resultMap="SimpleObjectResult"> 
    SELECT * FROM testschema."XSimpleTable" 
    WHERE ID in 
    <foreach item="item" index="index" collection="list" 
     open="(" separator="," close=")"> 
      #{item} 
    </foreach> 
</select> 

我有接口方法

List<SimpleObject> findObjectsWithIds(List<String> ids); 

我有接口的实现

@Override 
public List<SimpleObject> findObjectsWithIds(List<String> ids) { 
    SqlSession sqlSession = MyBatisSqlSessionFactory.openSession(); 
    try { 
     SimpleMapper simpleMapper = sqlSession.getMapper(SimpleMapper.class); 
     return simpleMapper.findObjectsWithIds(ids); 
    } finally { 
     sqlSession.close(); 
    } 
} 

而且当我试图运行 - 我有这个错误

enter image description here

如何正确使用foreach

回答

2

问题解决了。

我加了注释param

List<SimpleObject> findObjectsWithIds(@Param("list") List<Integer> ids); 

而且我也发指数,而不是IntegerString表示。