springboot集成mybatis 配置多数据源
创建一个springboot工程就不赘述了
直接上代码:
- 每一个数据源对应建立一个对应的包,如图:
- 数据源配置如下:
spring.datasource.basic.jdbc-url=jdbc:mysql://127.0.0.1:3306/XXX?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
spring.datasource.basic.username=root
spring.datasource.basic.password=root
#DB Configuration:
spring.datasource.mail.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.mail.jdbc-url=jdbc:mysql://127.0.0.1:3306/XXX_mail_reply?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
spring.datasource.mail.username=root
spring.datasource.mail.password=root
#DB Configuration:
spring.datasource.order.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.order.jdbc-url=jdbc:mysql://127.0.0.1:3306/XXX_XXX_order?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
spring.datasource.order.username=root
spring.datasource.order.password=root
- 给每一个数据源配置包扫描,注意主数据源的要添加**@Primary**注解
// 主配置类,创建对应数据源
@Configuration
public class DataSourceConfig {
@Bean("basic")
@Primary
@ConfigurationProperties(prefix = "spring.datasource.basic")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean("mail")
@ConfigurationProperties(prefix = "spring.datasource.mail")
public DataSource mailDataSource() {
return DataSourceBuilder.create().build();
}
@Bean("order")
@ConfigurationProperties(prefix = "spring.datasource.order")
public DataSource orderDataSource() {
return DataSourceBuilder.create().build();
}
}
/**
basic主数据源配置包扫描
**/
@Configuration
@MapperScan(basePackages = {"com.XXX.base.mapper"}, sqlSessionFactoryRef = "basicSqlSessionFactory")
public class BasicMybatisDataSourceConfig {
// 精确到master目录和其他数据源隔离
static final String MAPPER_LOCATION = "classpath:com/XXX/base/mapper/*.xml";
@Bean(name = "basicSqlSessionFactory")
@Primary
public SqlSessionFactory masterSqlSessionFactory(@Qualifier("basic") DataSource masterDataSource)
throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(masterDataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources(BasicMybatisDataSourceConfig.MAPPER_LOCATION));
return sessionFactory.getObject();
}
}
/**
mail数据源配置包扫描
**/
@Configuration
@MapperScan(basePackages = {"com.XXX.mail.mapper"}, sqlSessionFactoryRef = "mailSqlSessionFactory")
public class MailMybatisDataSourceConfig {
// 精确到master目录和其他数据源隔离
static final String MAPPER_LOCATION = "classpath:com/XXX/mail/mapper/*.xml";
@Bean(name = "mailSqlSessionFactory")
public SqlSessionFactory masterSqlSessionFactory(@Qualifier("mail") DataSource masterDataSource)
throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(masterDataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources(MailMybatisDataSourceConfig.MAPPER_LOCATION));
return sessionFactory.getObject();
}
}
/**
order数据源配置包扫描
**/
@Configuration
@MapperScan(basePackages = {"com.XXX.order.mapper"}, sqlSessionFactoryRef = "orderSqlSessionFactory")
public class OrderMybatisDataSourceConfig {
// 精确到master目录和其他数据源隔离
static final String MAPPER_LOCATION = "classpath:com/XXX/order/mapper/*.xml";
@Bean(name = "orderSqlSessionFactory")
public SqlSessionFactory masterSqlSessionFactory(@Qualifier("order") DataSource masterDataSource)
throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(masterDataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources(OrderMybatisDataSourceConfig.MAPPER_LOCATION));
return sessionFactory.getObject();
}
}