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

templates.datasource.ftl Maven / Gradle / Ivy

There is a newer version: 1.2.0
Show newest version
package ${configPackage};

import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.github.yhl452493373.bean.DataSourceGroup;
import com.github.yhl452493373.bean.DataSourceProperties;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

@SuppressWarnings("WeakerAccess")
@Configuration
@MapperScan(basePackages = ${name!?cap_first!""}DataSourceConfig.PACKAGE, sqlSessionFactoryRef = "${name!}${name???string("S","s")}qlSessionFactory")
public class ${name!?cap_first!""}DataSourceConfig {
    <#if name??>
    static final String PACKAGE = "${mapperPackage}.${name}";
        <#if mapperLocations?contains("classpath")>
    static final String MAPPER_LOCATION = "${mapperLocations}/${name}/*.xml";
        <#else>
    static final String MAPPER_LOCATION = "classpath:${mapperLocations?replace(".","/")}/${name}/*.xml";
        
    <#else>
    static final String PACKAGE = "${mapperPackage}";
        <#if mapperLocations?contains("classpath")>
    static final String MAPPER_LOCATION = "${mapperLocations}/*.xml";
        <#else>
    static final String MAPPER_LOCATION = "classpath:${mapperLocations?replace(".","/")}/*.xml";
        
    

    @Bean
    public DataSource ${name!}${name???string("D","d")}ataSource(DataSourceGroup dataSourceGroup) {
        DataSourceProperties dataSourceProperties = null;
        if (dataSourceGroup.getDataSourceProperties() != null) {
            dataSourceProperties = dataSourceGroup.getDataSourceProperties();
        } <#if name??> else if (dataSourceGroup.getDataSourcePropertiesList() != null) {
            for (DataSourceProperties tempDataSourceProperties : dataSourceGroup.getDataSourcePropertiesList()) {
                if (tempDataSourceProperties.getName().equalsIgnoreCase("${name}")) {
                    dataSourceProperties = tempDataSourceProperties;
                    break;
                }
            }
        }
        if (dataSourceProperties == null)
            throw new NullPointerException("${name!}数据源未配置");
        return dataSourceProperties;
    }

    @Bean
    <#if primary??>
    @Primary
    
    public DataSourceTransactionManager ${name!}${name???string("T","t")}ransactionManager(DataSourceGroup dataSourceGroup) {
        return new DataSourceTransactionManager(${name!}${name???string("D","d")}ataSource(dataSourceGroup));
    }

    @Bean
    <#if primary??>
    @Primary
    
    public SqlSessionFactory ${name!}${name???string("S","s")}qlSessionFactory(DataSource ${name!}${name???string("D","d")}ataSource, PaginationInterceptor paginationInterceptor) throws Exception {
        final MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();
        sessionFactory.setDataSource(${name!}${name???string("D","d")}ataSource);
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(${name!?cap_first!""}DataSourceConfig.MAPPER_LOCATION));
        sessionFactory.setGlobalConfig(new GlobalConfig().setDbConfig(new GlobalConfig.DbConfig().setIdType(IdType.UUID)));
        MybatisConfiguration configuration = new MybatisConfiguration();
        <#if cacheEnabled>
        configuration.setCacheEnabled(true);
        <#else>
        configuration.setCacheEnabled(false);
        
        sessionFactory.setConfiguration(configuration);
        sessionFactory.setPlugins(new Interceptor[]{
                paginationInterceptor
        });
        return sessionFactory.getObject();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy