package com.xjrsoft.common.handler; import com.baomidou.mybatisplus.extension.plugins.handler.DataPermissionHandler; import com.xjrsoft.common.annotation.DataPermission; import com.xjrsoft.module.authority.entity.DataAuth; import com.xjrsoft.module.authority.service.IDataAuthService; import net.sf.jsqlparser.expression.Expression; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Component; import java.lang.reflect.Method; /** * @Description 自定义数据权限处理 * @Author: tzx * @Date: 2023/2/6 16:54 */ public class MyDataPermissionHandler implements DataPermissionHandler { @Override public Expression getSqlSegment(Expression where, String mappedStatementId) { try { Class clazz = Class.forName(mappedStatementId.substring(0, mappedStatementId.lastIndexOf("."))); DataPermission mapperAnnotation = clazz.getAnnotation(DataPermission.class); //如果mapper 没有数据权限注解 不需要设置 if(mapperAnnotation == null){ return where; } // String methodName = mappedStatementId.substring(mappedStatementId.lastIndexOf(".") + 1); // Method[] methods = clazz.getDeclaredMethods(); // for (Method method : methods) { // if (!methodName.equals(method.getName())) { // continue; // } // // 获取自定义注解,无此注解则不控制数据权限 // DataPermission annotation = method.getAnnotation(DataPermission.class); // if (annotation == null) { // continue; // } // // } } catch (ClassNotFoundException e) { e.printStackTrace(); } return where; } }