赞
踩
1:添加MybatisConfig配置
- //默认给所有的用户添加is_deleted字段
- interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(new TenantLineHandler() {
- //字段类型
- @Override
- public Expression getTenantId() {
- return new LongValue(0);
- }
- @Override
- public String getTenantIdColumn() {
- return "is_deleted";
- }
- @Override
- public boolean ignoreTable(String tableName) {
- if (ObjectUtils.isNotEmpty(MybatisDeleteContext.getDeleteContextThreadLocal()) && Objects.nonNull(MybatisDeleteContext.get())){
- return MybatisDeleteContext.get();
- }
- return false;
- }
- }));
2:这个添加后会在所有的sql上加上is_deleted = 0;
3:有些sql不需要这样做可以写一个注解加拦截器去掉这个字段。
- package com.chaunve.cloud.common.annotations;
-
- import java.lang.annotation.ElementType;
- import java.lang.annotation.Retention;
- import java.lang.annotation.RetentionPolicy;
- import java.lang.annotation.Target;
-
- /**
- * @author dxl
- * Date 2023/11/15 16:10
- * Description:
- */
- @Retention(RetentionPolicy.RUNTIME)
- @Target({ElementType.METHOD, ElementType.TYPE})
- public @interface IgnoreDelete {
-
- /**
- * true为不做删除隔离 false为做删除隔离
- * @return
- */
- boolean isIgnore() default true;
- }
-
- package com.chaunve.cloud.aspect;
-
- import com.chaunve.cloud.common.annotations.IgnoreDelete;
- import com.chaunve.cloud.common.context.MybatisDeleteContext;
- import lombok.extern.slf4j.Slf4j;
- import org.aspectj.lang.ProceedingJoinPoint;
- import org.aspectj.lang.annotation.Around;
- import org.aspectj.lang.annotation.Aspect;
- import org.aspectj.lang.annotation.Pointcut;
- import org.aspectj.lang.reflect.MethodSignature;
- import org.springframework.core.annotation.AnnotationUtils;
- import org.springframework.stereotype.Component;
-
- import java.lang.reflect.Method;
- import java.util.Objects;
-
- /**
- * @author dxl
- * Date 2023/11/15 16:11
- * Description:
- */
- @Aspect
- @Slf4j
- @Component
- public class DeleteIgnoreAspect {
- /**
- * 切入点
- */
- @Pointcut("@within(com.chaunve.cloud.common.annotations.IgnoreDelete) ||@annotation(com.chaunve.cloud.common.annotations.IgnoreDelete)")
- public void pointcut() {
- }
-
- @Around("pointcut()")
- public Object around(ProceedingJoinPoint point) throws Throwable {
- try {
- Class<?> targetClass = point.getTarget().getClass();
- IgnoreDelete classIgnoreTenant = targetClass.getAnnotation(IgnoreDelete.class);
- MethodSignature signature = (MethodSignature) point.getSignature();
- Method method = signature.getMethod();
- IgnoreDelete methodIgnoreTenant = method.getAnnotation(IgnoreDelete.class);
-
- //判断类上是否有注解
- boolean isClassAnnotated = AnnotationUtils.isAnnotationDeclaredLocally(IgnoreDelete.class, targetClass);
- //判断方法上是否有注解
- boolean isMethodAnnotated = Objects.nonNull(methodIgnoreTenant);
-
- //如果类上有
- if (isClassAnnotated) {
- MybatisDeleteContext.set(classIgnoreTenant.isIgnore());
- }
- //如果方法上有 以方法上的为主
- if (isMethodAnnotated) {
- MybatisDeleteContext.set(methodIgnoreTenant.isIgnore());
- }
- return point.proceed();
- } finally {
- MybatisDeleteContext.clear();
- }
- }
- }
-
4:使用
如此批量添加+个别不用都实现了
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。