首页 > 其他分享 >mybatis-plus动态表名插件

mybatis-plus动态表名插件

时间:2023-03-13 21:03:05浏览次数:40  
标签:插件 String data table plus 表名 mybatis import com

mybatis-plus动态表名插件

一、书写动态表名处理器

TableDataDynamicTableNameHandler.java

package com.wangfan.conf;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.baomidou.mybatisplus.extension.plugins.handler.TableNameHandler;

public class TableDataDynamicTableNameHandler implements TableNameHandler {

    /**
     * 生成动态表名
     *
     * @param sql       当前执行 SQL
     * @param tableName 表名
     * @return String
     */
    public String dynamicTableName(String sql, String tableName) {
        String realTableName = getTableDataRealTableName(sql, tableName);
        if(realTableName == null || realTableName.equals("")) {
            return tableName;
        }
        return realTableName;
    }

    public String getTableDataRealTableName(String sql, String tableName) {
        //获取SQL语句 如果表名为table_data则获取SQL语句SELECT id,name FROM user_0 /*table_data=user_0 */中的注释语句/*table_data=user_0 */获取到注释中的中的真实表名并返回
        //这里是完全替换原表,如果在原表名的可以按需求修改即可
        if(tableName.equals("table_data")) {
            //正则表达式匹配并获取真实表名
            Pattern pattern2 = Pattern.compile("table_data=([\\s\\S]*?) " );
            Matcher matcher2 = pattern2.matcher(sql);
            while (matcher2.find()) {
                String group = matcher2.group();
                return group.replace("table_data=", "").replace(" ", "");
            }
        }
        return null;
    }
}

二、MyBatis-plus配置类添加动态表名插件

package com.wangfan.conf;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.TableNameHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.DynamicTableNameInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Configuration
public class MdmMybatisPlusConfig {
   //添加动态表名插件
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();
        TableNameHandler tableNameHandler = new TableDataDynamicTableNameHandler();
        dynamicTableNameInnerInterceptor.setTableNameHandler(tableNameHandler);
        interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);

        return interceptor;
    }
}

三、测试

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wangfan.mapper.TableDataMapper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author wjj
 */
@RestController
public class TableDataController {

    @Resource
    private TableDataMapper tableDataMapper;

    @RequestMapping("/aa")
    public void aa(){
        QueryWrapper wrapper = new QueryWrapper();
        //注意table_data=%s后面加一个空格
        wrapper.comment(String.format("table_data=%s ","user_0"));
        wrapper.select("id","name");
        List list = tableDataMapper.selectMaps(wrapper);
        System.out.println(list);
    }
}

标签:插件,String,data,table,plus,表名,mybatis,import,com
From: https://www.cnblogs.com/WangJingjun/p/17212847.html

相关文章