How do you handle with bulk deleting by an array of IDs in Spring Data JPA?

前端 未结 4 825
星月不相逢
星月不相逢 2021-02-04 05:15

Now I have a class User, I get a request data of an array from the jsp or html.

list this Integer[] arr=[5,6,9,10,62,52,21]

4条回答
  •  时光说笑
    2021-02-04 05:27

    Just add the following to your user repository interface

    void deleteByIdIn(List ids);
    

    Spring will automatically generate the appropriate query via method name derivation.

    https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods

    EDIT: A litte more detail on this

    Using Springs Repository interfaces like CrudRepository, JpaRespository brings the basic set of database operations, like create, read, update, delete, paging, sorting and so on.

    To manually add some simple queries like searching for a users name or mail address spring provides a fine mechnanism without annotating any string based HQL queries or similar.

    Spring just analyses your method names, searching for keywords. Read the documentation link above which keywords are provided.

    Example methods for a CrudRepository:

    Iterable findByNameLike(String search) resolves to select * from user where name like ''

    void deleteByIdIn(List ids) resolves to delete from user where id in ([ids])

提交回复
热议问题