lapots
lapots

Reputation: 13395

mybatis using dynamic sql foreach

How to use foreach in mybatis mapper? I mean what parameters I should send there?

For example I've got this select statement

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

I've got interface with method

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

I've got implementation of the interface

@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();
    }
}

And when I make an attempt to run - I've got this error

enter image description here

How to use foreach properly?

Upvotes: 1

Views: 6403

Answers (1)

lapots
lapots

Reputation: 13395

The problem is solved.

I added annotation param

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

And also I sent String representation of indices instead of Integer.

Upvotes: 2

Related Questions