4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【java】MyBatisでパラメータにDTOを使用する

4
Posted at

やりたいこと

  1. mapperメソッドのパラメータにListを渡す
  2. SQL内でプレスホルダーとして使用する

やったこと

mapper.java
public interface Mapper {
    int updateUsers(@Param("params")List<UpdateUser> params);
}
UpdateUser.java
@Data
public class UpdateUser {
    private int id;
    private String name;
    private String address;
}
mapper.xml
<update id="updateUsers">
    UPDATE
        users
    SET
        name = CASE id
            <foreach collection="params" item="param">
                WHEN #{param.id} THEN #{param.name}
            </foreach>
        END,
        address = CASE id
            <foreach collection="params" item="param">
                WHEN #{param.id} THEN #{param.address}
            </foreach>
        END
    WHERE
        id IN
        <foreach collection="params" item="param" open="(" separator="," close=")">
            #{param.id}
        </foreach>
</update>
4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?