1

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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.