在Spring Boot项目中整合MyBatis来实现多数据源的解决方案,可以通过一系列具体的步骤来完成。这种配置通常涉及到定义多个数据源、配置数据源属性、创建对应的SqlSessionFactory
实例以及事务管理器。下面是一个详细的步骤指南和示例代码:
1. 添加依赖
首先,确保你的pom.xml
文件中包含了Spring Boot和MyBatis的相关依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
</dependencies>
2. 配置多数据源
在application.properties
或application.yml
中配置两个数据源。例如,配置primary和secondary两个数据源:
spring.datasource.primary.url=jdbc:mysql://localhost:3306/db_primary?useSSL=false
spring.datasource.primary.username=root
spring.datasource.primary.password=password
spring.datasource.primary.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.secondary.url=jdbc:mysql://localhost:3306/db_secondary?useSSL=false
spring.datasource.secondary.username=root
spring.datasource.secondary.password=password
spring.datasource.secondary.driver-class-name=com.mysql.cj.jdbc.Driver
3. 数据源配置类
创建两个配置类,每个类配置一个数据源、一个SqlSessionFactory
和一个事务管理器。
@Configuration
@MapperScan(basePackages = "com.example.demo.mapper.primary", sqlSessionTemplateRef = "primarySqlSessionTemplate")
public class PrimaryDataSourceConfig {
@Bean(name = "primaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.primary")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "primarySqlSessionFactory")
public SqlSessionFactory primarySqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/primary/*.xml"));
return bean.getObject();
}
@Bean(name = "primaryTransactionManager")
public DataSourceTransactionManager primaryTransactionManager(@Qualifier("primaryDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "primarySqlSessionTemplate")
public SqlSessionTemplate primarySqlSessionTemplate(@Qualifier("primarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
@Configuration
@MapperScan(basePackages = "com.example.demo.mapper.secondary", sqlSessionTemplateRef = "secondarySqlSessionTemplate")
public class SecondaryDataSourceConfig {
@Bean(name = "secondaryDataSource")
@ConfigurationProperties(prefix = "spring.datasource.secondary")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "secondarySqlSessionFactory")
public SqlSessionFactory secondarySqlSessionFactory(@Qualifier("secondaryDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/secondary/*.xml"));
return bean.getObject();
}
@Bean(name = "secondaryTransactionManager")
public DataSourceTransactionManager secondaryTransactionManager(@Qualifier("secondaryDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "secondarySqlSessionTemplate")
public SqlSessionTemplate secondarySqlSessionTemplate(@Qualifier("secondarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
4. 定义Mapper XML文件
确保为每个数据源的Mapper创建正确的XML映射文件。例如,在`src/main/resources/mapper/primary`目录下和`src/main/resources/mapper/secondary`目录下放置相应的Mapper XML文件。 `primary/UserMapper.xml`示例:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.mapper.primary.UserMapper">
<insert id="insertUser" parameterType="com.example.demo.domain.User">
INSERT INTO users (name, email) VALUES (#{name}, #{email})
</insert>
<select id="findUserById" parameterType="int" resultType="com.example.demo.domain.User">
SELECT * FROM users WHERE id = #{id}
</select>
</mapper>
5. 写Mapper接口
6. 运行和测试
完成以上配置后,你应该能够运行你的Spring Boot应用,并在需要时切换不同的数据源。可以通过单元测试或直接通过服务调用来验证不同数据源的功能。
这些步骤和示例代码提供了一个基本框架,如何在Spring Boot和MyBatis环境中实现多数据源的配置。根据项目的具体需求,你可能还需要做一些额外的配置或调整。