1通过DataScopePermissionHandler  改写

@Aspect
@Slf4j
@Component
public class DataScopePermissionHandler implements DataPermissionHandler {/*** 通过ThreadLocal记录权限相关的属性值*/ThreadLocal<DataScopeParam> threadLocal = new ThreadLocal<>();/*** 清空当前线程上次保存的权限信息*/@After("dataScopePointCut()")public void clearThreadLocal(){threadLocal.remove();log.debug("threadLocal.remove()");}/*** 注解对象*/private DataScope controllerDataScope;/*** 配置织入点*/@Pointcut("@annotation(com.xxx.base.datascope.annotation.DataScope)")public void dataScopePointCut() {}@Before("dataScopePointCut()")public void doBefore(JoinPoint point) {// 获得注解controllerDataScope = getAnnotationLog(point);if (controllerDataScope != null) {// 获取当前的用户及相关属性,需提前获取和保存数据权限对应的部门ID集合User currentUser = SecurityUtil.getUser();DataScopeParam dataScopeParam = new DataScopeParam(controllerDataScope.deptAlias(),controllerDataScope.deptField(),currentUser.isAdmin(),currentUser.getDataScope());threadLocal.set(dataScopeParam);log.debug("currentUser.getDataScope() = {}", currentUser.getDataScope());}}/*** 是否存在注解,如果存在就获取*/private DataScope getAnnotationLog(JoinPoint joinPoint) {Signature signature = joinPoint.getSignature();MethodSignature methodSignature = (MethodSignature) signature;Method method = methodSignature.getMethod();if (method != null) {return method.getAnnotation(DataScope.class);}return null;}/*** @param where             原SQL Where 条件表达式* @param mappedStatementId Mapper接口方法ID* @return*/@SneakyThrows@Overridepublic Expression getSqlSegment(Expression where, String mappedStatementId) {log.debug("DataScopePermissionHandler .getSqlSegment");DataScopeParam dataScopeParam = threadLocal.get();if(controllerDataScope == null || dataScopeParam == null || dataScopeParam.isAdmin()){return where;}if (where == null) {where = new HexValue(" 1 = 1 ");}String deptSql = "".equals(dataScopeParam.deptAlias) ? dataScopeParam.deptField : dataScopeParam.deptAlias + "." + dataScopeParam.deptField;// 把集合转变为JSQLParser需要的元素列表ItemsList itemsList;if(CollectionUtils.isEmpty(dataScopeParam.secretary)){//如果权限为空,则只能看自己部门的itemsList = new ExpressionList(Collections.singletonList(new LongValue(SecurityUtil.getUser().getOrganizeId())));}else {//查看权限内的数据itemsList = new ExpressionList(dataScopeParam.secretary.stream().map(LongValue::new).collect(Collectors.toList()));}InExpression inExpression = new InExpression(new Column(deptSql), itemsList);log.debug("where = {}", where);log.debug("inExpression = {}", inExpression);return new AndExpression(where, inExpression);}/*** ThreadLocal存储对象*/@Data@AllArgsConstructorstatic class DataScopeParam{/*** 部门表的别名*/private String deptAlias;/*** 部门字段名*/private String deptField;/*** 是否是管理员*/private boolean isAdmin;/*** 数据权限范围*/private Set<Integer> secretary;}
}

2

mybatisplus sql 改写2相关推荐

  1. 优化案例 | CASE WHEN进行SQL改写优化

    导读 今天给大家分享一个通过SQL改写而独辟蹊径的SQL优化案例 待优化场景 发现SLOW QUERY LOG中有下面这样一条记录: ... # Query_time: 59.503827 Lock_ ...

  2. 十八般武艺玩转GaussDB(DWS)性能调优:SQL改写

    摘要:本文将系统介绍在GaussDB(DWS)系统中影响性能的坏味道SQL及SQL模式,帮助大家能够从原理层面尽快识别这些坏味道SQL,在调优过程中及时发现问题,进行整改. 数据库的应用中,充斥着坏味 ...

  3. Sharding-JDBC 源码之 SQL 改写

