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

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

问题描述

我正在使用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);

}

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

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