java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > mybatis-plus拦截器实现sql打印

mybatis-plus配置拦截器实现sql完整打印的代码设计

作者:shigen01

在使用mybatis-plus(mybatis)的时候,往往需要打印完整的sql语句,然而输出的日志不是很理想,因为sql语句中的关键字段信息都是用?来代替的,所以本文分享了一下自己写了一个拦截器实现了sql完整的打印,需要的朋友可以参考下

在使用mybatis-plus(mybatis)的时候,往往需要打印完整的sql语句,然而输出的日志不是很理想:

因为sql语句中的关键字段信息都是用?来代替的。那有什么方法实现完整的sql打印呢?有是有的,我记得IDEA的插件市场有一款插件可以实现完整sql的打印,但是好像是要收费的。今天刷某音的时候看到了某博主分享了一下自己写了一个拦截器实现了sql完整的打印,以下是实现的效果:

可以看到了sql的执行时间和完整的sql语句。sql的执行时间没啥好说的,关键是sql语句的完整打印。现在先来分享一下代码吧。

代码

controller的设计

这里仅展示关键的代码,一个更新的操作,一个分页查询的操作。

     @PostMapping(value = "update")
     public Result<String> update(@RequestBody @Validated(value = UpdateGroup.class) User user) {
         int update = userMapper.updateById(user);
         return update > 0 ? Result.ok(null) : Result.err(null);
     }
 ​
     @GetMapping(value = "get")
     public Result<List<User>> get(@RequestParam(value = "id", required = false) Integer id,
                                   @RequestParam(value = "name", required = false) String name
     ) {
         LambdaQueryWrapper<User> queryChainWrapper = new LambdaQueryWrapper<>();
         queryChainWrapper.eq(id != null, User::getId, id);
         queryChainWrapper.eq(name != null, User::getUsername, name);
         List<User> records = userMapper.selectPage(new Page<User>(0, 10), queryChainWrapper).getRecords();
         return Result.ok(records);
     }

拦截器设计

虽然这里是mybatis-plus框架,但是还是需要使用到mybatis的功能。

 /**
  * @author shigenfu
  * @date 2024/6/16 10:01
  */
 @Intercepts({
         @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}),
         @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}),
         @Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class})
 })
 @Slf4j
 public class SqlInterceptor implements Interceptor {
 ​
     @Override
     public Object intercept(Invocation invocation) throws Throwable {
         // 统计sql执行耗时
         long startTime = System.currentTimeMillis();
         Object proceed = invocation.proceed();
         long endTime = System.currentTimeMillis();
 ​
         String printSql = null;
         try {
             printSql = generateSql(invocation);
         } catch (Exception exception) {
             log.error("获取sql异常", exception);
         } finally {
             long costTime = endTime - startTime;
             log.info("\n 执行SQL耗时:{}ms \n 执行SQL:{}", costTime, printSql);
         }
         return proceed;
     }
 ​
     private static String generateSql(Invocation invocation) {
 ​
         MappedStatement statement = (MappedStatement) invocation.getArgs()[0];
         Object parameter = null;
         if (invocation.getArgs().length > 1) {
             parameter = invocation.getArgs()[1];
         }
         Configuration configuration = statement.getConfiguration();
         BoundSql boundSql = statement.getBoundSql(parameter);
 ​
         // 获取参数对象
         Object parameterObject = boundSql.getParameterObject();
         // 获取参数映射
         List<ParameterMapping> params = boundSql.getParameterMappings();
         // 获取到执行的SQL
         String sql = boundSql.getSql();
         // SQL中多个空格使用一个空格代替
         sql = sql.replaceAll("[\s]+", " ");
         if (!ObjectUtils.isEmpty(params) && !ObjectUtils.isEmpty(parameterObject)) {
             // TypeHandlerRegistry 是 MyBatis 用来管理 TypeHandler 的注册器 TypeHandler 用于在 Java 类型和 JDBC 类型之间进行转换
             TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
             // 如果参数对象的类型有对应的 TypeHandler,则使用 TypeHandler 进行处理
             if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
                 sql = sql.replaceFirst("\?", Matcher.quoteReplacement(getParameterValue(parameterObject)));
             } else {
                 // 否则,逐个处理参数映射
                 for (ParameterMapping param : params) {
                     // 获取参数的属性名
                     String propertyName = param.getProperty();
                     MetaObject metaObject = configuration.newMetaObject(parameterObject);
                     // 检查对象中是否存在该属性的 getter 方法,如果存在就取出来进行替换
                     if (metaObject.hasGetter(propertyName)) {
                         Object obj = metaObject.getValue(propertyName);
                         sql = sql.replaceFirst("\?", Matcher.quoteReplacement(getParameterValue(obj)));
                         // 检查 BoundSql 对象中是否存在附加参数
                     } else if (boundSql.hasAdditionalParameter(propertyName)) {
                         Object obj = boundSql.getAdditionalParameter(propertyName);
                         sql = sql.replaceFirst("\?", Matcher.quoteReplacement(getParameterValue(obj)));
                     } else {
                         // SQL匹配不上,带上“缺失”方便找问题
                         sql = sql.replaceFirst("\?", "缺失");
                     }
                 }
             }
         }
         return sql;
     }
 ​
     private static String getParameterValue(Object object) {
         String value = "";
         if (object instanceof String) {
             value = "'" + object + "'";
         } else if (object instanceof Date) {
             DateFormat format = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.CHINA);
             value = "'" + format.format((Date) object) + "'";
         } else if (!ObjectUtils.isEmpty(object)) {
             value = object.toString();
         }
         return value;
     }
 ​
     @Override
     public Object plugin(Object target) {
         return Plugin.wrap(target, this);
     }
 ​
 }

直接贴的代码,其实就是在sql执行完毕之后,根据sql的template和sql参数进行?的替换。

这里不分析代码,希望能亲自debug看一下。

配置类

这里的配置我都写在了mybatis-plus的配置代码里边。

 @Configuration
 @MapperScan(value = "main.java.shigen.demo.dao")
 public class MybatisPlusConfig {
 ​
     @Bean
     public MybatisPlusInterceptor mybatisPlusInterceptor() {
         MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
         // 分页插件
         interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
         // 乐观锁插件
         interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
         return interceptor;
     }
 ​
     @Bean
     public ConfigurationCustomizer configurationCustomizer() {
         return configuration -> {
             configuration.addInterceptor(new SqlInterceptor());
         };
     }
 }

以上就是核心的代码了,实测遇到的问题有一个:

希望有时间的时候能够再次优化一下。同时,也没有经过实际的项目测试,只是简单的demo测试。仅具有参考价值,无法保证实际的应用。

后记

到此这篇关于mybatis-plus配置拦截器实现sql完整打印的代码设计的文章就介绍到这了,更多相关mybatis-plus拦截器实现sql打印内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文