下面是使用MyBatis多数据源动态切换的完整攻略。
1. 添加依赖
在pom.xml文件中添加MyBatis和连接池的依赖,例如:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<!-- 数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.17</version>
</dependency>
2. 配置数据源
在application.yml或application.properties文件中配置数据源,例如:
spring:
datasource:
primary:
url: jdbc:mysql://localhost:3306/db1?useSSL=false&useUnicode=true&characterEncoding=utf8
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
secondary:
url: jdbc:mysql://localhost:3306/db2?useSSL=false&useUnicode=true&characterEncoding=utf8
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
3. 配置SqlSessionFactory
在MyBatis的配置文件mybatis-config.xml中,配置SqlSessionFactoryBean,指定数据源和mapper文件的路径,例如:
<beans>
<!-- 主数据源 -->
<bean id="primaryDataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="url" value="${spring.datasource.primary.url}" />
<property name="username" value="${spring.datasource.primary.username}" />
<property name="password" value="${spring.datasource.primary.password}" />
<property name="driverClassName" value="${spring.datasource.primary.driver-class-name}" />
</bean>
<!-- 第二数据源 -->
<bean id="secondaryDataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="url" value="${spring.datasource.secondary.url}" />
<property name="username" value="${spring.datasource.secondary.username}" />
<property name="password" value="${spring.datasource.secondary.password}" />
<property name="driverClassName" value="${spring.datasource.secondary.driver-class-name}" />
</bean>
<!-- 动态数据源,通过aop的方式实现动态切换 -->
<bean id="dynamicDataSource" class="com.xxx.DynamicDataSource">
<property name="targetDataSources">
<map key-type="java.lang.Object">
<entry key="primary" value-ref="primaryDataSource" />
<entry key="secondary" value-ref="secondaryDataSource" />
</map>
</property>
<property name="defaultTargetDataSource" ref="primaryDataSource" />
</bean>
<!-- SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dynamicDataSource" />
<property name="mapperLocations" value="classpath*:mapper/*.xml" />
</bean>
</beans>
4. 实现动态切换
在DynamicDataSource类中实现动态数据源切换,例如:
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return DynamicDataSourceContextHolder.getDataSourceType();
}
}
在DynamicDataSourceContextHolder类中保存数据源类型的上下文环境,例如:
public class DynamicDataSourceContextHolder {
private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();
public static void setDataSourceType(String dataSourceType) {
contextHolder.set(dataSourceType);
}
public static String getDataSourceType() {
return contextHolder.get();
}
public static void clearDataSourceType() {
contextHolder.remove();
}
}
然后在需要切换数据源的方法前调用setDataSourceType方法,例如:
// 切换到主数据源
DynamicDataSourceContextHolder.setDataSourceType("primary");
User user = userMapper.selectById(id);
// 切换到第二数据源
DynamicDataSourceContextHolder.setDataSourceType("secondary");
Order order = orderMapper.selectById(id);
// 切换回主数据源
DynamicDataSourceContextHolder.setDataSourceType("primary");
示例说明
下面是两个示例,第一个示例是查询主数据源中的用户,第二个示例是查询第二数据源中的订单。
// 示例一:查询主数据源中的用户
DynamicDataSourceContextHolder.setDataSourceType("primary");
User user = userMapper.selectById(id);
// 示例二:查询第二数据源中的订单
DynamicDataSourceContextHolder.setDataSourceType("secondary");
Order order = orderMapper.selectById(id);
注意,在调用完数据库操作方法后,需要调用DynamicDataSourceContextHolder.clearDataSourceType()方法,清除数据源类型的上下文环境。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:mybatis多数据源动态切换的完整步骤 - Python技术站