1、配置文件
spring.datasource.primary.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.primary.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.primary.jdbc-url=jdbc:mysql:///project_datahub?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true
spring.datasource.primary.username=root
spring.datasource.primary.password=root
spring.datasource.secondary.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.secondary.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.secondary.jdbc-url=jdbc:mysql:///project_datahub_second?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true
spring.datasource.secondary.username=root
spring.datasource.secondary.password=root
2、DataSourceConfig 配置
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 javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
/**
* @Description
* @Author lxk
* @version V1.0.0
*/
@Configuration
public class DataSourceConfig {
@Primary
@Bean(name = "firstDataSource")
@ConfigurationProperties(prefix="spring.datasource.primary")
public DataSource firstDataSource() {
System.out.println("primary db built");
return DataSourceBuilder.create().build();
}
@Bean(name = "secondDataSource")
@ConfigurationProperties(prefix="spring.datasource.secondary")
public DataSource secondDataSource() {
System.out.println("secondary db built");
return DataSourceBuilder.create().build();
}
}
3、各个数据源单独的配置
firstDataSource
@Configuration
@MapperScan(
basePackages = "com.it.iais.mapper.first",//mapper.java包路径
sqlSessionFactoryRef = "firstSqlSessionFactory")
public class PrimaryConfig {
@Autowired
@Qualifier("firstDataSource")
private DataSource firstDataSource;
private final static String mapper_location="classpath:mapper/first/*/*.xml";
@Bean
@Primary
public SqlSessionFactory firstSqlSessionFactory() throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(firstDataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapper_location));
bean.getObject().getConfiguration().setCallSettersOnNulls(true);
//开启驼峰转化,主要是为了解决复杂的sql对应的字段对应不上的问题
bean.getObject().getConfiguration.setMapUnderscoreToCamelCase(true);
return bean.getObject();
}
@Bean
@Primary
public SqlSessionTemplate firstSqlSessionTemplate() throws Exception {
return new SqlSessionTemplate(firstSqlSessionFactory());
}
}
secondDataSource
@Configuration
@MapperScan(
basePackages = "com.it.iais.mapper.second",//mapper.java包路径
sqlSessionFactoryRef = "secondSqlSessionFactory")
public class PrimaryConfig {
@Autowired
@Qualifier("secondDataSource")
private DataSource secondDataSource;
private final static String mapper_location="classpath:mapper/second/*/*.xml";
@Bean
public SqlSessionFactory secondSqlSessionFactory() throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(secondDataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mapper_location));
bean.getObject().getConfiguration().setCallSettersOnNulls(true);
//开启驼峰转化,主要是为了解决复杂的sql对应的字段对应不上的问题
bean.getObject().getConfiguration.setMapUnderscoreToCamelCase(true);
return bean.getObject();
}
@Bean
public SqlSessionTemplate secondSqlSessionTemplate() throws Exception {
return new SqlSessionTemplate(secondSqlSessionFactory());
}
}