下面我为大家详细介绍“Spring Boot后端配置多个数据源、MySQL数据库的便捷方法”攻略。
在pom.xml文件中,引入MyBatis和多数据源依赖:
<!--MyBatis依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!--多数据源依赖-->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.4.5</version>
</dependency>
在application.yml文件中,进行多数据源的配置,例如配置一个名为primary的数据源和一个名为secondary的数据源。
spring:
datasource:
primary:
jdbc-url: jdbc:mysql://localhost:3306/primary?characterEncoding=utf8&useSSL=false
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
type: com.zaxxer.hikari.HikariDataSource
hikari:
pool-name: primary-db-pool
minimum-idle: 5
maximum-pool-size: 20
connection-timeout: 30000
idle-timeout: 600000
max-lifetime: 1800000
secondary:
jdbc-url: jdbc:mysql://localhost:3306/secondary?characterEncoding=utf8&useSSL=false
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
type: com.zaxxer.hikari.HikariDataSource
hikari:
pool-name: secondary-db-pool
minimum-idle: 5
maximum-pool-size: 20
connection-timeout: 30000
idle-timeout: 600000
max-lifetime: 1800000
根据不同的数据源,在代码中分别创建数据源的配置类,例如:
@Configuration
@EnableTransactionManagement
@MapperScan(basePackages = {"com.example.mapper.primary"}, sqlSessionFactoryRef = "primarySqlSessionFactory")
public class PrimaryDataSourceConfig {
@Bean(name = "primaryDataSource")
@Primary
@ConfigurationProperties(prefix = "spring.datasource.primary")
public DataSource dataSource() {
return DataSourceBuilder.create().type(HikariDataSource.class).build();
}
@Bean(name = "primarySqlSessionFactory")
@Primary
public SqlSessionFactory sqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource,
@Qualifier("mybatisConfig") MybatisConfigurationProperties configurationProperties)
throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
sessionFactory.setTypeHandlersPackage("com.example.mybatis.typehandler");
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath:mybatis/mapper/primary/*.xml"));
sessionFactory.setConfiguration(configurationProperties.getConfiguration());
return sessionFactory.getObject();
}
@Bean(name = "primaryDataSourceTransactionManager")
@Primary
public DataSourceTransactionManager transactionManager(@Qualifier("primaryDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
}
如需要在service层中使用另一个数据源,则需使用@Qualifier注解标明需要使用的数据源,例如:
@Service
@Transactional
public class UserService {
@Autowired
@Qualifier("secondarySqlSessionFactory")
private SqlSessionFactory secondarySqlSessionFactory;
public User getUserById(Long id) {
try (SqlSession sqlSession = secondarySqlSessionFactory.openSession()) {
UserDao userDao = sqlSession.getMapper(UserDao.class);
return userDao.selectById(id);
}
}
}
这样,我们就可以方便地进行多数据源配置了。
另外,如果需要进行读写分离,可以使用ShardingSphere等中间件来实现。
关于在Spring Boot中使用多数据源,我写了一篇博客,里面包含了详细的代码示例:Spring Boot后端配置多个数据源、MySQL数据库的便捷方法。