• mybatis动态表名


    1.基于mybatis官方文档

    @Configuration
    public class MybatisPlusConfig {
    
        @Bean
        public MybatisPlusInterceptor mybatisPlusInterceptor() {
            MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
            DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();
            dynamicTableNameInnerInterceptor.setTableNameHandler((sql, tableName) -> {
                // 获取参数方法
                Map<String, Object> paramMap = RequestDataHelper.getRequestData();
                paramMap.forEach((k, v) -> System.err.println(k + "----" + v));
    
                String year = "_2018";
                int random = new Random().nextInt(10);
                if (random % 2 == 1) {
                    year = "_2019";
                }
                return tableName + year;
            });
            interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);
            // 3.4.3.2 作废该方式
            // dynamicTableNameInnerInterceptor.setTableNameHandlerMap(map);
            return interceptor;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    /**
     * 请求参数传递辅助类
     */
    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();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    2. 使用参数的方式

    @Mapper
    public interface LogMapper extends BaseMapper<LogEntity> {
        List<LogEntity> queryAll(@Param("tableName")String tableName);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    
    DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <mapper namespace="com.zhubayi.dynatable.mapper.LogMapper">
        
        <select id="queryAll" resultType="com.zhubayi.dynatable.entity.LogEntity">
            select * from ${tableName};
        select>
    mapper>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
  • 相关阅读:
    sealos爆火后依然要开发sealer,背后原因有哪些?
    js中的promise函数(ES6)
    P18 JMenuBar菜单栏
    UPC2022暑期个人训练赛第23场(Credit Card Payment)
    Flip技术
    Curator实现zookeeper分布式锁
    iNFTnews | NFT在门票领域会有哪些作用?
    [NOI2022] 众数 题解
    《数据结构、算法与应用C++语言描述》使用C++语言实现数组队列
    Pytorch学习——Pytorch的入门操作 02(未完)
  • 原文地址:https://blog.csdn.net/qq_44732146/article/details/134215568