mybatis攔截器攔截sql 並對sql進行修改
由於項目中需要對數據做權限控制。
涉及要改動的sql非常多
所有需要攔截sql,找尋統一的規則修改sql,獲取到想要的結果。
因此想到了用mybatis的攔截器。
使用:
定義一個類實現接口Interceptor(mybatis中的),重寫三個方法,並交由spring容器管理@Component
1.攔截StatementHandler(方式1)
在intercept方法中定義sql的修改邏輯
package com.sheep.mybatisplus.config;
import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.executor.BaseExecutor;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.reflection.DefaultReflectorFactory;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.Statement;
import java.util.Properties;
@Component
// 標志該類是一個攔截器
//@Intercepts({
// //指明該攔截器需要攔截哪一個接口的哪一個方法
// @Signature(
// type = Executor.class, // 四種類型接口中的某一個接口,如Executor.class。
// method = "query", //對應接口中的某一個方法名,比如Executor的query方法。
// //對應接口中的某一個方法的參數,比如Executor中query方法因為重載原因,有多個,args就是指明參數類型,從而確定是具體哪一個方法。
// args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}
// ),
// @Signature(
// type = Executor.class, // 四種類型接口中的某一個接口,如Executor.class。
// method = "query", //對應接口中的某一個方法名,比如Executor的query方法。
// //對應接口中的某一個方法的參數,比如Executor中query方法因為重載原因,有多個,args就是指明參數類型,從而確定是具體哪一個方法。
// args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}
// )})
@Intercepts({@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})})
public class ExamplePlugin implements Interceptor {
/**
* 進行攔截的時候要執行的方法
* @param invocation
* @return
* @throws Throwable
*/
public Object intercept(Invocation invocation) throws Throwable {
System.out.println("====intercept======");
System.out.println("====intercept======");
System.out.println("====intercept======");
System.out.println("====intercept======");
System.out.println("====intercept======");
StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
//通過MetaObject優雅訪問對象的屬性,這里是訪問statementHandler的屬性;:MetaObject是Mybatis提供的一個用於方便、
//優雅訪問對象屬性的對象,通過它可以簡化代碼、不需要try/catch各種reflect異常,同時它支持對JavaBean、Collection、Map三種類型對象的操作。
MetaObject metaObject = MetaObject
.forObject(statementHandler, SystemMetaObject.DEFAULT_OBJECT_FACTORY, SystemMetaObject.DEFAULT_OBJECT_WRAPPER_FACTORY,
new DefaultReflectorFactory());
//先攔截到RoutingStatementHandler,里面有個StatementHandler類型的delegate變量,其實現類是BaseStatementHandler,然后就到BaseStatementHandler的成員變量mappedStatement
MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");
//id為執行的mapper方法的全路徑名,如com.uv.dao.UserMapper.insertUser
String id = mappedStatement.getId();
//sql語句類型 select、delete、insert、update
String sqlCommandType = mappedStatement.getSqlCommandType().toString();
//數據庫連接信息
// Configuration configuration = mappedStatement.getConfiguration();
// ComboPooledDataSource dataSource = (ComboPooledDataSource)configuration.getEnvironment().getDataSource();
// dataSource.getJdbcUrl();
BoundSql boundSql = statementHandler.getBoundSql();
//獲取到原始sql語句
String sql = boundSql.getSql();
String mSql = "select * from user where email like concat('%',?,'%')";
//通過反射修改sql語句
Field field = boundSql.getClass().getDeclaredField("sql");
field.setAccessible(true);
field.set(boundSql, mSql);
//執行結果
return invocation.proceed();
}
public Object plugin(Object target) {
System.out.println("-----------------------------plugin-------------------------");
return Plugin.wrap(target, this);
}
public void setProperties(Properties properties) {
System.out.println("====setProperties======");
}
}
2,攔截Executor(方式2)
模擬pageHelper插件 底層的 PageInterceptor攔截器寫的 (PageInterceptor也是實現的mybatis的interceptor接口)
package com.sheep.mybatisplus.config;
import com.github.pagehelper.PageInterceptor;
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.springframework.beans.factory.annotation.Autowired;
import java.lang.reflect.Field;
import java.util.Properties;
/**
* mubatis攔截器 攔截sql的執行
*/
//@Component
@Intercepts(
{
@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}),
@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}),
}
)
public class MybatisInterceptor implements Interceptor {
/**
* 進行攔截的時候要執行的方法
*
* @param invocation
* @return
* @throws Throwable
*/
@Override
public Object intercept(Invocation invocation) throws Throwable {
System.out.println("====intercept======");
Object[] args = invocation.getArgs();
MappedStatement mappedStatement = (MappedStatement) args[0];
Object parameter = args[1];
RowBounds rowBounds = (RowBounds) args[2];
ResultHandler resultHandler = (ResultHandler) args[3];
Executor executor = (Executor) invocation.getTarget();
CacheKey cacheKey;
BoundSql boundSql;
//由於邏輯關系,只會進入一次
if (args.length == 4) {
//4 個參數時
boundSql = mappedStatement.getBoundSql(parameter);
cacheKey = executor.createCacheKey(mappedStatement, parameter, rowBounds, boundSql);
} else {
//6 個參數時
cacheKey = (CacheKey) args[4];
boundSql = (BoundSql) args[5];
}
//id為執行的mapper方法的全路徑名,如com.metro.dao.UserMapper.insertUser
String id = mappedStatement.getId();
//獲取到原始sql語句
String sql = boundSql.getSql();
String newSql = "$#####";//修改后的sql
//通過反射修改boundSql對象的sql語句
Field field = boundSql.getClass().getDeclaredField("sql");
field.setAccessible(true);
field.set(boundSql, newSql);
//執行修改后的sql語句
return executor.query(mappedStatement, parameter, rowBounds, resultHandler, cacheKey, boundSql);
//執行結果
//return invocation.proceed();
}
@Override
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}
@Override
public void setProperties(Properties properties) {
}
}