在配置文件中配置双数据源连接
spring:
datasource:
production:
jdbc-url: jdbc:mysql://127.0.0.1:3306/production?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
hamster:
jdbc-url: jdbc:mysql://127.0.0.1:3306/hamster002?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
禁用springboot默认的DataSourceAutoConfiguration,因为它会读取application.properties
文件的spring.datasource.*
属性并自动配置单数据源。在@SpringBootApplication
注解中添加exclude
属性
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@ServletComponentScan
public class ProductionApplication {
public static void main(String[] args) {
SpringApplication.run(ProductionApplication.class, args);
}
}
新建配置类,手动创建数据源
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 javax.sql.DataSource;
@Configuration
public class DataSourceConfig {
@Bean(name = "productionDS")
@ConfigurationProperties(prefix = "spring.datasource.production")
public DataSource dataSource1() {
return DataSourceBuilder.create().build();
}
@Bean(name = "hamsterDS")
@ConfigurationProperties(prefix = "spring.datasource.hamster")
public DataSource dataSource2() {
return DataSourceBuilder.create().build();
}
}
配置mybatis,使用不同的数据源
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.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = {"cn.vsx.hamster.production.mapper.production"}, sqlSessionFactoryRef = "sqlSessionFactory1")
public class MybatisDbAConfig {
@Autowired
@Qualifier("productionDS")
private DataSource productionDS;
@Bean
public SqlSessionFactory sqlSessionFactory1() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(productionDS);
factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:cn/vsx/hamster/production/mapping/production/*.xml"));
return factoryBean.getObject();
}
@Bean
public SqlSessionTemplate sqlSessionTemplate1() throws Exception {
SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory1());
return template;
}
}
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.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = {"cn.vsx.hamster.production.mapper.hamster"}, sqlSessionFactoryRef = "sqlSessionFactory2")
public class MybatisDbBConfig {
@Autowired
@Qualifier("hamsterDS")
private DataSource hamsterDS;
@Bean
public SqlSessionFactory sqlSessionFactory2() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(hamsterDS);
factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:cn/vsx/hamster/production/mapping/hamster/*.xml"));
return factoryBean.getObject();
}
@Bean
public SqlSessionTemplate sqlSessionTemplate2() throws Exception {
SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory2());
return template;
}
}
配置完成之后,hamster里面的mapper会自动连接hamster数据库,production里面的mapper会自动连接production的数据库