攔截器類型
1.Executor (update, query, flushStatements, commit, rollback, getTransaction, close, isClosed) 攔截執(zhí)行器的方法
2.ParameterHandler (getParameterObject, setParameters) 攔截參數(shù)的處理
2.ResultSetHandler (handleResultSets, handleOutputParameters) 攔截結(jié)果集的處理
4.StatementHandler (prepare, parameterize, batch, update, query) 攔截Sql語法和會話構(gòu)建的處理
執(zhí)行順序:Executor => StatementHandler => ParameterHandler => ResultSetHandler
攔截器應用場景
StatementHandler可改寫sql 實現(xiàn)分頁查詢
ParameterHandler可改寫攔截參數(shù)的處理装畅,新增創(chuàng)建時間、創(chuàng)建人、更新時間等
ResultSetHandler數(shù)據(jù)庫結(jié)果二次加密返回客戶端,過濾掉審計字段秉溉、敏感字段
攔截器增加分頁功能樣例
@Intercepts({@Signature(type = StatementHandler.class, method = "prepare",
args = {Connection.class, Integer.class}),
})
@Component
@Slf4j
public class SqlInterceptor implements Interceptor {
@Value("${jarye.pagehelper.rule}")
private String pagehelperRule;
@Override
public Object intercept(Invocation invocation) throws Throwable {
StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
MetaObject metaObject = SystemMetaObject.forObject(statementHandler);
MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");
//sql類型
SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType();
switch (sqlCommandType) {
// 判斷sql語句是為查詢類型
case SELECT:
extendLimit(statementHandler);
break;
}
return invocation.proceed();
}
@Override
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}
@Override
public void setProperties(Properties properties) {
}
/**
* 對sql實現(xiàn) 修改 加上limit
*/
private void extendLimit(StatementHandler statementHandler) throws NoSuchFieldException, IllegalAccessException {
// 獲取到原生sql語句
BoundSql boundSql = statementHandler.getBoundSql();
Class<? extends BoundSql> aClass = boundSql.getClass();
// 使用反射機制修改原生sqk語句
Field sql = aClass.getDeclaredField("sql");
sql.setAccessible(true);
String oldSqlStr = boundSql.getSql();
// 后面加上 limit
sql.set(boundSql, oldSqlStr + " " + pagehelperRule);
}
}
攔截器修改參數(shù)樣例
@Intercepts({
@Signature(type = ParameterHandler.class, method = "setParameters", args = PreparedStatement.class)
})
@Slf4j
@Component
public class ParamInterceptor implements Interceptor {
private static final ObjectFactory DEFAULT_OBJECT_FACTORY = new DefaultObjectFactory();
private static final ObjectWrapperFactory DEFAULT_OBJECT_WRAPPER_FACTORY = new DefaultObjectWrapperFactory();
private static final ReflectorFactory REFLECTOR_FACTORY = new DefaultReflectorFactory();
@Override
public Object intercept(Invocation invocation) throws Throwable {
// 獲取攔截器攔截的設置參數(shù)對象DefaultParameterHandler
ParameterHandler parameterHandler = (ParameterHandler) invocation.getTarget();
// 通過mybatis的反射來獲取對應的值
MetaObject metaResultSetHandler = MetaObject.forObject(parameterHandler, DEFAULT_OBJECT_FACTORY, DEFAULT_OBJECT_WRAPPER_FACTORY, REFLECTOR_FACTORY);
MappedStatement mappedStatement = (MappedStatement) metaResultSetHandler.getValue("mappedStatement");
// 如果方法是無慘的堡掏,則parameterObject會存在空的情況
Object parameterObject = metaResultSetHandler.getValue("parameterObject");
//sql類型
SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType();
// 回寫parameterObject對象
metaResultSetHandler.setValue("parameterObject", updateInsertParam(sqlCommandType, parameterObject));
return invocation.proceed();
}
@Override
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}
@Override
public void setProperties(Properties properties) {
}
private Object updateInsertParam(SqlCommandType sqlCommandType, Object parameterObject) throws NoSuchFieldException, IllegalAccessException {
if(null==parameterObject){
return parameterObject;
}
Class<?> aClass = parameterObject.getClass();
switch (sqlCommandType) {
case INSERT:
// 使用反射獲取到createDatetime修改時間為當前系統(tǒng)時間
Field createDatetime = aClass.getSuperclass().getDeclaredField("createDatetime");
createDatetime.setAccessible(true);
createDatetime.set(parameterObject, new Date());
case UPDATE:
//updateDatetime字段 修改時間為當前系統(tǒng)時間
Field updateDatetime = aClass.getSuperclass().getDeclaredField("updateDatetime");
updateDatetime.setAccessible(true);
updateDatetime.set(parameterObject, new Date());
break;
}
return parameterObject;
}
}
推薦閱讀:
<<<Mybatis的整體執(zhí)行原理圖解
<<<SqlSessionFactory的創(chuàng)建過程原理
<<<SqlSession的創(chuàng)建過程
<<<sqlSession如何獲得具體的Mapper接口信息
<<<userMapper.getUser(1);底層實現(xiàn)原理
<<<sqlSession.selectOne底層實現(xiàn)原理
<<<Mybatis一級緩存知識匯總
<<<Mybatis二級緩存知識匯總
<<<Springboot整合Mybatis二級緩存
<<<Mybatis常見面試題