nacos+mybatisPlus 实现数据库sql语句动态表名替换

核心思想是使用mp的拦截器,在sql语句执行后,拿到sql语句,并进行表名校验,如果符合我们的需求就替换。

一、使用你自己服务的nacos的配置,或者如果所有的服务都有这个需求,最好放在共享配置文件application.yml里面

mdm:
  tablename:
    # 是否开启替换表名默认是false
    enabled: false
    # 旧表名前缀
    oldTableName: jdbc_orcale.user
    # 新表名前缀
    newTableName: jdbc_orcale.userTest

二、在自己微服务的核心配置模块,加上下面配置类

@Data
@Component
@ConfigurationProperties(prefix = "mdm.tablename")
public class ProTableNameProperties {
    /**
     * 是否开启替换表名
     */
    private Boolean enabled = false;

    /**
     * 要替换的新表名
     */
    private String newTableName = "jdbc_orcale.user";

    /**
     * 要替换的旧表名
     */
    private String oldTableName = "jdbc_orcale.userTest";
}

三、创建一个处理类,实现TableNameHandler,重写dynamicTableName方法

public class ClientProTableNameHandler implements TableNameHandler {

    private  String oldTableName;

    private  String newTableName;

    public ClientProTableNameHandler() {
    }

    public ClientProTableNameHandler(String oldTableName, String newTableName) {
        this.oldTableName = oldTableName;
        this.newTableName = newTableName;
    }
    @Override
    public String dynamicTableName(String sql, String tableName) {

        if (tableName.startsWith(oldTableName)) {
            // 获取表名前缀并替代
            return tableName.replace(oldTableName, newTableName);
        }else{
            return tableName;
        }
    }
}

四、在MybatisPlusConfig里面把上面的处理类添加到mp的拦截器

在这里需要注意,如果使用了分页插件,那分页插件需要在所有的插件最后面添加否则不生效

@Configuration
@EnableTransactionManagement
public class MybatisPlusConfig {

    @Autowired
    private ProTableNameProperties tableNameProperties;
    /**
     * 分页插件和动态替换表名前缀插件
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 是否加载动态替换表名前缀
        if(tableNameProperties.getEnabled()!=null&&tableNameProperties.getEnabled()) {
            DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();
            dynamicTableNameInnerInterceptor.setTableNameHandler(
                    //当匹配到对应的表名时根据动态设置的前缀来定位到对应的表
                    new ClientProTableNameHandler(tableNameProperties.getOldTableName(),tableNameProperties.getNewTableName())
            );
            interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);
        }
        //分页插件
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

}

五、总结

       因为是以mp插件的形式进行拦截sql并处理,这个插件的加载时机是在springboot启动初始化就完成了,所以即便我们使用了nacos,也无法热更新,所以每次修改nacos配置文件就需要重启一次服务

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刘个Java

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值