当前位置:   article > 正文

sql默认添加is_deleted删除字段_deleted sql

deleted sql

1:添加MybatisConfig配置

  1. //默认给所有的用户添加is_deleted字段
  2. interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(new TenantLineHandler() {
  3. //字段类型
  4. @Override
  5. public Expression getTenantId() {
  6. return new LongValue(0);
  7. }
  8. @Override
  9. public String getTenantIdColumn() {
  10. return "is_deleted";
  11. }
  12. @Override
  13. public boolean ignoreTable(String tableName) {
  14. if (ObjectUtils.isNotEmpty(MybatisDeleteContext.getDeleteContextThreadLocal()) && Objects.nonNull(MybatisDeleteContext.get())){
  15. return MybatisDeleteContext.get();
  16. }
  17. return false;
  18. }
  19. }));

2:这个添加后会在所有的sql上加上is_deleted = 0;

3:有些sql不需要这样做可以写一个注解加拦截器去掉这个字段。

  1. package com.chaunve.cloud.common.annotations;
  2. import java.lang.annotation.ElementType;
  3. import java.lang.annotation.Retention;
  4. import java.lang.annotation.RetentionPolicy;
  5. import java.lang.annotation.Target;
  6. /**
  7. * @author dxl
  8. * Date 2023/11/15 16:10
  9. * Description:
  10. */
  11. @Retention(RetentionPolicy.RUNTIME)
  12. @Target({ElementType.METHOD, ElementType.TYPE})
  13. public @interface IgnoreDelete {
  14. /**
  15. * true为不做删除隔离 false为做删除隔离
  16. * @return
  17. */
  18. boolean isIgnore() default true;
  19. }
  1. package com.chaunve.cloud.aspect;
  2. import com.chaunve.cloud.common.annotations.IgnoreDelete;
  3. import com.chaunve.cloud.common.context.MybatisDeleteContext;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.aspectj.lang.ProceedingJoinPoint;
  6. import org.aspectj.lang.annotation.Around;
  7. import org.aspectj.lang.annotation.Aspect;
  8. import org.aspectj.lang.annotation.Pointcut;
  9. import org.aspectj.lang.reflect.MethodSignature;
  10. import org.springframework.core.annotation.AnnotationUtils;
  11. import org.springframework.stereotype.Component;
  12. import java.lang.reflect.Method;
  13. import java.util.Objects;
  14. /**
  15. * @author dxl
  16. * Date 2023/11/15 16:11
  17. * Description:
  18. */
  19. @Aspect
  20. @Slf4j
  21. @Component
  22. public class DeleteIgnoreAspect {
  23. /**
  24. * 切入点
  25. */
  26. @Pointcut("@within(com.chaunve.cloud.common.annotations.IgnoreDelete) ||@annotation(com.chaunve.cloud.common.annotations.IgnoreDelete)")
  27. public void pointcut() {
  28. }
  29. @Around("pointcut()")
  30. public Object around(ProceedingJoinPoint point) throws Throwable {
  31. try {
  32. Class<?> targetClass = point.getTarget().getClass();
  33. IgnoreDelete classIgnoreTenant = targetClass.getAnnotation(IgnoreDelete.class);
  34. MethodSignature signature = (MethodSignature) point.getSignature();
  35. Method method = signature.getMethod();
  36. IgnoreDelete methodIgnoreTenant = method.getAnnotation(IgnoreDelete.class);
  37. //判断类上是否有注解
  38. boolean isClassAnnotated = AnnotationUtils.isAnnotationDeclaredLocally(IgnoreDelete.class, targetClass);
  39. //判断方法上是否有注解
  40. boolean isMethodAnnotated = Objects.nonNull(methodIgnoreTenant);
  41. //如果类上有
  42. if (isClassAnnotated) {
  43. MybatisDeleteContext.set(classIgnoreTenant.isIgnore());
  44. }
  45. //如果方法上有 以方法上的为主
  46. if (isMethodAnnotated) {
  47. MybatisDeleteContext.set(methodIgnoreTenant.isIgnore());
  48. }
  49. return point.proceed();
  50. } finally {
  51. MybatisDeleteContext.clear();
  52. }
  53. }
  54. }

4:使用

如此批量添加+个别不用都实现了

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/409395
推荐阅读
相关标签
  

闽ICP备14008679号