spring:
datasource:
hikari:
connection-timeout: 300000
minimum-idle: 2
maximum-pool-size: 100
idle-timeout: 120000
leak-detection-threshold: 300000
slot1:
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://127.0.0.1:3306/cspg_aomp_db_ms_new?serverTimezone=Asia/Shanghai&characterEncoding=UTF-8&useSSL=false&allowPublicKeyRetrieval=true
username: root
password: ...
slot2:
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://188.188.188.188:31827/storage-user?useSSL=false&autoReconnect=true&characterEncoding=utf8&allowPublicKeyRetrieval=true
username: root
password: ...
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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
@Configuration
@MapperScan(
basePackages = "com.cspg.nsx.mapper1",
sqlSessionFactoryRef = "masterSqlSessionFactory")
public class DataSourceSecondaryConfig {
@Bean(name = "dslot1")
@Qualifier("dslot1")
@Primary
@ConfigurationProperties(prefix="spring.datasource.slot1")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "masterSqlSessionFactory")
@Primary
public SqlSessionFactory masterSqlSessionFactory(@Qualifier("dslot1") DataSource masterDataSource)
throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(masterDataSource);
/*
获取xml文件资源对象
当Mapper接口所对应的.xml文件与Mapper接口文件分离,存储在 resources
文件夹下的时候,需要手动指定.xml文件所在的路径
*/
Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath*:mapper1/*.xml");
sessionFactory.setMapperLocations(resources);
return sessionFactory.getObject();
}
@Bean(name = "MasterDataSourceManager")
@Primary
public DataSourceTransactionManager MasterDataSourceManager() {
return new DataSourceTransactionManager(primaryDataSource());
}
}
第二个数据源配置,去掉@Primary,改MapperScan和ClassPath路径,改bean的命名,此处不写啦
@Configuration
@MapperScan(
basePackages = "com.cspg.nsx.mapper1",
sqlSessionFactoryRef = "masterSqlSessionFactory")
public class SecondDataSourceConfig {
@Bean(name = "dslot1")
@Qualifier("dslot1")
@Primary
@ConfigurationProperties(prefix="spring.datasource.slot1")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "masterSqlSessionFactory")
@Primary
public SqlSessionFactory masterSqlSessionFactory(@Qualifier("dslot1") DataSource masterDataSource)
throws Exception {
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
bean.setDataSource(masterDataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper1/*.xml"));
return bean.getObject();
}
}
还有一些SqlSessionTemplate 和DataSourceTransactionManager 没有配上,看你情况定,可以参考下面的链接配,这里主要讲区别!
MybatisPlus是用MybatisSqlSessionFactoryBean 来创建SqlSessionFactory ,而SpringBoot的则使用SqlSessionFactoryBean来创建SqlSessionFactory 😮