潍坊网站设计,自己怎样免费建网站,济宁门户网站建设,注册电气工程师报考条件Mybatis拦截器 概述应用场景项目结构实现分页查询其它拦截器的使用 概述
Mybatis允许使用者在映射语句执行过程中的某一些指定的节点进行拦截调用#xff0c;通过织入拦截器#xff0c;在不同节点修改一些执行过程中的关键属性#xff0c;从而影响SQL的生成、执行和返回结果… Mybatis拦截器 概述应用场景项目结构实现分页查询其它拦截器的使用 概述
Mybatis允许使用者在映射语句执行过程中的某一些指定的节点进行拦截调用通过织入拦截器在不同节点修改一些执行过程中的关键属性从而影响SQL的生成、执行和返回结果。
默认情况下Mybatis支持四种对象拦截
Executor拦截执行器的方法ParameterHandler拦截参数的处理ResultSetHandler拦截结果集的处理StatementHandler拦截Sql语法构建的处理
执行顺序Executor StatementHandler ParameterHandler ResultSetHandler
注本文代码基于《Mybatis一级缓存二级缓存》中的“一级缓存Spring整合Mybatis”的代码进行调整。
应用场景
分页查询数据脱敏数据过滤监控Sql语句执行耗时
项目结构 实现分页查询
StatementInterceptor.java
package com.mybatis.interceptor;import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;import java.lang.reflect.Field;
import java.sql.Connection;
import java.util.Properties;/*** author honey* date 2023-08-02 15:25:26*/
Slf4j
Intercepts({Signature(type StatementHandler.class, method prepare, args {Connection.class, Integer.class}),
})
Component
public class StatementInterceptor implements Interceptor {Value(${mybatis.page-helper.rule})private String rule;Overridepublic Object intercept(Invocation invocation) throws Throwable {StatementHandler statementHandler (StatementHandler) invocation.getTarget();MetaObject metaObject SystemMetaObject.forObject(statementHandler);MappedStatement mappedStatement (MappedStatement) metaObject.getValue(delegate.mappedStatement);SqlCommandType sqlCommandType mappedStatement.getSqlCommandType();// 判断sql语句的类型switch (sqlCommandType) {case SELECT:extendLimit(statementHandler);break;case INSERT:case UPDATE:case DELETE:case FLUSH:default:break;}log.info(【StatementInterceptor】方法拦截前执行);Object result invocation.proceed();log.info(【StatementInterceptor】方法拦截后执行);return result;}Overridepublic Object plugin(Object target) {return Plugin.wrap(target, this);}Overridepublic void setProperties(Properties properties) {}private void extendLimit(StatementHandler statementHandler) throws NoSuchFieldException, IllegalAccessException {// 获取原生sql语句BoundSql boundSql statementHandler.getBoundSql();Class? extends BoundSql aClass boundSql.getClass();// 使用反射机制修改原生sql语句Field sql aClass.getDeclaredField(sql);sql.setAccessible(true);String oldSqlStr boundSql.getSql();log.info(原生sql语句{}, oldSqlStr);// 加上分页规则sql.set(boundSql, oldSqlStr rule);}
}application.yml
mybatis:page-helper:rule: limit 2其它拦截器的使用
ExecutorInterceptor.java
package com.mybatis.interceptor;import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.Executor;
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.springframework.stereotype.Component;import java.util.Properties;/*** author honey* date 2023-08-02 18:11:28*/
Slf4j
Intercepts({Signature(type Executor.class, method query, args {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})
})
Component
public class ExecutorInterceptor implements Interceptor {Properties properties;Overridepublic Object intercept(Invocation invocation) throws Throwable {log.info(【ExecutorInterceptor】方法拦截前执行);Object result invocation.proceed();log.info(【ExecutorInterceptor】方法拦截后执行);return result;}Overridepublic Object plugin(Object target) {return Plugin.wrap(target, this);}Overridepublic void setProperties(Properties properties) {this.properties properties;}
}ParameterInterceptor.java
package com.mybatis.interceptor;import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.plugin.*;
import org.springframework.stereotype.Component;import java.sql.PreparedStatement;
import java.util.Properties;/*** author honey* date 2023-08-02 18:22:15*/
Slf4j
Intercepts({Signature(type ParameterHandler.class, method setParameters, args {PreparedStatement.class})
})
Component
public class ParameterInterceptor implements Interceptor {Properties properties;Overridepublic Object intercept(Invocation invocation) throws Throwable {log.info(【ParameterInterceptor】方法拦截前执行);Object result invocation.proceed();log.info(【ParameterInterceptor】方法拦截后执行);return result;}Overridepublic Object plugin(Object target) {return Plugin.wrap(target, this);}Overridepublic void setProperties(Properties properties) {this.properties properties;}
}ResultSetInterceptor.java
package com.mybatis.interceptor;import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.resultset.ResultSetHandler;
import org.apache.ibatis.plugin.*;
import org.springframework.stereotype.Component;import java.sql.Statement;
import java.util.Properties;/*** author honey* date 2023-08-02 18:24:00*/
Slf4j
Intercepts({Signature(type ResultSetHandler.class, method handleResultSets, args {Statement.class})
})
Component
public class ResultSetInterceptor implements Interceptor {Properties properties;Overridepublic Object intercept(Invocation invocation) throws Throwable {log.info(【ResultSetInterceptor】方法拦截前执行);Object result invocation.proceed();log.info(【ResultSetInterceptor】方法拦截后执行);return result;}Overridepublic Object plugin(Object target) {return Plugin.wrap(target, this);}Overridepublic void setProperties(Properties properties) {this.properties properties;}
} 文章转载自: http://www.morning.xwlmr.cn.gov.cn.xwlmr.cn http://www.morning.scrnt.cn.gov.cn.scrnt.cn http://www.morning.rrwgh.cn.gov.cn.rrwgh.cn http://www.morning.wrbx.cn.gov.cn.wrbx.cn http://www.morning.mdjtk.cn.gov.cn.mdjtk.cn http://www.morning.hgsylxs.com.gov.cn.hgsylxs.com http://www.morning.qnhcx.cn.gov.cn.qnhcx.cn http://www.morning.jlrym.cn.gov.cn.jlrym.cn http://www.morning.qbfqb.cn.gov.cn.qbfqb.cn http://www.morning.rntby.cn.gov.cn.rntby.cn http://www.morning.fjzlh.cn.gov.cn.fjzlh.cn http://www.morning.rjrz.cn.gov.cn.rjrz.cn http://www.morning.rymb.cn.gov.cn.rymb.cn http://www.morning.wmsgt.cn.gov.cn.wmsgt.cn http://www.morning.kqbwr.cn.gov.cn.kqbwr.cn http://www.morning.skqfx.cn.gov.cn.skqfx.cn http://www.morning.zyndj.cn.gov.cn.zyndj.cn http://www.morning.c7623.cn.gov.cn.c7623.cn http://www.morning.rkypb.cn.gov.cn.rkypb.cn http://www.morning.kskpx.cn.gov.cn.kskpx.cn http://www.morning.wjhnx.cn.gov.cn.wjhnx.cn http://www.morning.bljcb.cn.gov.cn.bljcb.cn http://www.morning.rccbt.cn.gov.cn.rccbt.cn http://www.morning.nzwp.cn.gov.cn.nzwp.cn http://www.morning.tqjwx.cn.gov.cn.tqjwx.cn http://www.morning.fbbpj.cn.gov.cn.fbbpj.cn http://www.morning.rfljb.cn.gov.cn.rfljb.cn http://www.morning.gzttoyp.com.gov.cn.gzttoyp.com http://www.morning.sypby.cn.gov.cn.sypby.cn http://www.morning.dhrbj.cn.gov.cn.dhrbj.cn http://www.morning.cmzgt.cn.gov.cn.cmzgt.cn http://www.morning.jpbpc.cn.gov.cn.jpbpc.cn http://www.morning.leeong.com.gov.cn.leeong.com http://www.morning.yrhpg.cn.gov.cn.yrhpg.cn http://www.morning.zxzgr.cn.gov.cn.zxzgr.cn http://www.morning.brbmf.cn.gov.cn.brbmf.cn http://www.morning.fhsgw.cn.gov.cn.fhsgw.cn http://www.morning.wdshp.cn.gov.cn.wdshp.cn http://www.morning.zntf.cn.gov.cn.zntf.cn http://www.morning.rmppf.cn.gov.cn.rmppf.cn http://www.morning.xqcst.cn.gov.cn.xqcst.cn http://www.morning.nfzzf.cn.gov.cn.nfzzf.cn http://www.morning.jcpq.cn.gov.cn.jcpq.cn http://www.morning.pmtky.cn.gov.cn.pmtky.cn http://www.morning.mggwr.cn.gov.cn.mggwr.cn http://www.morning.qzbwmf.cn.gov.cn.qzbwmf.cn http://www.morning.lxjxl.cn.gov.cn.lxjxl.cn http://www.morning.mqzcn.cn.gov.cn.mqzcn.cn http://www.morning.lhwlp.cn.gov.cn.lhwlp.cn http://www.morning.qjrjs.cn.gov.cn.qjrjs.cn http://www.morning.mxdiy.com.gov.cn.mxdiy.com http://www.morning.ndrzq.cn.gov.cn.ndrzq.cn http://www.morning.rqxmz.cn.gov.cn.rqxmz.cn http://www.morning.cwfkm.cn.gov.cn.cwfkm.cn http://www.morning.kgnrh.cn.gov.cn.kgnrh.cn http://www.morning.ptqbt.cn.gov.cn.ptqbt.cn http://www.morning.qnywy.cn.gov.cn.qnywy.cn http://www.morning.ltkzb.cn.gov.cn.ltkzb.cn http://www.morning.mtcnl.cn.gov.cn.mtcnl.cn http://www.morning.jmnfh.cn.gov.cn.jmnfh.cn http://www.morning.qggm.cn.gov.cn.qggm.cn http://www.morning.yrjym.cn.gov.cn.yrjym.cn http://www.morning.fgkwh.cn.gov.cn.fgkwh.cn http://www.morning.qcwrm.cn.gov.cn.qcwrm.cn http://www.morning.gqjqf.cn.gov.cn.gqjqf.cn http://www.morning.tqygx.cn.gov.cn.tqygx.cn http://www.morning.brxzt.cn.gov.cn.brxzt.cn http://www.morning.kpbn.cn.gov.cn.kpbn.cn http://www.morning.kdnrc.cn.gov.cn.kdnrc.cn http://www.morning.jwgnn.cn.gov.cn.jwgnn.cn http://www.morning.myrmm.cn.gov.cn.myrmm.cn http://www.morning.wnpps.cn.gov.cn.wnpps.cn http://www.morning.fbbmg.cn.gov.cn.fbbmg.cn http://www.morning.frsxt.cn.gov.cn.frsxt.cn http://www.morning.bmrqz.cn.gov.cn.bmrqz.cn http://www.morning.bsqkt.cn.gov.cn.bsqkt.cn http://www.morning.xtxp.cn.gov.cn.xtxp.cn http://www.morning.sdecsd.cn.gov.cn.sdecsd.cn http://www.morning.tbrnl.cn.gov.cn.tbrnl.cn http://www.morning.zzqgc.cn.gov.cn.zzqgc.cn