利用mybatis 拦截器实现数据权限

0.注意事项

        a.代码可能不全,大致读一下逻辑即可实现

        b.重点就是拦截器sql拼接部分,其他可按不同场景个性化
        c.如果拦截器不起作用可参考6服务启动时操作或参考下方配置拦截器

@Configuration
@MapperScan("com.rtzh.ejjzhgd.web.**.mapper")
@EnableConfigurationProperties(value = DataScopeProperties.class)
public class MybatisPlusConfig {
 
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        interceptor.addInnerInterceptor(new QueryParameterInnerInterceptor());
        return interceptor;
    }

    @Bean
    @ConfigurationProperties(prefix = "pagehelper")
    public Properties pageHelperProperties() {
        return new Properties();
    }
}

        


1.使用方法


        查询前执行该方法即可 参数1:字段名,参数2:自定义id可传null,在拦截器更具用户查询
        该方法就是为了往ThreadLocal set数据
        DataScopeHelper.start("office_id", filterIds); 

  示例:

@Override
public Object test() {

    LambdaQueryWrapper<RtPro> lambdaQuery = new LambdaQueryWrapper();
    lambdaQuery.ne(RtPro::getProId,"1");
    lambdaQuery.last("limit 10");
    DataScopeHelper.start("dept_id", new HashSet<>(Arrays.asList("id")));
    return  rtProMapper.selectList(lambdaQuery);
}
2.线程缓存


public class DataScopeHelper {
    private static final ThreadLocal<DataScope> DATA_PERMISSION = new ThreadLocal();

    public DataScopeHelper() {
    }

    public static void start(DataScope dataScope) {
        DATA_PERMISSION.set(dataScope);
    }

    public static void start(String scopeName, Set<String> deptIds) {
        DataScope dataScope = new DataScope();
        dataScope.setScopeName(scopeName);
        dataScope.setDeptIds(deptIds);
        DATA_PERMISSION.set(dataScope);
    }

    public static void startDataScope(String scopeName, String... deptIds) {
        Object idSet;
        if (deptIds != null && deptIds.length > 0) {
            idSet = (Set)Arrays.stream(deptIds).filter(StringUtils::isNotEmpty).collect(Collectors.toSet());
        } else {
            idSet = new HashSet();
        }
        start(scopeName, (Set)idSet);
    }

    public static DataScope getLocalDataPermissions() {
        return (DataScope)DATA_PERMISSION.get();
    }

    public static void clearDataPermissions() {
        DATA_PERMISSION.remove();
    }

  • 9
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值