You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
2.7 KiB
Java

1 year ago
package com.example.duplicate.config;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
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 org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;
/**
* @ClassName RecordConfig
* @Description
* @Author linjj
* @Date 2023/8/2 16:48
* @Version 1.0
*/
@Configuration
// 指定主数据库扫描对应的Mapper文件生成代理对象
@MapperScan(basePackages ="com.example.duplicate.infrastructure.configTwoDao" ,sqlSessionFactoryRef = "configTwoSqlSessionFactory")
public class ConfigTwo {
// mapper.xml所在地址
private static final String MAPPER_LOCATION = "classpath*:mapper2/*.xml";
/**
* Primary
*
*/
@Primary
@Bean(name = "configTwoDataSource")
// 读取spring.datasource.master前缀的配置文件映射成对应的配置对象
@ConfigurationProperties(prefix = "spring.datasource.db2")
public DataSource dataSource() {
DataSource build = DataSourceBuilder.create().build();
return build;
}
/**
* Primary
*/
@Bean(name = "configTwoTransactionManager")
@Primary
public PlatformTransactionManager dataSourceTransactionManager(@Qualifier("configTwoDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
/**
* sessionPrimary
*/
@Bean(name = "configTwoSqlSessionFactory")
@Primary
public SqlSessionFactory sqlSessionFactory(@Qualifier("configTwoDataSource") DataSource dataSource) throws Exception {
final SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
sessionFactoryBean.setDataSource(dataSource);
sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(ConfigTwo.MAPPER_LOCATION));
return sessionFactoryBean.getObject();
}
}