leutu
2024-05-08 543e4eb01ca210b20876e8139cb3d0403d7d065c
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
package com.skyline.electricity.utils;
 
import org.mybatis.spring.annotation.*;
import javax.sql.*;
 
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.*;
import org.springframework.boot.context.properties.*;
import org.springframework.beans.factory.annotation.*;
import org.apache.ibatis.session.*;
import org.springframework.core.io.support.*;
import org.springframework.jdbc.datasource.*;
import org.mybatis.spring.*;
import org.springframework.context.annotation.Configuration;
 
@Configuration
@MapperScan(basePackages = { "com.skyline.electricity.mapper" }, sqlSessionTemplateRef = "firstSqlSessionTemplate")
public class FirstDataSourceConfig
{
    @Bean(name = { "firstDataSource" })
    @Primary
    @ConfigurationProperties(prefix = "first.datasource")
    public DataSource firstDataSource() {
        return DataSourceBuilder.create().build();
    }
    
    @Bean
    public SqlSessionFactory firstSqlSessionFactory(@Qualifier("firstDataSource") final DataSource dataSource) throws Exception {
        final SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapping/innerMapper/*.xml"));
        bean.setTypeAliasesPackage(" com.skyline.electricity.pojo,com.skyline.electricity.entity");
        bean.setDataSource(dataSource);
        return bean.getObject();
    }
    
    @Bean(name = { "firstDataSourceTransactionManger" })
    public DataSourceTransactionManager masterTransactionManger(@Qualifier("firstDataSource") final DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
    
    @Bean
    public SqlSessionTemplate firstSqlSessionTemplate(@Qualifier("firstSqlSessionFactory") final SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}