博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mybatis输出SQL格式化
阅读量:7260 次
发布时间:2019-06-29

本文共 5870 字,大约阅读时间需要 19 分钟。

通过第三方日志工具可以控制日志级别的输出,但是我们发现mybatis输出的SQL不是那么的完整,我们SQL里的参数值并没有打印出来,下面我就来讲讲怎么样对mybatis的输出sql格式化。

首先我这个案例是基于Spring boot 来开发的,所以配置和传统的xml配置有所区别,spring boot大大简化了一些配置,它把配置放到java代码,我们只需要使用注解就可替代一些以前xml的配置。

自定义拦截器

import java.io.PrintStream;import java.text.DateFormat;import java.util.Date;import java.util.List;import java.util.Locale;import java.util.Properties;import java.util.regex.Matcher;import org.apache.commons.collections.CollectionUtils;import org.apache.ibatis.mapping.BoundSql;import org.apache.ibatis.mapping.MappedStatement;import org.apache.ibatis.mapping.ParameterMapping;import org.apache.ibatis.plugin.Interceptor;import org.apache.ibatis.plugin.Intercepts;import org.apache.ibatis.plugin.Invocation;import org.apache.ibatis.plugin.Plugin;import org.apache.ibatis.reflection.MetaObject;import org.apache.ibatis.session.Configuration;import org.apache.ibatis.type.TypeHandlerRegistry;import org.springframework.stereotype.Component;/** * 自定义mybatis拦截器,格式化SQL输出, * * @author zengsong * @version 1.0 * @description 只对查询和更新语句做了格式化,其它语句需要手动添加 * @date 2019/5/30 10:17 **/@Intercepts({@org.apache.ibatis.plugin.Signature(type=org.apache.ibatis.executor.Executor.class, method="update", args={MappedStatement.class, Object.class}), @org.apache.ibatis.plugin.Signature(type=org.apache.ibatis.executor.Executor.class, method="query", args={MappedStatement.class, Object.class, org.apache.ibatis.session.RowBounds.class, org.apache.ibatis.session.ResultHandler.class})})@Componentpublic class MybatisResultInterceptor        implements Interceptor{    public Object intercept(Invocation invocation)            throws Throwable    {        try        {            MappedStatement mappedStatement = (MappedStatement)invocation.getArgs()[0];            Object parameter = null;            if (invocation.getArgs().length > 1) {                parameter = invocation.getArgs()[1];            }            String sqlId = mappedStatement.getId();            BoundSql boundSql = mappedStatement.getBoundSql(parameter);            Configuration configuration = mappedStatement.getConfiguration();            String sql = getSql(configuration, boundSql, sqlId);            System.out.println(sql);        }        catch (Exception localException) {}        return invocation.proceed();    }    public static String getSql(Configuration configuration, BoundSql boundSql, String sqlId)    {        String sql = showSql(configuration, boundSql);        StringBuilder str = new StringBuilder(100);        str.append(sqlId);        str.append(":");        str.append(sql);        return str.toString();    }    private static String getParameterValue(Object obj)    {        String value = null;        if ((obj instanceof String))        {            value = "'" + obj.toString() + "'";        }        else if ((obj instanceof Date))        {            DateFormat formatter = DateFormat.getDateTimeInstance(2, 2, Locale.CHINA);            value = "'" + formatter.format(new Date()) + "'";        }        else if (obj != null)        {            value = obj.toString();        }        else        {            value = "";        }        return value;    }    public static String showSql(Configuration configuration, BoundSql boundSql)    {        Object parameterObject = boundSql.getParameterObject();        List
parameterMappings = boundSql.getParameterMappings(); String sql = boundSql.getSql().replaceAll("[\\s]+", " "); MetaObject metaObject; if ((CollectionUtils.isNotEmpty(parameterMappings)) && (parameterObject != null)) { TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry(); if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) { sql = sql.replaceFirst("\\?", Matcher.quoteReplacement(getParameterValue(parameterObject))); } else { metaObject = configuration.newMetaObject(parameterObject); for (ParameterMapping parameterMapping : parameterMappings) { String propertyName = parameterMapping.getProperty(); if (metaObject.hasGetter(propertyName)) { Object obj = metaObject.getValue(propertyName); sql = sql.replaceFirst("\\?", Matcher.quoteReplacement(getParameterValue(obj))); } else if (boundSql.hasAdditionalParameter(propertyName)) { Object obj = boundSql.getAdditionalParameter(propertyName); sql = sql.replaceFirst("\\?", Matcher.quoteReplacement(getParameterValue(obj))); } else { sql = sql.replaceFirst("\\?", "缺失"); } } } } return sql; } public Object plugin(Object target) { return Plugin.wrap(target, this); } public void setProperties(Properties properties) {}}

配置拦截器

import com.sengled.cloud.data.platform.dao.mybatis.MybatisResultInterceptor;import java.util.Properties;import javax.annotation.Resource;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;/** * 自定义mybatis拦截器 * * @author zengsong * @version 1.0 * @description * @date 2019/5/30 10:17 **/@Configurationpublic class MybatisInterceptorConfig{    @Resource    private MybatisResultInterceptor mybatisResultInterceptor;    @Bean    public String myInterceptor()    {        Properties properties = new Properties();        this.mybatisResultInterceptor.setProperties(properties);        return "interceptor";    }}

配置日志级别

 

转载于:https://www.cnblogs.com/zengsong-restService/p/3296371.html

你可能感兴趣的文章
2.6 datetime 模块
查看>>
[论文笔记] Task search in a human computation market (HCOMP, 2010)
查看>>
034:DTL常用过滤器(3)
查看>>
Socket编程之简单介绍
查看>>
ant常用命令
查看>>
计算机专业学习浅谈
查看>>
.NET 进程和线程
查看>>
设计数据库的三范式
查看>>
【c学习-6】
查看>>
自测题的整理(持续更新)
查看>>
DAMS2019中国数据智能管理峰会将于7月在上海召开!
查看>>
[原创]TimeQuest约束外设之诡异的Create Generated Clocks用法
查看>>
Unity UGUI —— 无限循环List(转载)
查看>>
【总结整理】《人人都是产品经理》---读后感
查看>>
第23件事 评估产品或项目是否靠谱的7个标准
查看>>
MySQL的优化与执行
查看>>
04-人员增删改查
查看>>
Python之自动单元测试之一(unittest使用实例)
查看>>
ORA-04031:oracle无法分配共享内存
查看>>
Mysql SQL Mode详解
查看>>