• Mybatis-Plus实现动态表名sql解析器


    Mybatis-Plus实现动态表名sql解析器

    在实现动态表名sql解析之前我们先配置动态表名的创建

    动态表名的创建

    配置mapper

    1 public interface SysTestMapper extends BaseMapper<SysTest> {
    2 
    3     void createTable(@Param("tableName") String tableName);
    4 
    5 }

    配置mapper.xml

     1 <mapper namespace="com.demo.studynew.mapper.SysTestMapper">
     2 
     3     <update id="createTable" parameterType="String">
     4         CREATE TABLE ${tableName} (
     5           `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
     6           `name` varchar(20) DEFAULT NULL,
     7           `gmt_create` datetime DEFAULT NULL,
     8           PRIMARY KEY (`id`)
     9         ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
    10   </update>
    11 
    12 </mapper>

    测试

    1 @Test
    2 void testCreateTable(){
    3      sysTestMapper.createTable("sys_test_2019");
    4 }

    至此动态表名的创建配置完成。

    动态表名sql解析

    添加依赖

     1 <dependency>
     2    <groupId>com.baomidou</groupId>
     3     <artifactId>mybatis-plus-boot-starter</artifactId>
     4     <version>3.4.0</version>
     5 </dependency>
     6 
     7 <dependency>
     8     <groupId>mysql</groupId>
     9     <artifactId>mysql-connector-java</artifactId>
    10 </dependency>

    配置yml

     1 spring:
     2   servlet:
     3     multipart:
     4       max-file-size: 50MB
     5       max-request-size: 100MB
     6   datasource:
     7     username: root
     8     password: 123456
     9     url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF8&useSSL=false
    10     driver-class-name: com.mysql.jdbc.Driver
    11 file:
    12   path: /Users/jisen/Desktop
    13   folder: /files/
    14 
    15 ## mybatis-plus
    16 mybatis-plus:
    17   mapper-locations: classpath*:mapper/**/*.xml
    18   configuration:
    19     map-underscore-to-camel-case: true
    20   global-config:
    21     db-config:
    22       update-strategy: ignored

    编写sql解析器

     1 package com.demo.studynew;
     2 
     3 import com.baomidou.mybatisplus.annotation.DbType;
     4 import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
     5 import com.baomidou.mybatisplus.extension.plugins.handler.TableNameHandler;
     6 import com.baomidou.mybatisplus.extension.plugins.inner.DynamicTableNameInnerInterceptor;
     7 import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
     8 import org.mybatis.spring.annotation.MapperScan;
     9 import org.springframework.context.annotation.Bean;
    10 import org.springframework.context.annotation.Configuration;
    11 
    12 import java.util.HashMap;
    13 import java.util.Map;
    14 
    15 
    16 @Configuration
    17 @MapperScan("com.demo.studynew.**")
    18 public class MybatisPlusConfig {
    19 
    20     public static ThreadLocal<String> TABLE_NAME = new ThreadLocal<String>();
    21 
    22     /**
    23      * 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
    24      */
    25     @Bean
    26     public MybatisPlusInterceptor mybatisPlusInterceptor() {
    27         MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
    28         PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);
    29         interceptor.addInnerInterceptor(paginationInnerInterceptor);
    30         DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();
    31         Map<String, TableNameHandler> tableNameHandlerMap = new HashMap<String, TableNameHandler>();
    32         tableNameHandlerMap.put("sys_test", new TableNameHandler() {
    33             @Override
    34             public String dynamicTableName(String sql, String tableName) {
    35                 return TABLE_NAME.get();
    36             }
    37         });
    38         dynamicTableNameInnerInterceptor.setTableNameHandlerMap(tableNameHandlerMap);
    39         interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);
    40         return interceptor;
    41     }
    42 }

    测试

     1 package com.demo.studynew;
     2 
     3 import com.demo.studynew.entity.SysTest;
     4 import io.swagger.annotations.Api;
     5 import io.swagger.annotations.ApiOperation;
     6 import org.springframework.beans.factory.annotation.Autowired;
     7 import org.springframework.web.bind.annotation.GetMapping;
     8 import org.springframework.web.bind.annotation.RestController;
     9 
    10 import java.time.LocalDateTime;
    11 import java.util.List;
    12 
    13 
    14 @RestController
    15 @Api(tags = "动态表名测试")
    16 public class SysTestController {
    17 
    18     @Autowired
    19     private SysTestMapper sysTestMapper;
    20 
    21     @GetMapping("list")
    22     @ApiOperation("列表")
    23     public List<SysTest> list() {
    24         MybatisPlusConfig.TABLE_NAME.set("sys_test_2020");
    25         return sysTestMapper.selectList(null);
    26     }
    27 
    28     @GetMapping("add")
    29     @ApiOperation("新增")
    30     public String add(){
    31         MybatisPlusConfig.TABLE_NAME.set("sys_test_2020");
    32         SysTest sysTest = new SysTest();
    33         sysTest.setName("test1");
    34         sysTest.setGmtCreate(LocalDateTime.now());
    35         sysTestMapper.insert(sysTest);
    36         return "ok";
    37     }
    38 }

    至此动态表名sql解析完成。

    参考:

    https://blog.csdn.net/qq_21896123/article/details/108437839

    https://blog.csdn.net/dothetrick/article/details/113152874

  • 相关阅读:
    异步CTP(Async CTP)为什么那样工作?
    Async Console Programs 异步控制台程序
    Async and Await 异步和等待
    OpenJudge计算概论-自整除数
    OpenJudge计算概论-整数的个数
    OpenJudge计算概论-找和为K的两个元素
    OpenJudge计算概论-找出第k大的数
    OpenJudge计算概论-异常细胞检测
    OpenJudge计算概论-数组逆序重放
    OpenJudge计算概论-数字求和
  • 原文地址:https://www.cnblogs.com/116970u/p/14342452.html
Copyright © 2020-2023  润新知