All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zodiac.autoconfigure.mybatisplus.MyBatisPlusConfigAutoConfiguration Maven / Gradle / Ivy

There is a newer version: 1.6.8
Show newest version
package org.zodiac.autoconfigure.mybatisplus;

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.core.annotation.Order;
import org.zodiac.commons.util.ArrayUtil;
import org.zodiac.core.data.ProtectFieldHandler;
import org.zodiac.sdk.toolkit.util.lang.StringUtil;

@SpringBootConfiguration
//@ConditionalOnSingleCandidate(value = DataSource.class)
//@AutoConfigureAfter(value = {com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration.class})
@ConditionalOnClass(value = {org.apache.ibatis.session.SqlSessionFactory.class, org.mybatis.spring.SqlSessionFactoryBean.class})
public class MyBatisPlusConfigAutoConfiguration {

    private com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties mybatisPlusProperties;

    public MyBatisPlusConfigAutoConfiguration(com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties mybatisPlusProperties) {
        this.mybatisPlusProperties = mybatisPlusProperties;
    }

    @Bean
    @ConfigurationProperties(prefix = org.zodiac.mybatisplus.constants.MyBatisPlusSystemPropertiesConstants.MYBATIS_PLUS_PREFIX, ignoreInvalidFields = true)
    protected MyBatisPlusConfigProperties myBatisPlusProperties(ObjectProvider dataSourcePropertiesProvider,
        ObjectProvider dynamicDataSourcePropertiesProvider) {
        String driverName = null;

        /*1、常规配置。*/
        DataSourceProperties dataSourceProperties = dataSourcePropertiesProvider.getIfAvailable();
        if (null != dataSourceProperties && null != dataSourceProperties.getDriverClassName()) {
            driverName = StringUtil.trimToNull(dataSourceProperties.getDriverClassName());
        }

        if (null == driverName) {
            /*2、动态数据源配置。*/
            com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties dynamicDataSourceProperties =
                dynamicDataSourcePropertiesProvider.getIfAvailable();
            if(null != dynamicDataSourceProperties) {
                /*动态数据源使用主数据库。*/
                String primaryDataSourceName = StringUtil.trimToNull(dynamicDataSourceProperties.getPrimary());
                if (null != primaryDataSourceName) {
                    com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty dataSourceProperty =
                        dynamicDataSourceProperties.getDatasource().get(primaryDataSourceName);
                    if (null != dataSourceProperty) {
                        driverName = StringUtil.trimToNull(dataSourceProperty.getDriverClassName());
                    }
                }
            }
        }
        return null == driverName ? new MyBatisPlusConfigProperties() : new MyBatisPlusConfigProperties(driverName);
    }

    /*参考 https://www.csdn.net/tags/MtTaEg1sOTk1ODgxLWJsb2cO0O0O.html, 解决 “MyBatis-Plus 分页 SQL 两个LIMIT”问题。*/
//    @Bean
//    @ConditionalOnMissingBean(value = {com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor.class})
//    protected com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor paginationInterceptor() {
//        return new com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor();
//    }

//    @Bean
//    @ConditionalOnMissingBean(value = {com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor.class})
//    protected com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor paginationInnerInterceptor() {
//        return new com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor();
//    }

    /*
     * SQL 注入。
     */
    @Bean
    @ConditionalOnMissingBean(value = com.baomidou.mybatisplus.core.injector.ISqlInjector.class)
    protected com.baomidou.mybatisplus.core.injector.ISqlInjector sqlInjector() {
        return new org.zodiac.mybatisplus.injector.MyBatisPlusSqlInjector();
    }

    @Bean
    @Order(value = Ordered.HIGHEST_PRECEDENCE)
    @ConditionalOnProperty(value = org.zodiac.mybatisplus.constants.MyBatisPlusSystemPropertiesConstants.MYBATIS_PLUS_SMART_ENUM, havingValue = "true")
    //@ConditionalOnProperty(value = org.zodiac.mybatisplus.constants.MyBatisPlusSystemPropertiesConstants.MYBATIS_PLUS_SMART_ENUM, havingValue = "true", matchIfMissing = true)
    protected MyBatisPlusSqlSessionFactoryCustomizer myBatisPlusSqlSessionFactoryCustomizer(org.apache.ibatis.session.SqlSessionFactory sqlSessionFactory,
        ApplicationContext applicationContext) {
        return new MyBatisPlusSqlSessionFactoryCustomizer(mybatisPlusProperties.getTypeEnumsPackage(), sqlSessionFactory, applicationContext);
    }

    /*
     * mybatis-plus 拦截器集合。
     */
    @Bean
    @ConditionalOnMissingBean(com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor.class)
    protected com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor mybatisPlusInterceptor(ObjectProvider queryInterceptors,
        ObjectProvider tenantLineInnerInterceptorProvider, MyBatisPlusConfigProperties myBatisPlusProperties) {
        com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor interceptor = new com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor();
        /*配置租户拦截器。*/
        if (myBatisPlusProperties.isTenantMode()) {
            com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor tenantLineInnerInterceptor = tenantLineInnerInterceptorProvider.getIfAvailable();
            if (null != tenantLineInnerInterceptor)
                interceptor.addInnerInterceptor(tenantLineInnerInterceptor);
        }
        /*配置分页拦截器。*/
        org.zodiac.mybatisplus.interceptor.MyBatisPlusPaginationInterceptor paginationInterceptor = new org.zodiac.mybatisplus.interceptor.MyBatisPlusPaginationInterceptor();
        /*配置自定义查询拦截器。*/
        org.zodiac.mybatisplus.interceptor.QueryInterceptor[] queryInterceptorArray = queryInterceptors.getIfAvailable();
        if (ArrayUtil.notEmptyArray(queryInterceptorArray)) {
            AnnotationAwareOrderComparator.sort(queryInterceptorArray);
            paginationInterceptor.setQueryInterceptors(queryInterceptorArray);
        }
        paginationInterceptor.setMaxLimit(myBatisPlusProperties.getPageLimit());
        paginationInterceptor.setOverflow(myBatisPlusProperties.isOverflow());
        paginationInterceptor.setOptimizeJoin(myBatisPlusProperties.isOptimizeJoin());
        interceptor.addInnerInterceptor(paginationInterceptor);
        return interceptor;
    }

    /*数据保护拦截器。默认不注入,通过“mybatis-plus.data-protect-enabled=true”可开启。 */
    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnBean(value = ProtectFieldHandler.class)
    protected org.zodiac.mybatisplus.interceptor.ProtectInterceptor protectInterceptor() {
        return new org.zodiac.mybatisplus.interceptor.ProtectInterceptor();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy