MyBatis-Plus动态表名如何使用

发布时间:2023-04-07 11:41:07 作者:iii
来源:亿速云 阅读:121

本篇内容介绍了“MyBatis-Plus动态表名如何使用”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

MyBatis-Plus实现动态表名

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

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

MyBatis-Plus官网说明

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.yisu.com/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();
    }
}

使用

MyBatis-Plus动态表名如何使用

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动态表名如何使用

“MyBatis-Plus动态表名如何使用”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

推荐阅读:
  1. win10安全模式初始密码忘记了如何解决
  2. win11网络图标消失如何解决

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mybatis-plus

上一篇:Java内部类如何使用

下一篇:Vue开发技巧实例代码分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》