yml如下:
server:
port: 80
servlet:
context-path: /
spring:
datasource:
base:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: oracle.jdbc.driver.OracleDriver
initialize: true #指定初始化数据源,是否用data.sql来初始化,默认: true
url: jdbc:oracle:thin:@x.x.x.x:1521:orcl
username: username
password: password
gas:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
initialize: true
url: jdbc:sqlserver://x.x.x.x:2433
username: username
password: password
bridge:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
initialize: true
url: jdbc:sqlserver://x.x.x.x:1433
username: username
password: password
mybatis:
configuration:
map-underscore-to-camel-case: true
package com.ltsk.whcg.config;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
@Configuration
// 加上这个注解,使得支持事务
@EnableTransactionManagement
@MapperScan(basePackages = "com.ltsk.whcg.base.mapper", sqlSessionTemplateRef = "baseSqlSessionTemplate")
public class BaseConfig {
@Bean(name = "baseDataSource")
@ConfigurationProperties(prefix = "spring.datasource.base")
@Primary
public DataSource setDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "baseTransactionManager")
@Primary
public DataSourceTransactionManager setTransactionManager(@Qualifier("baseDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "baseSqlSessionFactory")
@Primary
public SqlSessionFactory setSqlSessionFactory(@Qualifier("baseDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
return bean.getObject();
}
@Bean(name = "baseSqlSessionTemplate")
@Primary
public SqlSessionTemplate setSqlSessionTemplate(
@Qualifier("baseSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
package com.ltsk.whcg.config;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
@Configuration
// 加上这个注解,使得支持事务
@EnableTransactionManagement
@MapperScan(basePackages = "com.ltsk.whcg.bridge.mapper", sqlSessionTemplateRef = "bridgeSqlSessionTemplate")
public class BridgeConfig {
@Bean(name = "bridgeDataSource")
@ConfigurationProperties(prefix = "spring.datasource.bridge")
public DataSource setDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "bridgeTransactionManager")
public DataSourceTransactionManager setTransactionManager(@Qualifier("bridgeDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "bridgeSqlSessionFactory")
public SqlSessionFactory setSqlSessionFactory(@Qualifier("bridgeDataSource") DataSource dataSource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
return bean.getObject();
}
@Bean(name = "bridgeSqlSessionTemplate")
public SqlSessionTemplate setSqlSessionTemplate(
@Qualifier("bridgeSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
package com.ltsk.whcg.config;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
@Configuration
// 加上这个注解,使得支持事务
@EnableTransactionManagement
@MapperScan(basePackages = "com.ltsk.whcg.gas.mapper", sqlSessionTemplateRef = "gasSqlSessionTemplate")
public class GasConfig {
@Bean(name = "gasDataSource")
@ConfigurationProperties(prefix = "spring.datasource.gas")
public DataSource setDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "gasTransactionManager")
public DataSourceTransactionManager setTransactionManager(@Qualifier("gasDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "gasSqlSessionFactory")
public SqlSessionFactory setSqlSessionFactory(@Qualifier("gasDataSource") DataSource dataSource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
return bean.getObject();
}
@Bean(name = "gasSqlSessionTemplate")
public SqlSessionTemplate setSqlSessionTemplate(
@Qualifier("gasSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
之后将mapper文件分别放入com.ltsk.whcg.base.mapper;com.ltsk.whcg.bridge.mapper;com.ltsk.whcg.gas.mapper三个包中即可