//类1
package com.mk.config;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import javax.annotation.PostConstruct;
import java.util.List;
@Configuration
@Order
public class MybatisInterceptorAutoConfiguration {
@Autowired
private List<SqlSessionFactory> sqlSessionFactories;
/**
* 拦截器
*/
@PostConstruct
public void addInterceptor(){
SqlQueryInterceptor sqlQueryInterceptor = new SqlQueryInterceptor();
for(SqlSessionFactory factory :sqlSessionFactories){
org.apache.ibatis.session.Configuration configuration = factory.getConfiguration();
configuration.addInterceptor(sqlQueryInterceptor);
}
}
}
// 类2
package com.mk.config;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.plugin.*;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.util.Properties;
/**
* @author
* @desc sql查询拦截器
* @since 2023-05-15 15:46
*/
@Intercepts(
{@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})}
)
@Slf4j
@AllArgsConstructor
public class SqlQueryInterceptor implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
Object parameterObject = statementHandler.getParameterHandler().getParameterObject();
JSONObject paramObject = JSONObject.parseObject(JSON.toJSONString(parameterObject));
String originalSql = statementHandler.getBoundSql().getSql();
String modifiedSql = modifySql(originalSql,paramObject);
Field field = BoundSql.class.getDeclaredField("sql");
field.setAccessible(true);
field.set(statementHandler.getBoundSql(), modifiedSql);
return invocation.proceed();
}
private String modifySql(String originalSql,JSONObject paramObject) {
String tableName="";
String filedName="";
String filedCode="";
if (ObjectUtil.isNotNull(paramObject)){
tableName = paramObject.getString("tablename");
filedName = paramObject.getString("filedName");
filedCode = paramObject.getString("filedCode");
}
if (StringUtils.isNotEmpty(tableName)){
originalSql = originalSql.replace("@{tablename}", tableName);
}
if (StringUtils.isNotEmpty(filedName)){
originalSql = originalSql.replace("@{filedName}", filedName);
}
if (StringUtils.isNotEmpty(filedCode)){
originalSql = originalSql.replace("@{filedCode}", filedCode);
}
return originalSql;
}
@Override
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}
@Override
public void setProperties(Properties properties) {
// 可以接收配置文件中的属性
}
}
mybatis拦截器进行处理动态表名的问题
最新推荐文章于 2024-05-21 19:25:47 发布
![](https://img-home.csdnimg.cn/images/20240611030827.png)