123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- package com.zcxk.config;
- import org.apache.ibatis.session.SqlSessionFactory;
- import org.mybatis.spring.SqlSessionFactoryBean;
- import org.mybatis.spring.SqlSessionTemplate;
- import org.mybatis.spring.annotation.MapperScan;
- import org.springframework.beans.factory.annotation.Qualifier;
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.boot.jdbc.DataSourceBuilder;
- 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;
- @Configuration
- @MapperScan(basePackages = "com.zcxk.dao.sqlserver", sqlSessionTemplateRef = "sqlServerSqlSessionTemplate")
- public class SqlServerDataSourceConfig {
- @Bean(name = "sqlServerDataSource")
- @ConfigurationProperties(prefix = "spring.datasource.sqlsever")
- @Primary
- public DataSource testDataSource() {
- return DataSourceBuilder.create().build();
- }
- @Bean(name = "sqlServerSqlSessionFactory")
- @Primary
- public SqlSessionFactory testSqlSessionFactory(@Qualifier("sqlServerDataSource") DataSource dataSource) throws Exception {
- SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
- bean.setDataSource(dataSource);
- bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:sqlServer/*.xml"));
- return bean.getObject();
- }
- @Bean(name = "sqlServerTransactionManager")
- @Primary
- public DataSourceTransactionManager testTransactionManager(@Qualifier("sqlServerDataSource") DataSource dataSource) {
- return new DataSourceTransactionManager(dataSource);
- }
- @Bean(name = "sqlServerSqlSessionTemplate")
- @Primary
- public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("sqlServerSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
- return new SqlSessionTemplate(sqlSessionFactory);
- }
- }
|