fwq
2024-09-27 4034fdffb309c5636c2f7430b8f52bc2b0f2b514
提交 | 用户 | age
739bf5 1 package com.hx.phip.config;
C 2
3 import org.apache.ibatis.session.SqlSessionFactory;
4 import org.mybatis.spring.SqlSessionFactoryBean;
5 import org.mybatis.spring.SqlSessionTemplate;
6 import org.mybatis.spring.annotation.MapperScan;
7 import org.springframework.beans.factory.annotation.Qualifier;
8 import org.springframework.boot.context.properties.ConfigurationProperties;
9 import org.springframework.boot.jdbc.DataSourceBuilder;
10 import org.springframework.context.annotation.Bean;
11 import org.springframework.context.annotation.Configuration;
12 import org.springframework.context.annotation.Primary;
13 import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
14 import org.springframework.jdbc.datasource.DataSourceTransactionManager;
15
16 import javax.sql.DataSource;
17
18 /**
19  * Druid配置类
20  */
21 //@Configuration
22 //@MapperScan(basePackages = "com.hx.phip.dao.mapper.syn", sqlSessionTemplateRef  = "synSqlSessionTemplate")
23 public class DataSourceSynConfig {
24
25     @Bean(name = "synDataSource")
26     @ConfigurationProperties(prefix = "spring.datasource.syn")
27
28     public DataSource testDataSource() {
29         return DataSourceBuilder.create().build();
30     }
31
32     @Bean(name = "synSqlSessionFactory")
33     public SqlSessionFactory testSqlSessionFactory(@Qualifier("synDataSource") DataSource dataSource) throws Exception {
34         SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
35         bean.setDataSource(dataSource);
36         bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:/mapper/syn/*.xml"));
37         return bean.getObject();
38     }
39
40     @Bean(name = "synTransactionManager")
41     public DataSourceTransactionManager testTransactionManager(@Qualifier("synDataSource") DataSource dataSource) {
42         return new DataSourceTransactionManager(dataSource);
43     }
44
45     @Bean(name = "synSqlSessionTemplate")
46     public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("synSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
47         return new SqlSessionTemplate(sqlSessionFactory);
48     }
49
50 }