Search in sources :

Example 1 with DynamicTableNameInnerInterceptor

use of com.baomidou.mybatisplus.extension.plugins.inner.DynamicTableNameInnerInterceptor in project onex-boot by zhangchaoxu.

the class BaseMybatisPlusConfig method initDynamicTableNameInnerInterceptor.

/**
 * 初始化动态表名拦截器
 */
protected InnerInterceptor initDynamicTableNameInnerInterceptor() {
    DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();
    dynamicTableNameInnerInterceptor.setTableNameHandler((sql, tableName) -> {
        switch(tableName) {
            /*case xx:
                    return tableName + "_" + tableNameLocal.get();*/
            default:
                return tableName;
        }
    });
    return dynamicTableNameInnerInterceptor;
}
Also used : DynamicTableNameInnerInterceptor(com.baomidou.mybatisplus.extension.plugins.inner.DynamicTableNameInnerInterceptor)

Example 2 with DynamicTableNameInnerInterceptor

use of com.baomidou.mybatisplus.extension.plugins.inner.DynamicTableNameInnerInterceptor in project mybatis-plus-samples by baomidou.

the class MybatisPlusConfig method mybatisPlusInterceptor.

@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
    MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
    DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();
    dynamicTableNameInnerInterceptor.setTableNameHandler((sql, tableName) -> {
        // 获取参数方法
        Map<String, Object> paramMap = RequestDataHelper.getRequestData();
        paramMap.forEach((k, v) -> System.err.println(k + "----" + v));
        String year = "_2018";
        int random = new Random().nextInt(10);
        if (random % 2 == 1) {
            year = "_2019";
        }
        return tableName + year;
    });
    interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor);
    // dynamicTableNameInnerInterceptor.setTableNameHandlerMap(map);
    return interceptor;
}
Also used : DynamicTableNameInnerInterceptor(com.baomidou.mybatisplus.extension.plugins.inner.DynamicTableNameInnerInterceptor) Random(java.util.Random) MybatisPlusInterceptor(com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor) Bean(org.springframework.context.annotation.Bean)

Example 3 with DynamicTableNameInnerInterceptor

use of com.baomidou.mybatisplus.extension.plugins.inner.DynamicTableNameInnerInterceptor in project helio-starters by uncarbon97.

the class TenantTableSupport method support.

@Override
public void support(HelioProperties helioProperties, MybatisPlusInterceptor interceptor) {
    Collection<String> ignoredTables = helioProperties.getTenant().getIgnoredTables();
    DynamicTableNameInnerInterceptor innerInterceptor = new DynamicTableNameInnerInterceptor();
    innerInterceptor.setTableNameHandler(new HelioTableTenantHandler(ignoredTables));
    // 添加表级租户内联拦截器
    interceptor.addInnerInterceptor(innerInterceptor);
    log.info("\n\n[多租户支持] >> 隔离级别: 表级,以下数据表不参与租户隔离: {}\n", ignoredTables);
}
Also used : DynamicTableNameInnerInterceptor(com.baomidou.mybatisplus.extension.plugins.inner.DynamicTableNameInnerInterceptor)

Aggregations

DynamicTableNameInnerInterceptor (com.baomidou.mybatisplus.extension.plugins.inner.DynamicTableNameInnerInterceptor)3 MybatisPlusInterceptor (com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor)1 Random (java.util.Random)1 Bean (org.springframework.context.annotation.Bean)1