栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot整合MybatisSQL过滤@Intercepts的实现

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot整合MybatisSQL过滤@Intercepts的实现

场景:

系统模块查询数据库需要根据用户的id去筛选数据。那么如果在 每个sql加user_id的过滤显然不明确。所以要在查询前将sql拼接上条件,做统一管理。

开发环境:

spring boot + mybatis

只需一个拦截类即可搞定(在看代码前需要了解注解@Intercepts()):

@Component
@Intercepts({ @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class }) })
public class SqlInterceptor implements Interceptor {

  private Logger logger = LoggerFactory.getLogger(SqlInterceptor.class);

  @Override
  public Object intercept(Invocation invocation) throws Throwable {
 logger.info("Interceptor......");
//    获取sql
 MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
 Object parameter = invocation.getArgs()[1];
 BoundSql boundSql = mappedStatement.getBoundSql(parameter);
 String oldsql = boundSql.getSql();
 logger.info("old:"+oldsql);

//    判断sql是否有where条件。改变sql。
 boolean status = oldsql.toLowerCase().contains("where");

 if (status) {
   BoundSql newBoundSql = new BoundSql(mappedStatement.getConfiguration(), oldsql + " and 1=1",
boundSql.getParameterMappings(), boundSql.getParameterObject());
   MappedStatement newMs = copyFromMappedStatement(mappedStatement, new BoundSqlSqlSource(newBoundSql));
   invocation.getArgs()[0] = newMs;
 }
// 继续执行
 Object result = invocation.proceed();
 return result;
  }

  @Override
  public Object plugin(Object target) {
 return Plugin.wrap(target, this);
  }

  @Override
  public void setProperties(Properties properties) {

  }

  // 复制原始MappedStatement
  private MappedStatement copyFromMappedStatement(MappedStatement ms, SqlSource newSqlSource) {
 MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), ms.getId(), newSqlSource,
     ms.getSqlCommandType());
 builder.resource(ms.getResource());
 builder.fetchSize(ms.getFetchSize());
 builder.statementType(ms.getStatementType());
 builder.keyGenerator(ms.getKeyGenerator());
 if (ms.getKeyProperties() != null) {
   for (String keyProperty : ms.getKeyProperties()) {
     builder.keyProperty(keyProperty);
   }
 }
 builder.timeout(ms.getTimeout());
 builder.parameterMap(ms.getParameterMap());
 builder.resultMaps(ms.getResultMaps());
 builder.cache(ms.getCache());
 builder.useCache(ms.isUseCache());
 return builder.build();
  }

  public static class BoundSqlSqlSource implements SqlSource {
 BoundSql boundSql;

 public BoundSqlSqlSource(BoundSql boundSql) {
   this.boundSql = boundSql;
 }

 public BoundSql getBoundSql(Object parameterObject) {
   return boundSql;
 }
  }

}

这样重启访问即可发现每次查询都会走这边!

到此这篇关于SpringBoot整合MybatisSQL过滤@Intercepts的实现的文章就介绍到这了,更多相关SpringBoot MybatisSQL过滤@Intercepts内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/134843.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号