找回密码
 立即注册
首页 业界区 安全 springboot~jpa优雅的软删除能力

springboot~jpa优雅的软删除能力

胆饬 2 小时前
之前写过关于springboot~jpa优雅的处理isDelete的默认值的文章,今天说一下在jpa或者其它类型的Repository中实现软删除的方法,主要借助了自定义的仓储的能力。
优雅的引用方式
  1. /**
  2. * 开启软删除的能力
  3. *
  4. * @author lind
  5. * @date 2025/9/8 11:24
  6. * @since 1.0.0
  7. */
  8. @Target(ElementType.TYPE)
  9. @Retention(RetentionPolicy.RUNTIME)
  10. @EnableJpaRepositories(repositoryBaseClass = SoftDeleteRepositoryImpl.class)
  11. public @interface EnableSoftDeleteRepository {
  12. }
复制代码
接口标准化
  1. /**
  2. * 软删除能力,通过@EnableSoftDeleteRepository注解开启功能,通过接口继承的方式实现这个能力
  3. *
  4. * @param <T>
  5. * @param <ID>
  6. */
  7. @NoRepositoryBean // 不让jpa使用代理建立实现类
  8. public interface SoftDeleteRepository<T, ID> {
  9.         T getEntityById(ID id);
  10.         /**
  11.          * 希望重写findById方法
  12.          * @param id
  13.          * @return
  14.          */
  15.         T getById(ID id);
  16. }
复制代码
覆盖默认的deleteById方法,实现软删除
  1. /**
  2. * 自定义的公共仓储的实现
  3. *
  4. * @param <T>
  5. * @param <ID>
  6. */
  7. public class SoftDeleteRepositoryImpl<T, ID> extends SimpleJpaRepository<T, ID> implements SoftDeleteRepository<T, ID> {
  8.         private final EntityManager entityManager;
  9.         private final JpaEntityInformation<T, ID> jpaEntityInformation;
  10.         Class<T> domainType;
  11.         Logger logger = LoggerFactory.getLogger(SoftDeleteRepositoryImpl.class);
  12.         public SoftDeleteRepositoryImpl(JpaEntityInformation<T, ID> jpaEntityInformation, EntityManager entityManager) {
  13.                 super(jpaEntityInformation, entityManager); // 必须调用父类构造函数
  14.                 this.entityManager = entityManager;
  15.                 this.jpaEntityInformation = jpaEntityInformation;
  16.                 this.domainType = jpaEntityInformation.getJavaType();
  17.         }
  18.         @Override
  19.         public T getEntityById(ID id) {
  20.                 return entityManager.find(this.domainType, id);
  21.         }
  22.         /**
  23.          * @param id
  24.          * @deprecated
  25.          */
  26.         @Override
  27.         public void deleteById(ID id) {
  28.                 logger.info("CustomRepositoryImpl.getById " + id);
  29.                 T entity = getEntityById(id);
  30.                 if (entity != null && entity instanceof DeletedFlagField) {
  31.                         ((DeletedFlagField) entity).setDeletedFlag(1);
  32.                         entityManager.merge(entity);
  33.                 }
  34.                 else {
  35.                         super.deleteById(id);
  36.                 }
  37.         }
  38. }
复制代码
需要实现软删除的仓库接口上,继承这个接口即有这个软删除的能力
  1. /**
  2. * 用户仓储
  3. *
  4. * @author lind
  5. * @date 2025/7/15 15:56
  6. * @since 1.0.0
  7. */
  8. public interface UserEntityRepository extends SoftDeleteRepository<UserEntity, String>,
  9.                 JpaRepository<UserEntity, String>, JpaSpecificationExecutor<UserEntity> {
  10. }
复制代码
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

您需要登录后才可以回帖 登录 | 立即注册