zoukankan      html  css  js  c++  java
  • mybtis plus 3.2.0 动态表名sql解析器的配置

    一.Configuration

    @Configuration
    public class MybatisPlusConfiguration {
    
        public static ThreadLocal<String> inputTableName = new ThreadLocal<>();
    
        @Bean
        public PaginationInterceptor paginationInterceptor(){
            PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
            List<ISqlParser> sqlParserList = new ArrayList<>();
            DynamicTableNameParser dynamicTableNameParser = new DynamicTableNameParser();
            HashMap<String, ITableNameHandler> map = new HashMap<>();
            map.put("user", (metaObject, sql, tableName) -> inputTableName.get());
            dynamicTableNameParser.setTableNameHandlerMap(map);
            sqlParserList.add(dynamicTableNameParser);
            paginationInterceptor.setSqlParserList(sqlParserList);
            paginationInterceptor.setSqlParserFilter(metaObject -> {
                MappedStatement statement = SqlParserHelper.getMappedStatement(metaObject);
            //如果是selectById方法,则表名不会被替换,其他的会被动态替换表名
    return "com.mp.dao.UserMapper.selectById".equals(statement.getId()); }); return paginationInterceptor; } }

    二.测试

    传入参数

        @Test
        public void testTableNAme(){
            MybatisPlusConfiguration.inputTableName.set("user_2020");
            userMapper.selectList(null);
        }

    三.运行结果

     可以观察到查询的表名已经被替换

  • 相关阅读:
    VMware安装虚拟机(Ubuntu)
    鼠标拖拽事件
    css层叠样式表
    html--form表单常用操作
    python学习之HTML-table标签
    python之web前端day01
    字符串各种操作,关于字符串的内置函数
    正则中匹配次数的问题
    re模块
    Github网站打不开的问题
  • 原文地址:https://www.cnblogs.com/wwjj4811/p/12617092.html
Copyright © 2011-2022 走看看