Mybatis拦截器实现SQL性能监控

来源:互联网 发布:java纯数字正则表达式 编辑:程序博客网 时间:2024/05/21 07:54
Mybatis拦截器实现SQL性能监控

  Mybatis拦截器只能拦截四类对象,分别为:Executor、ParameterHandler、StatementHandler、ResultSetHandler,而SQL数据库的操作都是从Executor开始,因此要记录Mybatis数据库操作的耗时,需要拦截Executor类,代码实现如下:

复制代码
/** * 数据库操作性能拦截器,记录耗时 * @Intercepts定义Signature数组,因此可以拦截多个,但是只能拦截类型为: *         Executor *         ParameterHandler *         StatementHandler *         ResultSetHandler * */@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 TimerInterceptor implements Interceptor {    private static final Logger logger = Logger.getLogger(TimerInterceptor.class);        /**     * 实现拦截的地方     * */    @Override    public Object intercept(Invocation invocation) throws Throwable {        Object target = invocation.getTarget();        Object result = null;        if (target instanceof Executor) {            long start = System.currentTimeMillis();            Method method = invocation.getMethod();            /**执行方法*/            result = invocation.proceed();            long end = System.currentTimeMillis();            logger.info("[TimerInterceptor] execute [" + method.getName() + "] cost [" + (end - start) + "] ms");        }        return result;    }    /**     * Plugin.wrap生成拦截代理对象     * */    @Override    public Object plugin(Object target) {        return Plugin.wrap(target, this);    }    @Override    public void setProperties(Properties properties) {    }}
复制代码

  完成上面的拦截后,需要将该类在Mybatis配置文件中声明,如下:

<plugins>     <!-- SQL性能拦截器 -->     <plugin interceptor="com.quar.interceptor.TimerInterceptor" /></plugins>

 


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 内存太小怎么办手机 苹果手机屏幕不能滑动怎么办 vivo手机软件不兼容怎么办 微信无法录音怎么办 手机卡住了怎么办vivo 好钱包闪退怎么办 闲鱼认证失败怎么办 闲鱼买了假门票怎么办 买黄金买到假的怎么办 闲鱼被买家骗了怎么办 闲鱼上小视频没法保存怎么办 qq空间无法查看怎么办 华为手机电池不耐用怎么办 内内被动过怎么办 hp电脑开机黑屏怎么办 网上开店快递费怎么办 保温杯外壳掉漆怎么办 拖鞋前面磨脚怎么办 塑料拖鞋磨脚怎么办 路由器进不去设置界面怎么办 手机号丢了微信登不上怎么办 电脑总是闪黑屏怎么办 支付宝破产钱怎么办 淘宝号码注册过怎么办 农行k宝怎么办信用卡 电脑页面无法显示怎么办 对方银行停止收款怎么办 淘宝东西买太多怎么办 淘宝号黑号了怎么办… 中通包裹异常怎么办 包裹退回去了怎么办 qq支付密码错误怎么办 ie8出现闪退怎么办 平板输不了密码怎么办 华硕笔记本键盘打不开怎么办 电脑打不开rar文件怎么办 苹果手机淘宝卡怎么办 淘宝联盟网址打不开怎么办 淘宝买东西卖家不退货怎么办 手机清理后微信打不开视频怎么办 搜索历史已关闭怎么办