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

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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);
}
/**
* session工厂Primary注解作用同上
*/
@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();
}
}