如何在Mybatis映射器中使用foreach
?我的意思是我应该发送什么参数?
例如,我有此选择语句
<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();
}
}
当我尝试运行时-我遇到了这个错误
如何正确使用
foreach
? 最佳答案
问题已经解决了。
我添加了注释param
List<SimpleObject> findObjectsWithIds(@Param("list") List<Integer> ids);
我还发送了
String
索引的表示形式,而不是Integer
。