    Sharding-JDBC 系列 第一篇 Sharding-JDBC 源码之启动流程分析 第二篇 Sharding-JDBC 源码之 SQL 解析 第三篇 Sharding-JDBC 源码之 SQL ...

  4. mybatis sql 改写

    1mybatis sql 改写 package com.macro.mall.tiny.config;import lombok.extern.slf4j.Slf4j; import org.apac ...

  5. sharding-jdbc之SQL改写

    [引用官网]在包含分表的场景中,需要将分表配置中的逻辑表名称改写为路由之后所获取的真实表名称.仅分库则不需要表名称的改写.除此之外,还包括补列和分页信息修正等内容,如图: 本文主要以SELECT i. ...

  6. SQL做的能改成Oracle吗,从SQL改写到SQL重写,什么样的SQL才是好SQL?(黄浩)

    从SQL改写到SQL重写,什么样的SQL才是好SQL?黄浩 2016-12-14 10:02:26 作者介绍 黄浩,现任职于中国惠普,从业十年,始终专注于SQL.十年一剑,十年磨砺.3年通信行业,写就 ...

  7. SQL改写的书终于出版了

    众所周知,SQL 优化很多 SQL 都是写法上有问题,目前市面上没有这种书籍,为了造福广大Oracle使用者 教主和我特意编写 SQL 改写 优化的书籍 ,大家可以到这里购买 https://item ...

  8. Mybatis-plus sql注入以及防止sql注入

    Mybatis-plus sql注入 以及防止sql注入 一.SQL注入是什么? SQL注入是一种代码注入技术,用于攻击数据驱动的应用,恶意的SQL语句被插入到执行的SQL语句中来改变查询结果,例如: ...

  9. Sharding-JDBC 源码分析 —— SQL 改写

    1. 概述 前置阅读:<SQL 解析(三)之查询SQL> 本文分享SQL 改写的源码实现.主要涉及两方面: SQL 改写:改写 SQL,解决分库分表后,查询结果需要聚合,需要对 SQL 进 ...

最新文章

  1. Spark的安装和使用
  2. 超越99.9%人类玩家,微软专业十段麻将AI论文细节首次公布
  3. 跨境电商三单对碰三单申报流程详解
  4. vue vuex vue-router后台项目——权限路由(超详细简单版)
  5. 如果一栋楼起火谁赔偿_南昌一居民楼起火,短短几分钟里三四十人上演“救火大合唱”...
  6. Python爬虫开发
  7. 腾讯AI Lab刘霁谈机器学习,异步计算和去中心化是两大杀器
  8. linux常用命令大全(转)好东西要分享
  9. graphpad prism画折线图_如何用Graphpad Prism 8作折线图
  10. PHP网站配置项,Thinkphp5通用网站后台配置项的动态添加及更新
  11. html video 样式修改,修改video样式代码
  12. python-成都Python课程
  13. JAVA多线程----用--取钱问题1
  14. Android查看每个线程CPU占用情况,以及工作内容分析
  15. JEECG框架的入门学习
  16. Markdown编辑器使用教程_被迫流浪者的博客
  17. 我还是很喜欢你,像风走了八千里,不问归期
  18. android 视频、图片混合轮播控件zbanner
  19. 【优化】1389- 万字长文:分享前端性能优化知识体系
  20. 经典俄罗斯方块游戏手机版

热门文章

  1. [CPNet]-理想亲和图的生成以及作用——Blank
  2. 复杂正态分布运用——独立正态分布组合概率、二项分布、泊松分布近似正态分布的运用
  3. Android6.0动态壁纸,M Launcher下载-M桌面-安卓6.0桌面 v1.4.3_手机乐园
  4. mysql带条件的插入语句
  5. 构建ROP链实现远程栈溢出
  6. 打开keil提示未安装xx器件包,实际上已经安装
  7. Python爬取某站上海租房图片!
  8. MYSQL 源码安装 root 用户
  9. 中国服务器连通状态,ppp服务器连通状态显示失败怎么办?
  10. Neo4j-Graph Database Internals