mybatis Interceptor 監控慢查詢

  1. 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 resource="db.properties" />
    
    	<settings>
    
    
    		<setting name="mapUnderscoreToCamelCase" value="true" />
    
    		<setting name="aggressiveLazyLoading" value="false" />
    
    
    
    
    	</settings>
    
    	<!-- 別名定義 -->
    
    	<typeAliases>
    		<package name="com.enjoylearning.mybatis.entity" />
    	</typeAliases>
    
    	<plugins>
    
    		<!-- 定位慢查詢 -->
    		<plugin interceptor="com.enjoylearning.mybatis.Interceptors.ThresholdInterceptor"> 
    			<property name="threshold" value="10"/>
    		</plugin>
    			<!--分頁 插件-->
    		 <plugin interceptor="com.github.pagehelper.PageInterceptor">
    			<property name="pageSizeZero" value="true" />  <!--默認爲false    pageSize 爲0 而且 pageSizeZero 爲true 代碼不分頁 -->
    		</plugin>
    
    
    
    	</plugins>
    
    
    	<!--配置environment環境 -->
    	<environments default="development">
    		<!-- 環境配置1,每個SqlSessionFactory對應一個環境 -->
    		<environment id="development">
    			<transactionManager type="JDBC" />
    			<dataSource type="UNPOOLED">
    				<property name="driver" value="${jdbc_driver}" />
    				<property name="url" value="${jdbc_url}" />
    				<property name="username" value="${jdbc_username}" />
    				<property name="password" value="${jdbc_password}" />
    			</dataSource>
    		</environment>
    
    
    	</environments>
    
    	<!-- 映射文件,mapper的配置文件 -->
    	<mappers>
    		<!--直接映射到相應的mapper文件 -->
    		<mapper resource="sqlmapper/TUserMapper.xml" />
    		<mapper resource="sqlmapper/TRoleMapper.xml" />
    		<mapper resource="sqlmapper/TJobHistoryMapper.xml" />
    		<mapper resource="sqlmapper/TPositionMapper.xml" />
    		<mapper resource="sqlmapper/THealthReportFemaleMapper.xml" />
    		<mapper resource="sqlmapper/THealthReportMaleMapper.xml" />
    
    	</mappers>
    
    
    	<!-- <mapper class="com.enjoylearning.mybatis.mapper.TJobHistoryAnnoMapper"/> 
    		</mappers> -->
    
    </configuration>  

     

package com.enjoylearning.mybatis.Interceptors;

import java.sql.Statement;
import java.util.Properties;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.resultset.ResultSetHandler;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.logging.jdbc.PreparedStatementLogger;
import org.apache.ibatis.logging.jdbc.StatementLogger;
import org.apache.ibatis.mapping.MappedStatement;
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.reflection.SystemMetaObject;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;


@Intercepts({
	@Signature(type=StatementHandler.class,method="query",args={Statement.class, ResultHandler.class})
//	@Signature(type=StatementHandler.class,method="query",args={MappedStatement.class,Object.class, RowBounds.class, ResultHandler.class})
})
//攔截所有查詢方法
public class ThresholdInterceptor implements Interceptor {
	// 在config 配置文件中配置的<property name="threshold" value="100"/> 此時爲10毫秒
	private long threshold;

	@Override
	public Object intercept(Invocation invocation) throws Throwable {
		long begin = System.currentTimeMillis();
		Object ret = invocation.proceed();
		long end=System.currentTimeMillis();
		long runTime = end - begin;
		if(runTime>=threshold){
			Object[] args = invocation.getArgs();
			Statement stat = (Statement) args[0];
			MetaObject metaObjectStat = SystemMetaObject.forObject(stat);
			PreparedStatementLogger statementLogger = (PreparedStatementLogger)metaObjectStat.getValue("h");
			Statement statement = statementLogger.getPreparedStatement();
			System.out.println("sql語句:“"+statement.toString()+"”執行時間爲:"+runTime+"毫秒,已經超過閾值!");
		}
		return ret;
	}

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

	@Override
	public void setProperties(Properties properties) {
		this.threshold = Long.valueOf(properties.getProperty("threshold"));
	}



	
	

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章