mybatis拦截器进行处理动态表名的问题

本文介绍了如何在SpringBoot项目中使用Mybatis的SqlQueryInterceptor对SQL查询进行动态替换,通过JSONObject解析参数对象并修改原SQL语句。
摘要由CSDN通过智能技术生成
//类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) {
        // 可以接收配置文件中的属性
    }

}


  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值