开发时调试使用

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<property name="dataSource">
<ref bean="dataSource" />
</property>
<!-- 自动到mappers包下面去搜索mybatis的映射文件 -->
<property name="mapperLocations" value="classpath:com/a/*/**/mapping/*.xml" />
<property name="typeAliasesPackage" value="com.a" />
</bean>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<plugins>
<plugin interceptor="com.a.ab.mybatis.interceptor.SqlInterceptor">
</plugin>
</plugins>
</configuration>
package com.cares.asis.mybatis.interceptor;

import java.text.DateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Properties; import org.apache.ibatis.executor.Executor;
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.plugin.Signature;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.type.TypeHandlerRegistry; /**
* mybatis打印sql 用于在后台打印执行的sql,在applicationContext.xml中的<bean
* id="sqlSessionFactory" 标签中添加:<property name="configLocation"
* value="classpath:mybatis-config.xml" /> 即可打印sql语句 此方法仅用于开发调试,配置之后请勿提交
*/ @Intercepts( { @Signature(type = Executor.class, method = "update", args = { MappedStatement.class, Object.class }), @Signature(type = Executor.class, method = "query", args = { MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class }) })
public class SqlInterceptor implements Interceptor { private Properties properties; public Object intercept(Invocation invocation) throws Throwable {
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();
Object returnValue = null;
returnValue = invocation.proceed();
String sql = getSql(configuration, boundSql, sqlId);
System.err.println("ASIS_Print:" + sql);
return returnValue;
} /**
* sql执行的具体位置与sql的拼接
* */
public static String getSql(Configuration configuration, BoundSql boundSql, String sqlId) {
String sql = showSql(configuration, boundSql);
StringBuilder str = new StringBuilder(600);
str.append(sqlId);
str.append(":");
str.append(sql);
return str.toString();
} /**
* 拼接sql中的所有参数
* */
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(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.CHINA);
value = "'" + formatter.format(new Date()) + "'";
} else {
if (obj != null) {
value = obj.toString();
} else {
value = "";
}
}
return value;
} /**
* 解析sql
* */
public static String showSql(Configuration configuration, BoundSql boundSql) {
Object parameterObject = boundSql.getParameterObject();
List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
String sql = boundSql.getSql().replaceAll("[\\s]+", " ");
if (parameterMappings.size() > 0 && parameterObject != null) {
TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
sql = sql.replaceFirst("\\?", getParameterValue(parameterObject)); } else {
MetaObject metaObject = configuration.newMetaObject(parameterObject);
for (ParameterMapping parameterMapping : parameterMappings) {
String propertyName = parameterMapping.getProperty();
if (metaObject.hasGetter(propertyName)) {
Object obj = metaObject.getValue(propertyName);
sql = sql.replaceFirst("\\?", getParameterValue(obj));
} else if (boundSql.hasAdditionalParameter(propertyName)) {
Object obj = boundSql.getAdditionalParameter(propertyName);
sql = sql.replaceFirst("\\?", getParameterValue(obj));
}
}
}
}
return sql;
} public Object plugin(Object target) {
return Plugin.wrap(target, this);
} public void setProperties(Properties properties0) {
this.properties = properties0;
}
}

mybatis 控制台打印sql的更多相关文章

  1. Springboot中mybatis控制台打印sql语句

    Springboot中mybatis控制台打印sql语句 https://www.jianshu.com/p/3cfe5f6e9174 https://www.jianshu.com/go-wild? ...

  2. mybatis 控制台打印sql语句

    其实很简单,打印SQL只需要加一个setting就可以了.亲测可用. mybatis-config.xml: <settings>        <setting name=&quo ...

  3. Mybatis控制台打印SQL语句的两种方式

    问题描述在使用mybatis进行开发的时候,由于可以动态拼接sql,这样大大方便了我们.但是也有一定的问题,当我们动态sql拼接的块很多的时候,我们要想从*mapper.xml中直接找出完整的sql就 ...

  4. Mybatis控制台打印sql

    mybatis-config.xml配置如下: <configuration> <settings> <setting name="lazyLoadingEna ...

  5. mybatis 控制台打印sql脚本

    在mybatis-config.xml文件中加一句 <setting name="logImpl" value="STDOUT_LOGGING" /> ...

  6. log4j.xml简单配置实现在控制台打印sql执行语句【加注释】

    转: log4j.xml简单配置实现在控制台打印sql执行语句 2017年09月27日 13:02:34 艾然丶 阅读数 8804   版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协 ...

  7. Spring Boot 集成日志logback + 控制台打印SQL

    一: 控制台打印SQL application.properties中添加如下即可在控制台打印sql logging.level.com.fx.fxxt.mapper=debug 二:日志 因为Spr ...

  8. mybatis日志,打印sql语句,输出sql

    mybatis日志,打印sql语句,输出sql<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE ...

  9. maven项目整合SSM配置log4j, 实现控制台打印SQL语句

    在原有项目正常启动的情况下, 实现在控制台打印mapper包下SQL语句. 1.在pom.xml配置文件中添加两个依赖(缺一不可) <!--日志包--> <dependency> ...

随机推荐

  1. (C#) 求两个数组的交集

    基本上在面试的时候,会具体到两个int数组,或string数组.具体也就是讨论算法. 首先需要的是和面试的人确认题目的含义,并非直接答题. 然后,可以提出自己的想法,首先最快的是用linq { Lis ...

  2. HTC Vive开发笔记之手柄震动

    手柄震动的代码SteamVR_Controller脚本的最上面的注释里面就有说明,其实也很简单 // Example usage: //这个栗子是左手柄震动 右手震动只需把Leftmost换成Righ ...

  3. ArcGIS 10.1 中的style和serverstyle及制作方法

    图层的符号都是随机的,动态变化的,所以不利于图层的美观.还有一点就是符号有增加电子地图可读性的功能. Desktop的符号文件 把要用的符号用desktop的style manage制作相应符号,并把 ...

  4. goldengate 参数之GETTRUNCATES | IGNORETRUNCATES --转载

    GETTRUNCATES | IGNORETRUNCATESValid ForExtract and ReplicatDescriptionUse the GETTRUNCATESand IGNORE ...

  5. C++学习基础六——复制构造函数和赋值操作符

    1.什么是复制构造函数 复制构造函数:是构造函数,其只有一个参数,参数类型是所属类的类型,且参数是一个const引用. 作用:将本类的成员变量赋值为引用形参的成员变量. 2.什么是赋值操作符 赋值操作 ...

  6. Linux:安装图形界面

    能连接网络的前提下,使用yum安装 yum groupinstall  -y "Desktop"yum groupinstall   -y "X Window Syste ...

  7. yum命令指南

    yum check-update  检查可更新的所有软件包 yum update  下载更新系统已安装的所有软件包yum upgrade  大规模的版本升级,与yum update不同的是,连旧的淘汰 ...

  8. myeclipse 在mac中字体模糊问题解决方案

    找到文件:/Applications/MyEclipse 2014/MyEclipse 2014.app/Contents/Profile/myeclipse.app/Contents/Info.pl ...

  9. ---Under Ubuntu 14.04

    Thinprint provides driver-free printing. Do you wish to enable this feature? [yes] Disabling timer-b ...

  10. 处理返回结果(XML)

    var xmlHttp function showUser(str) { xmlHttp=GetXmlHttpObject() if (xmlHttp==null) { alert ("Br ...