Spring Data:“删除者"支持吗? [英] Spring Data: "delete by" is supported?

查看:17
本文介绍了Spring Data:“删除者"支持吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Spring JPA 进行数据库访问.我能够找到诸如 findByName 和 countByName 之类的示例,我不必为此编写任何方法实现.我希望找到根据某些条件删除一组记录的示例.

I am using Spring JPA for database access. I am able to find examples such as findByName and countByName, for which I dont have to write any method implementation. I am hoping to find examples for delete a group of records based on some condition.

Spring JPA 是否支持类似 deleteByName 的删除?任何指针表示赞赏.

Does Spring JPA support deleteByName-like delete? Any pointer is appreciated.

问候和感谢.

推荐答案

不推荐使用的答案(Spring Data JPA <=1.6.x):

@Modifying 注释来救援.不过,您需要提供您的自定义 SQL 行为.

@Modifying annotation to the rescue. You will need to provide your custom SQL behaviour though.

public interface UserRepository extends JpaRepository<User, Long> {
    @Modifying
    @Query("delete from User u where u.firstName = ?1")
    void deleteUsersByFirstName(String firstName);
}

更新:

在 Spring Data JPA (>=1.7.x) 的现代版本中,deleteremovecount 操作的查询派生是可访问的.

In modern versions of Spring Data JPA (>=1.7.x) query derivation for delete, remove and count operations is accessible.

public interface UserRepository extends CrudRepository<User, Long> {

    Long countByFirstName(String firstName);

    Long deleteByFirstName(String firstName);

    List<User> removeByFirstName(String firstName);

}

这篇关于Spring Data:“删除者"支持吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