Mybatis拦截器 http://www.cnblogs.com/fangjian0423/p/mybatis-interceptor.html
两个重要注解:@Intercepts和@Signature,
@Intercepts用于表示当前对象是Interceptor拦截器,
@Signature用于展示需要拦截的接口、方法和参数。
eg:
@Intercepts(@Signature(method=”handleResultSets”,type=ResultSetHandler.class,args={Statement.class}))
这里的method就是org.apache.ibatis.executor.Executor里面的方法, 接口为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
/** * Copyright 2009-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.ibatis.executor; import java.sql.SQLException; import java.util.List; import org.apache.ibatis.cache.CacheKey; import org.apache.ibatis.cursor.Cursor; import org.apache.ibatis.mapping.BoundSql; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.reflection.MetaObject; import org.apache.ibatis.session.ResultHandler; import org.apache.ibatis.session.RowBounds; import org.apache.ibatis.transaction.Transaction; /** * @author Clinton Begin */ public interface Executor { ResultHandler NO_RESULT_HANDLER = null; int update(MappedStatement ms, Object parameter) throws SQLException; <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey cacheKey, BoundSql boundSql) throws SQLException; <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException; <E> Cursor<E> queryCursor(MappedStatement ms, Object parameter, RowBounds rowBounds) throws SQLException; List<BatchResult> flushStatements() throws SQLException; void commit(boolean required) throws SQLException; void rollback(boolean required) throws SQLException; CacheKey createCacheKey(MappedStatement ms, Object parameterObject, RowBounds rowBounds, BoundSql boundSql); boolean isCached(MappedStatement ms, CacheKey key); void clearLocalCache(); void deferLoad(MappedStatement ms, MetaObject resultObject, String property, CacheKey key, Class<?> targetType); Transaction getTransaction(); void close(boolean forceRollback); boolean isClosed(); void setExecutorWrapper(Executor executor); } |
自己的一个打印参数的插件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
package com.pandy.framework.core.plugin.mybatis;/** * Created by pandy on 16-5-12. */ import com.pandy.framework.base.utils.JSONUtil; import com.pandy.framework.base.utils.MapUtil; 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.reflection.SystemMetaObject; import org.apache.ibatis.session.Configuration; import org.apache.ibatis.session.ResultHandler; import org.apache.ibatis.session.RowBounds; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.text.SimpleDateFormat; import java.util.*; /** * 项目名称: workspace * * 功能说明: 显示和打印SQL的信息 * 创建者: Pandy, * 邮箱: panyongzheng@163.com, 1453261799@qq.com * 版权: * 官网: * 创建日期: 16-5-12. * 创建时间: 下午3:57. * 修改历史: * ----------------------------------------------- */ /** * 拦截StatementHandler里的 prepare方法把执行的sql进行记录到文件里 * * @author panguixiang */ @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 PrintSQLInterceptor implements Interceptor { private static Logger logger = LoggerFactory.getLogger(PrintSQLInterceptor.class); private String dialect; private boolean isDebug = false; private String dateFormat = "yyyy-MM-dd"; private String timeFormat = "HH:mm"; private String dateTimeFormat = "yyyy-MM-dd HH:mm"; @Override public Object intercept(Invocation invocation) throws Throwable { if (!isDebug||!logger.isDebugEnabled()) { return invocation.proceed(); } Object obj = invocation.proceed(); proxyIntercept(invocation); return obj; } private void proxyIntercept(Invocation invocation) { try { /* StatementHandler statementHandler = (StatementHandler) invocation.getTarget(); MetaObject metaStatementHandler = MetaObject.forObject(statementHandler); */ MappedStatement ms = (MappedStatement) invocation.getArgs()[0]; MetaObject metaStatementHandler = SystemMetaObject.forObject(ms); Configuration configuration = ms.getConfiguration(); String sqlId = ms.getId(); Object parameter = null; if (invocation.getArgs().length > 1) { parameter = invocation.getArgs()[1]; } BoundSql boundSql = ms.getBoundSql(parameter); String sql = boundSql.getSql(); List<ParameterMapping> list = boundSql.getParameterMappings(); Object parameterObject = boundSql.getParameterObject(); logger.debug("=================================执行SQL信息========================================"); logger.debug("Method: " + (invocation.getMethod().getDeclaringClass()+"."+invocation.getMethod().getName())); logger.debug("SQL ID: " + sqlId); logger.debug("SQL 内容:\n " + sql); if (parameterObject != null) { Map<String, Object> p = null; if (parameterObject instanceof HashMap) { p = (Map<String, Object>) parameterObject; } else { p = MapUtil.transBean2Map(parameterObject); } if (p.size() > 0) { logger.debug("打印参数"); for (Map.Entry<String, Object> entry : p.entrySet()) { String key = entry.getKey(); Object value = entry.getValue(); printParamter(key, value); } logger.debug("实际参数1:\n " + JSONUtil.toJSONString(parameterObject)); logger.debug("参数类型:\n " + JSONUtil.toJSONString(list)); List<Object> params = new ArrayList<Object>(); for(ParameterMapping parameterMapping:list){ params.add(getValue(p.get(parameterMapping.getProperty()))); System.out.println(parameterMapping.getProperty()+":"+getValue(p.get(parameterMapping.getProperty()))); } logger.debug("实际参数2:\n " + JSONUtil.toJSONString(params)); } } logger.debug("\n"); } catch (Exception e) { e.printStackTrace(); } } private Object getValue(Object value){ if (value == null) { return null; } if (value instanceof Date) { SimpleDateFormat format = new SimpleDateFormat(dateTimeFormat); return format.format(value); } return value; } private void printParamter(String key, Object value) { if (value == null) { logger.debug(" " + key + ":null"); return; } if (value instanceof Date) { SimpleDateFormat format = new SimpleDateFormat(dateTimeFormat); logger.debug(" " + key + ":" + format.format(value)); return; } logger.debug(" " + key + ":" + value.toString()); } @Override public Object plugin(Object target) { return Plugin.wrap(target, this); } @Override public void setProperties(Properties properties) { // TODO Auto-generated method stub } public String getDialect() { return dialect; } public void setDialect(String dialect) { this.dialect = dialect; } public boolean getDebug() { return isDebug; } public void setIsDebug(boolean debug) { isDebug = debug; } public String getDateFormat() { return dateFormat; } public void setDateFormat(String dateFormat) { this.dateFormat = dateFormat; } public String getTimeFormat() { return timeFormat; } public void setTimeFormat(String timeFormat) { this.timeFormat = timeFormat; } public String getDateTimeFormat() { return dateTimeFormat; } public void setDateTimeFormat(String dateTimeFormat) { this.dateTimeFormat = dateTimeFormat; } } |
写个mybatis的拦截插件,实现将所有执行的sql写入文件里 原文 http://3131854.blog.51cto.com/3121854/1147446
mybatis使用拦截器显示sql,使用druid配置连接信息 http://www.cnblogs.com/babyhhcsy/p/4500884.html@Intercepts( {
@Signature(method = “query”, type = Executor.class, args = {
MappedStatement.class, Object.class, RowBounds.class,
ResultHandler.class }),
@Signature(method = “prepare”, type = StatementHandler.class, args = { Connection.class }) })
@Intercepts标记了这是一个Interceptor,然后在@Intercepts中定义了两个@Signature,即两个拦截点。第一个@Signature我们定义了该Interceptor将拦截Executor接口中参数类型为MappedStatement、Object、RowBounds和ResultHandler的query方法;第二个@Signature我们定义了该Interceptor将拦截StatementHandler中参数类型为Connection的prepare方法。
这次项目中要求把所有mybatis所执行的sql都记录到一个文件中,下面是自己写的一个插件(源是参考网上一个重写mybatis分页插件的例子):
1.配置插件(在mybatis的配置文件里mybatis.xml配置自己写的这个插件(拦截器)):
2.MyBatisSQLInterceptor的内容: