MyBatis-Plus动态表名的使用

 更新时间:2023年04月07日 10:45:39   作者:不想做咸鱼的王富贵  
本文主要介绍了MyBatis-Plus动态表名的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

MyBatis-Plus实现动态表名

MyBatis实现方法如下现在要用MyBatis-Plus 实现

   <select id="getList" resultType="com.wys.entity.User">
        SELECT *
        FROM ${tableName}
    </select>

MyBatis-Plus官网说明

MyBatis-Plus版本

1、添加MyBatis-Plus依赖

<dependency>
	<groupId>com.baomidou</groupId>
	<artifactId>mybatis-plus-boot-starter</artifactId>
	<version>3.5.1</version>
</dependency>

MyBatis-Plus配置

2、添加MyBatis-Plus配置,利用拦截器获取到表名给替换

@Configuration
public class MybatisPlusConfig {
    static List<String> tableList(){
        List<String> tables = new ArrayList<>();
        //伪表名  可以为任意字符串 建议设置复杂度 避免重复    tables.add("C55EA8171877E962E08DFF63AA3678841");
        tables.add("TestUser");
        return tables;
    }

	//拦截器,获取到表名给替换
    @Bean
    public MybatisPlusInterceptor dynamicTableNameInnerInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();
        dynamicTableNameInnerInterceptor.setTableNameHandler((sql, tableName) -> {
            String newTable = null;
            for (String table : tableList()) {
                newTable = RequestDataHelper.getRequestData(table);
                if (table.equals(tableName) && newTable!=null){
                    tableName = newTable;
                    break;
                }
            }
            return tableName;
        });
        interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);
        return interceptor;
    }
}

如果上面的拦截器不生效可以使用下面这个https://www.jb51.net/article/280321.htm

@Configuration
@AutoConfigureAfter(PageHelperAutoConfiguration.class)
public class MybatisPlusConfig {
    static List<String> tableList(){
        List<String> tables = new ArrayList<>();
        //表名
        tables.add("C55EA8171877E962E08DFF63AA3678841");
        return tables;
    }

    //拦截器,获取到表名给替换
//    @Bean
//    public MybatisPlusInterceptor dynamicTableNameInnerInterceptor() {
//        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//        DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();
//        dynamicTableNameInnerInterceptor.setTableNameHandler((sql, tableName) -> {
//            String newTable = null;
//            for (String table : tableList()) {
//                newTable = RequestDataHelper.getRequestData(table);
//                if (table.equals(tableName) && newTable!=null){
//                    tableName = newTable;
//                    break;
//                }
//            }
//            return tableName;
//        });
//        interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);
//        return interceptor;
//    }

    @Autowired
    private List<SqlSessionFactory> sqlSessionFactoryList;

    @PostConstruct
    public void addMyInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();
        dynamicTableNameInnerInterceptor.setTableNameHandler((sql, tableName) -> {
            String newTable = null;
            for (String table : tableList()) {
                newTable = RequestDataHelper.getRequestData(table);
                if (table.equals(tableName) && newTable!=null){
                    tableName = newTable;
                    break;
                }
            }
            return tableName;
        });
        interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);
        for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
            sqlSessionFactory.getConfiguration().addInterceptor(interceptor);
        }
    }
}

请求参数传递辅助类

3、创建请求参数传递辅助类

public class RequestDataHelper {
    /**
     * 请求参数存取
     */
    private static final ThreadLocal<Map<String, Object>> REQUEST_DATA = new ThreadLocal<>();

    /**
     * 设置请求参数
     *
     * @param requestData 请求参数 MAP 对象
     */
    public static void setRequestData(Map<String, Object> requestData) {
        REQUEST_DATA.set(requestData);
    }

    /**
     * 获取请求参数
     *
     * @param param 请求参数
     * @return 请求参数 MAP 对象
     */
    public static <T> T getRequestData(String param) {
        Map<String, Object> dataMap = getRequestData();
        if (CollectionUtils.isNotEmpty(dataMap)) {
            return (T) dataMap.get(param);
        }
        return null;
    }

    /**
     * 获取请求参数
     *
     * @return 请求参数 MAP 对象
     */
    public static Map<String, Object> getRequestData() {
        return REQUEST_DATA.get();
    }
}

使用

4、在程序中使用,注意如果实际表名与实体类与不同,可先在实体类类注明表名@TableName(“TestUser”)

@GetMapping("/listUser")
    public void listUser(){

        RequestDataHelper.setRequestData(new HashMap<String, Object>() {{
            put("kfafkasfaskfasjfkasf", "user_2018");

        }});
        Integer age=2018;

       User user=new User();
        List list = userMapper.getList(user);
     //  User user_2019 = userMapper.findById("user_2019", 2019);

        System.out.println(list);
        System.out.println("-------------");
       // System.out.println(user_2019);
        RequestDataHelper.setRequestData(new HashMap<String, Object>() {{
            put("kfafkasfaskfasjfkasf", "user_2019");

        }});
        List lis2 = userMapper.getList(user);
        System.out.println(lis2);
        System.out.println("-------------");

    }

结果:

 到此这篇关于MyBatis-Plus动态表名的使用的文章就介绍到这了,更多相关MyBatis-Plus动态表名内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • JAVA 多线程编程之CountDownLatch使用详解

    JAVA 多线程编程之CountDownLatch使用详解

    当多个线程需要协调和同步执行任务时,Java中的CountDownLatch(倒计时门闩)是一个常用的工具类,本文将介绍 CountDownLatch 的基本原理、用法以及示例代码,需要的朋友可以参考下
    2023-05-05
  • Java手写持久层框架的详细代码

    Java手写持久层框架的详细代码

    这篇文章主要介绍了Java手写持久层框架,本文适合有一定java基础的同学,通过自定义持久层框架,可以更加清楚常用的mybatis等开源框架的原理,需要的朋友可以参考下
    2022-08-08
  • IDEA下lombok安装及找不到get,set的问题的解决方法

    IDEA下lombok安装及找不到get,set的问题的解决方法

    这篇文章主要介绍了IDEA下lombok安装及找不到get,set的问题的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-04-04
  • SpringBoot深入探究@Conditional条件装配的使用

    SpringBoot深入探究@Conditional条件装配的使用

    这篇文章主要为大家介绍了SpringBoot底层注解@Conditional的使用分析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-06-06
  • SpringBoot返回文件使前端下载的几种方式小结

    SpringBoot返回文件使前端下载的几种方式小结

    本文主要介绍了Spring Boot中几种文件下载的方法,通过后端应用下载文件并进行业务处理,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-11-11
  • 如何基于Spring使用工厂模式实现程序解耦

    如何基于Spring使用工厂模式实现程序解耦

    这篇文章主要介绍了如何基于Spring使用工厂模式实现程序解耦,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-12-12
  • SpringCloud OpenFeign与Ribbon客户端配置详解

    SpringCloud OpenFeign与Ribbon客户端配置详解

    在springcloud中,openfeign是取代了feign作为负载均衡组件的,feign最早是netflix提供的,他是一个轻量级的支持RESTful的http服务调用框架,内置了ribbon,而ribbon可以提供负载均衡机制,因此feign可以作为一个负载均衡的远程服务调用框架使用
    2022-11-11
  • 详解Spring中@Component和@Configuration的区别

    详解Spring中@Component和@Configuration的区别

    一直有同学搞不清Spring中@Component和@Configuration这两个注解有什么区别,所以这篇文章小编就给大家简单介绍一下@Component和@Configuration的区别,需要的朋友可以参考下
    2023-07-07
  • JAVA读取二进制文件以及画图教程

    JAVA读取二进制文件以及画图教程

    由于项目需要,需要对二进制文件进行读取,所以这篇文章主要给大家介绍了关于JAVA读取二进制文件以及画图的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2023-07-07
  • Java中的ThreadLocal详解

    Java中的ThreadLocal详解

    THreadLocalMap中的Entry的key使用的是ThreadLocal对象的弱引用,在没有其他地方对ThreadLoca依赖,ThreadLocalMap中的ThreadLocal对象就会被回收掉,但是对应的不会被回收,具体内容请和小编一起看下面文章详情
    2021-09-09

最新评论