数据源配置:
spring: application: name: fin-web-service datasource: manage: driver-class-name: com.mysql.jdbc.Driver jdbc-url: jdbc:mysql://localhost:3306/manage?useUnicode=true&characterEncoding=utf8&autoReconnect=true username: root password: root connection-test-query: SELECT 1 FROM DUAL risk: driver-class-name: com.mysql.jdbc.Driver jdbc-url: jdbc:mysql://localhost:3306/risk?useUnicode=true&characterEncoding=utf8&autoReconnect=true username: root password: root connection-test-query: SELECT 1 FROM DUAL
@Configuration @MapperScan(basePackages = "com.demo.fin.orm.manage.mapper", sqlSessionTemplateRef = "mexcSqlSessionTemplate") public class DataSource1Config { @Value("classpath*:mybatis/manage/*.xml") private Resource[] mapperLocations; @Bean(name = "mexcDataSource") @ConfigurationProperties(prefix = "spring.datasource.manage") @Primary public DataSource mexcDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "mexcSqlSessionFactory") @Primary /*此处必须在主数据库的数据源配置上加上@Primary*/ public SqlSessionFactory mSqlSessionFactory(@Qualifier("mDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); /*加载mybatis全局配置文件*/ bean.setConfigLocation(new PathMatchingResourcePatternResolver().getResource("classpath:mybatis/mybatis-config.xml")); /*加载所有的mapper.xml映射文件*/ bean.setMapperLocations(mapperLocations); return bean.getObject(); } @Bean(name = "mTransactionManager") @Primary public DataSourceTransactionManager mTransactionManager(@Qualifier("mDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "mSqlSessionTemplate") @Primary public SqlSessionTemplate mSqlSessionTemplate(@Qualifier("mSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } }
@Configuration @MapperScan(basePackages = "com.demo.fin.orm.risk.mapper", sqlSessionTemplateRef = "finSqlSessionTemplate") public class DataSource2Config { @Value("classpath*:mybatis/risk/*.xml") private Resource[] mapperLocations; @Bean(name = "rDataSource") @ConfigurationProperties(prefix = "spring.datasource.risk") public DataSource rDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "rSqlSessionFactory") public SqlSessionFactory rSqlSessionFactory(@Qualifier("rDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); /*加载mybatis全局配置文件*/ Resource resource = new PathMatchingResourcePatternResolver().getResource("classpath:mybatis/mybatis-config.xml"); bean.setConfigLocation(resource); /*加载所有的mapper.xml映射文件*/ bean.setMapperLocations(mapperLocations); return bean.getObject(); } @Bean(name = "rTransactionManager") public DataSourceTransactionManager rTransactionManager(@Qualifier("rDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "rSqlSessionTemplate") public SqlSessionTemplate rSqlSessionTemplate(@Qualifier("rSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } }
启动类:
@SpringBootApplication @EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class}) @ComponentScan(basePackages = { "xxx" }) public class ServerApplication {
public static void main(String[] args) { new SpringApplicationBuilder(ServerApplication.class).web(WebApplicationType.SERVLET).run(args);
}
}