iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >MyBatis-Plus动态表名的使用
  • 287
分享到

MyBatis-Plus动态表名的使用

mybatis-plus动态表名 2023-05-14 20:05:32 287人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

目录mybatis-Plus版本MyBatis-Plus配置请求参数传递辅助类使用MyBatis-Plus实现动态表名 MyBatis实现方法如下现在要用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<>();

    
    public static void setRequestData(Map<String, Object> requestData) {
        REQUEST_DATA.set(requestData);
    }

    
    public static <T> T getRequestData(String param) {
        Map<String, Object> dataMap = getRequestData();
        if (CollectionUtils.isNotEmpty(dataMap)) {
            return (T) dataMap.get(param);
        }
        return null;
    }

    
    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动态表名内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: MyBatis-Plus动态表名的使用

本文链接: https://www.lsjlt.com/news/204596.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作