攔截器的一個做用就是咱們能夠攔截某些方法的調用,咱們能夠選擇在這些被攔截的方法執行先後加上某些邏輯,也能夠在執行這些被攔截的方法時執行本身的邏輯而再也不執行被攔截的方法。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>