SpringBoot 와 MybatisPlus3. X 통합 의 동적 테이블 이름 SQL 해상도 기 (7)

pom.xml
<dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starterartifactId>
        dependency>
        <dependency>
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plus-boot-starterartifactId>
            <version>3.2.0version>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
        dependency>
        
        <dependency>
            <groupId>p6spygroupId>
            <artifactId>p6spyartifactId>
            <version>3.8.0version>
        dependency>
        <dependency>
            <groupId>com.h2databasegroupId>
            <artifactId>h2artifactId>
            <scope>runtimescope>
        dependency><dependency>
            <groupId>com.alibabagroupId>
            <artifactId>fastjsonartifactId>
            <version>1.2.49version>
            <scope>testscope>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

 
application.yml
spring:
  datasource:
    driver-class-name: com.p6spy.engine.spy.P6SpyDriver
    url: jdbc:p6spy:h2:tcp://192.168.180.115:19200/~/mem/test
    username: root
    password: test
​
# Logger Config
logging:
  level:
    com.mp.dynamic-tablename: debug

 
설정 클래스 는 ITableNameHandler 인 터 페 이 스 를 DynamicTable NameParser 프로세서 체인 에 주입 하고 동적 테이블 이름 해석 기 를 MP 분석 체인 에 주입 합 니 다.
@Configuration
@MapperScan("com.mq.dynamictablename.mapper")
public class MybatisPlusConfig {
​
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        DynamicTableNameParser dynamicTableNameParser = new DynamicTableNameParser();
        dynamicTableNameParser.setTableNameHandlerMap(new HashMap(2) {{
            put("user", (metaObject, sql, tableName) -> {
                // metaObject         ,            
                String year = "_2018";
                int random = new Random().nextInt(10);
                if (random % 2 == 1) {
                    year = "_2019";
                }
                return tableName + year;
            });
        }});
        paginationInterceptor.setSqlParserList(Collections.singletonList(dynamicTableNameParser));
        return paginationInterceptor;
    }
}

실체 류
@Data
@Accessors(chain = true)
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
​
}
public interface UserMapper extends BaseMapper {
​
}


데이터베이스 스 크 립 트
DELETE FROM user_2018;
​
INSERT INTO user_2018 (id, name, age, email) VALUES
(1, 'Jone', 18, '[email protected]');
​
DELETE FROM user_2019;
​
INSERT INTO user_2019 (id, name, age, email) VALUES
(1, 'Jack', 20, '[email protected]');
DROP TABLE IF EXISTS user_2018;
​
CREATE TABLE user_2018
(
    id BIGINT(20) NOT NULL COMMENT '  ID',
    name VARCHAR(30) NULL DEFAULT NULL COMMENT '  ',
    age INT(11) NULL DEFAULT NULL COMMENT '  ',
    email VARCHAR(50) NULL DEFAULT NULL COMMENT '  ',
    PRIMARY KEY (id)
);
​
DROP TABLE IF EXISTS user_2019;
​
CREATE TABLE user_2019
(
    id BIGINT(20) NOT NULL COMMENT '  ID',
    name VARCHAR(30) NULL DEFAULT NULL COMMENT '  ',
    age INT(11) NULL DEFAULT NULL COMMENT '  ',
    email VARCHAR(50) NULL DEFAULT NULL COMMENT '  ',
    PRIMARY KEY (id)
);

 
테스트 클래스
@SpringBootTest
class DynamicTablenameApplicationTests {
​
​
    @Autowired
    private UserMapper userMapper;
​
    @Test
    public void test() {
        //         SQL        user_2018  user_2019  
        for (int i = 0; i < 6; i++) {
            User user = userMapper.selectById(1);
            System.err.println(user.getName());
        }
    }
​
}


테스트 결과:
Consume Time:19 ms 2019-10-30 16:46:39
 Execute SQL:SELECT id,name,email,age FROM user_2018 WHERE id=1 
​
Jone
 Consume Time:2 ms 2019-10-30 16:46:39
 Execute SQL:SELECT id,name,email,age FROM user_2019 WHERE id=1 
​
Jack
 Consume Time:0 ms 2019-10-30 16:46:39
 Execute SQL:SELECT id,name,email,age FROM user_2019 WHERE id=1 
​
Jack
 Consume Time:0 ms 2019-10-30 16:46:39
 Execute SQL:SELECT id,name,email,age FROM user_2019 WHERE id=1 
​
Jack
 Consume Time:0 ms 2019-10-30 16:46:39
 Execute SQL:SELECT id,name,email,age FROM user_2018 WHERE id=1 
​
Jone
 Consume Time:0 ms 2019-10-30 16:46:39
 Execute SQL:SELECT id,name,email,age FROM user_2018 WHERE id=1 
​
Jone

좋은 웹페이지 즐겨찾기