zsmeijin
6/16/2019 - 1:50 PM

MySQL数据源配置

@Configuration
public class ManagerDbConfig {

    @Bean(name = "managerDataSourceProperties")
    @ConfigurationProperties(prefix = "manager.datasource")
    public DataSourceProperties primaryDataSourceProperties() {
        return new DataSourceProperties();
    }

    @Bean(name = "managerDataSource")
    @ConfigurationProperties(prefix = "manager.datasource")
    public DataSource dataSource(@Qualifier("managerDataSourceProperties") DataSourceProperties dataSourceProperties) {
        return dataSourceProperties.initializeDataSourceBuilder().build();
    }

    @Bean(name = "managerTransactionManager")
    public DataSourceTransactionManager transactionManager(@Qualifier("managerDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "managerSqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("managerDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        return factoryBean.getObject();
    }

    @Bean(name = "managerConfigurer")
    public static MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage("cn.datawin.dao.mysql.manager");
        Properties propertiesMapper = new Properties();
        //通用mapper位置,不要和其他mapper、dao放在同一个目录
        propertiesMapper.setProperty("mappers", "tk.mybatis.mapper.common.Mapper,tk.mybatis.mapper.common.special.InsertListMapper");
        propertiesMapper.setProperty("notEmpty", "false");
        //主键UUID回写方法执行顺序,默认AFTER,可选值为(BEFORE|AFTER)
        //propertiesMapper.setProperty("ORDER","BEFORE");
        mapperScannerConfigurer.setProperties(propertiesMapper);
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("managerSqlSessionFactory");
        return mapperScannerConfigurer;
    }

}