国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

用MyBatis攔截器記錄SQL執行時間

2019-11-08 20:18:15
字體:
來源:轉載
供稿:網友

1、在項目的applicationContext-dao.xml 文件中增加一個plugins:sqlStatementInterceptor

<bean id="dao-sqlsessionFactory" class="org.mybatis.sPRing.SqlSessionFactoryBean">        <property name="dataSource" ref="dataSource" />        <!-- <property name="configLocation" value="classpath:config/mybatis-config.xml" /> -->        <property name="mapperLocations">            <list>                <value>classpath:com/test/mapper/*.xml</value>                <value>classpath:com/test/mapper/extend/*.xml</value>            </list>        </property>        <property name="plugins">            <array>                <bean id="sqlStatementInterceptor" class="com.test.SqlStatementInterceptor"/>            </array>        </property>    </bean>

2、編寫sqlStatementInterceptor的實現代碼

package com.test;import java.util.Properties;import org.apache.ibatis.cache.CacheKey;import org.apache.ibatis.executor.Executor;import org.apache.ibatis.mapping.BoundSql;import org.apache.ibatis.mapping.MappedStatement;import org.apache.ibatis.plugin.*;import org.apache.ibatis.session.ResultHandler;import org.apache.ibatis.session.RowBounds;import org.slf4j.Logger;import org.slf4j.LoggerFactory;/** * 數據庫操作性能攔截器,記錄耗時 */@Intercepts(value = {        @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, CacheKey.class, BoundSql.class }),        @Signature(type = Executor.class, method = "query", args = { MappedStatement.class, Object.class,                RowBounds.class, ResultHandler.class }) })public class SqlStatementInterceptor implements Interceptor{    private static Logger logger = LoggerFactory.getLogger(SqlStatementInterceptor.class);    private Properties properties;    @Override    public Object intercept(Invocation arg0) throws Throwable    {        MappedStatement mappedStatement = (MappedStatement) arg0.getArgs()[0];        String sqlId = mappedStatement.getId();        Object returnValue;        long start = System.currentTimeMillis();        returnValue = arg0.proceed();        long end = System.currentTimeMillis();        long time = end - start;        StringBuilder str = new StringBuilder(100);        str.append(sqlId);        str.append(": ");        str.append("cost time ");        str.append(time);        str.append(" ms.");        String sqlInfo = str.toString();        logger.debug(sqlInfo);        return returnValue;    }    @Override    public Object plugin(Object arg0)    {        return Plugin.wrap(arg0, this);    }    @Override    public void setProperties(Properties arg0)    {        this.properties = arg0;    }}

3、對sqlStatementInterceptor.java類里面的輸出級別(上面代碼塊是debug級別),進行相應的logback.xml設置(只需修改文件內的level標簽)

4、啟動項目,從log文件中可看到日志打印[http-8080-5] DEBUG [Caller+0 at com.test.SqlStatementInterceptor.intercept(SqlStatementInterceptor.java:49) ] - com.test.testMapper.selectByExample: cost time 1 ms.

參考文章:

https://my.oschina.net/u/2265143/blog/786295

http://blog.csdn.net/tq02h2a/article/details/50772652


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 和平县| 苗栗市| 金乡县| 汉寿县| 托克托县| 龙口市| 汝阳县| 泰安市| 邢台县| 花莲市| 泸水县| 本溪| 玛沁县| 乌恰县| 报价| 漳平市| 兴化市| 新闻| 清苑县| 伊宁市| 德兴市| 麦盖提县| 新安县| 阳信县| 宁海县| 昌吉市| 黄大仙区| 调兵山市| 嘉黎县| 四会市| 德阳市| 林周县| 沙洋县| 绍兴县| 克拉玛依市| 翁牛特旗| 永顺县| 高要市| 仙桃市| 禹州市| 贺州市|