fwq
2024-09-27 4034fdffb309c5636c2f7430b8f52bc2b0f2b514
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.hx.phip.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;
 
/**
 * Druid配置类
 */
//@Configuration
//@MapperScan(basePackages = "com.hx.phip.dao.mapper.syn", sqlSessionTemplateRef  = "synSqlSessionTemplate")
public class DataSourceSynConfig {
 
    @Bean(name = "synDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.syn")
 
    public DataSource testDataSource() {
        return DataSourceBuilder.create().build();
    }
 
    @Bean(name = "synSqlSessionFactory")
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("synDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:/mapper/syn/*.xml"));
        return bean.getObject();
    }
 
    @Bean(name = "synTransactionManager")
    public DataSourceTransactionManager testTransactionManager(@Qualifier("synDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
 
    @Bean(name = "synSqlSessionTemplate")
    public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("synSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
 
}