mybatis 拦截器的使用—打印sql语句

 

拦截器的一个做用就是咱们能够拦截某些方法的调用,咱们能够选择在这些被拦截的方法执行先后加上某些逻辑,也能够在执行这些被拦截的方法时执行本身的逻辑而再也不执行被拦截的方法。Mybatis拦截器设计的一个初衷就是为了供用户在某些时候能够实现本身的逻辑而没必要去动Mybatis固有的逻辑。mybatis拦截器通常用于分页插件、输出日志、sql等。使用的方法以下:java

首先要实现mybatis的Interceptor接口,spring

实现它的三个方法:sql

Object intercept(Invocation invocation) throws Throwable;

Object plugin(Object target);

void setProperties(Properties properties);

plugin方法是拦截器用于封装目标对象的,经过该方法咱们能够返回目标对象自己,也能够返回一个它的代理。当返回的是代理的时候咱们能够对其中的方法进行拦截来调用intercept方法,固然也能够调用其余方法,这点将在后文讲解。apache

setProperties方法是用于在Mybatis配置文件中指定一些属性的。缓存

plugin方法中咱们能够决定是否要进行拦截进而决定要返回一个什么样的目标对象。而intercept方法就是要进行拦截的时候要执行的方法。session

下面例子原本相用于打印日志到控制台,可是因为mybatis底层没法注入spring的对象,因此,只能用于输出日志:mybatis

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.*;
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;

import java.text.DateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Properties;


/**
 * 注解拦截接口的方法
 * Executor (update, query, flushStatements, commit, rollback, getTransaction, close, isClosed)
 * ParameterHandler (getParameterObject, setParameters)
 * ResultSetHandler (handleResultSets, handleOutputParameters)
 * StatementHandler (prepare, parameterize, batch, update, query)
 */
@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;
    @Override
    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;
        long start = System.currentTimeMillis();
        returnValue = invocation.proceed();
        long end = System.currentTimeMillis();
        long time = (end - start);
        if (time > 1) {
            String sql = getSql(configuration, boundSql, sqlId, time);
            System.err.println(sql);
        }
        return returnValue;
    }

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

    @Override
    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    public static String getSql(Configuration configuration, BoundSql boundSql, String sqlId, long time) {
        String sql = showSql(configuration, boundSql);
        StringBuilder str = new StringBuilder(100);
        str.append(sqlId);
        str.append(":");
        str.append(sql);
        str.append(">>>>>>");
        str.append(time);
        str.append("ms");
        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(DateFormat.DEFAULT, DateFormat.DEFAULT, 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<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;
    }

}

类上面必须添加@Intercepts注解,@Signature,表明拦截点,app

@Intercepts({@Signature(method = "update", type = Executor.class, args = {MappedStatement.class, Object.class})})

method表示须要拦截的方法,mybatis有ide

update, query, flushStatements, commit, rollback, getTransaction, close, isClosed

方法,其中,update包括新增、修改、删除等方法,query用于查询,其它的基本用不到。ui

type表示拦截的接口类型,有Executor、StatementHandler、ParameterHandler和ResultSetHandler。

args表示拦截的参数类型,有MappedStatement、Object、RowBounds和ResultHandler等等.

编写完拦截器,须要在配置文件中注册拦截器,在mybatis-config.xml配置文件中加上

<?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>
    <properties>
        <property name="dialectClass" value="com.my.mybatis.test.dialect.MySql5Dialect"/>
    </properties>

    <!-- 配置mybatis的缓存,延迟加载等等一系列属性 -->
    <settings>
        <setting name="aggressiveLazyLoading" value="false"/>

    </settings>

  

<!-- 拦截器的使用 -->
    <plugins>
  
<!-- sql输出拦截器 -->
        <plugin interceptor="com.my.mybatis.test.interceptors.SqlInterceptor"/>

    </plugins>

</configuration>
相关文章
相关标签/搜索