sql默认添加is_deleted删除字段

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:使用

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

  • 13
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值