0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

MyBatisのSQL文でforeachを回したい

Posted at

Spring BootでMyBatisを使っており、一括でSQLを回したいとき

例えばSELECTやDELETEでIN句を使って一括で値取得したり削除したりしたい場合ありますよね?foreachを使えばよいことは分かったのですが、List<Integer>とかで受けた値をそのままIN句に渡したいとき、微妙に例が見つからなくて困ったので備忘録です。

StudentMapper.kt
      interface StudentMapper {

           @DELETE(
              """
              <script>
               DELETE FROM Student
                 WHERE studentid IN
                 <foreach item="studentids" opem="("close=")" collection="studentids" separator=",">
                    #{studentids}
                 </foreach> 
              </script>
              """
           )
           fun removeStudentsByStudentId(studentids: List<Long>) {}
      }

MyBatisだとxmlでこういった<select></select>タグの中にこういったスクリプトタグで書く方法か、StringでSQL文を投げるかどっちかは見つかるのですが、SQL文の中にforeachを入れたいけど#{something}的な渡し方だとforeachが出来ないっぽかったのでこういったやり方になりました。

一応動きますが、無理やり感があるので、もしもっと良い方法があれば知りたいです。(MyBatisの公式doc読んでも方法分からず)

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?