二十一、Spring boot中Jpa持久化监听器,拦截增删改查

  在JPA中,我们使用Java Persistence API进行数据的持久化(增删改查),相应的该API也提供了监听数据持久化生命周期中的回调方法,主要由以下几个注解来实现:
* @PrePersist 保存前
* @PostPersist 保存后
* @PreRemove 删除前
* @PostRemove 删除后
* @PreUpdate 更新前
* @PostUpdate 更新后
* @PostLoad 查询后
  这些注解必须配合@EntityListeners注解来使用,@EntityListeners注解就是实体监听器注解。
  我们以监听保存Customer对象为例:
第一步:创建监听器类CustomerListener

public class CustomerListener {
    /**
     * 在保存之前调用
     */
    @PrePersist
    public void prePersist(Object source){
        System.out.println("@PrePersist:" + source);
    }
    /**
     * 在保存之后调用
     */
    @PostPersist
    public void postPersist(Object source){
        System.out.println("@PostPersist:" + source);
    }
}


第二步:在Customer类上加@EntityListeners注解

@EntityListeners(value = {CustomerListener.class})
public class Customer {
}

  通过以上,即可拦截数据持久化

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,以下是 Spring Boot JPA 实现一对多增删的示例代码: 1. 创建实体类 ``` @Entity public class Parent { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, orphanRemoval = true) private List<Child> children = new ArrayList<>(); // getters and setters } @Entity public class Child { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "parent_id") private Parent parent; // getters and setters } ``` 2. 创建 Repository 接口 ``` public interface ParentRepository extends JpaRepository<Parent, Long> { } ``` 3. 创建 Service 类 ``` @Service public class ParentService { @Autowired private ParentRepository parentRepository; public Parent save(Parent parent) { return parentRepository.save(parent); } public void delete(Long id) { parentRepository.deleteById(id); } public Parent findById(Long id) { return parentRepository.findById(id).orElse(null); } public List<Parent> findAll() { return parentRepository.findAll(); } } ``` 4. 创建 Controller 类 ``` @RestController @RequestMapping("/parents") public class ParentController { @Autowired private ParentService parentService; @PostMapping public Parent create(@RequestBody Parent parent) { return parentService.save(parent); } @PutMapping("/{id}") public Parent update(@PathVariable Long id, @RequestBody Parent parent) { Parent existingParent = parentService.findById(id); if (existingParent == null) { throw new RuntimeException("Parent not found"); } existingParent.setName(parent.getName()); existingParent.setChildren(parent.getChildren()); return parentService.save(existingParent); } @DeleteMapping("/{id}") public void delete(@PathVariable Long id) { parentService.delete(id); } @GetMapping("/{id}") public Parent findById(@PathVariable Long id) { return parentService.findById(id); } @GetMapping public List<Parent> findAll() { return parentService.findAll(); } } ``` 以上就是 Spring Boot JPA 实现一对多增删的示例代码。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值