MybatisPlusAutoConfigure.java 2.3 KB
Newer Older
1 2
package com.central.db.config;

3
import com.baomidou.mybatisplus.annotation.DbType;
4
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
5 6 7
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
8
import com.central.common.properties.TenantProperties;
9
import com.central.db.interceptor.CustomTenantInterceptor;
10 11 12 13 14 15 16 17 18 19 20 21 22
import com.central.db.properties.MybatisPlusAutoFillProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

/**
 * mybatis-plus自动配置
 *
 * @author zlt
 * @date 2020/4/5
 * <p>
23
 * Blog: https://zlt2000.gitee.io
24 25 26 27 28
 * Github: https://github.com/zlt2000
 */
@EnableConfigurationProperties(MybatisPlusAutoFillProperties.class)
public class MybatisPlusAutoConfigure {
    @Autowired
29
    private TenantLineHandler tenantLineHandler;
30 31 32 33 34 35 36 37 38 39 40

    @Autowired
    private TenantProperties tenantProperties;

    @Autowired
    private MybatisPlusAutoFillProperties autoFillProperties;

    /**
     * 分页插件,自动识别数据库类型
     */
    @Bean
41 42
    public MybatisPlusInterceptor paginationInterceptor() {
        MybatisPlusInterceptor mpInterceptor = new MybatisPlusInterceptor();
43 44 45
        boolean enableTenant = tenantProperties.getEnable();
        //是否开启多租户隔离
        if (enableTenant) {
46 47 48
            CustomTenantInterceptor tenantInterceptor = new CustomTenantInterceptor(
                    tenantLineHandler, tenantProperties.getIgnoreSqls());
            mpInterceptor.addInnerInterceptor(tenantInterceptor);
49
        }
Z
zhult13 已提交
50
        mpInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
51
        return mpInterceptor;
52 53 54 55 56 57 58 59 60
    }

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(prefix = "zlt.mybatis-plus.auto-fill", name = "enabled", havingValue = "true", matchIfMissing = true)
    public MetaObjectHandler metaObjectHandler() {
        return new DateMetaObjectHandler(autoFillProperties);
    }
}