(2)、Spring Boot与Mybatis的整合( 多数据源)

1、背景的介绍

有时候我们需要做分库分表,那么肯定存在多数据源,Spring Boot和Mybatis的多数据源是如何整合的呢?比如说我们现在做了一个浪迹天涯管理的后台系统,商品信息是存在itemCenter数据源中的,而与用户相关的信息是存在account数据源中,项目结构如下:


2、Spring Boot和Mybatis的整合

2.1 添加相关的pom文件

这个我不在这里详细的贴代码,稍后请查看具体的源代码。

2.2 在环境配置文件中添加数据库的配置


因为我们这里有四个环境,分别是dev、sit、ust、prod。所以我们需要在每个环境下配置jdbc.properties。并且我们在jdbc.properties配置文件中配置了2个数据源,分别是itemcenter和account。

2.3 数据源的读取和事物的配置以及Mybatis的配置

因为这些都是通用的,所以我们放在了common这个文件下统一配置了,如下图所示:


(1). 现在我们具体的看下dataSource.xml的配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     	http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

	<aop:aspectj-autoproxy proxy-target-class="false"/>

	<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
	
	<tx:annotation-driven transaction-manager="txManager" />
	
	<bean id="itemcenterDataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
		<property name="url" value="${itemcenter.jdbc.url}" />
		<property name="username" value="${itemcenter.jdbc.username}" />
		<property name="password" value="${itemcenter.jdbc.password}" />
		<property name="driverClassName" value="${itemcenter.jdbc.driver}" />
	</bean>

	<bean id="accountDataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
		<property name="url" value="${account.jdbc.url}" />
		<property name="username" value="${account.jdbc.username}" />
		<property name="password" value="${account.jdbc.password}" />
		<property name="driverClassName" value="${account.jdbc.driver}" />
	</bean>

	<bean id ="dataSource" class= "com.niepengfei.langjitianya.config.DynamicDataSource" >
		<property name ="targetDataSources">
			<map key-type ="java.lang.String">
				<entry value-ref ="itemcenterDataSource" key= "itemcenter_dataSource"/>
				<entry value-ref ="accountDataSource" key= "account_dataSource"/>
			</map>
		</property>
	</bean>

	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="configLocation" value="classpath:config/common/mybatisConfig.xml"/>
		<property name="dataSource" ref="dataSource"/>
	</bean>
</beans>

(2). 现在我们具体的看下mybatisConfig.xml的配置:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	
		<!-- 配置分页插件 -->
	<plugins>
		<plugin interceptor="com.github.pagehelper.PageHelper">
			<!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->        
        	<property name="dialect" value="mysql"/>
		</plugin>
	</plugins>

</configuration>
(3). 通过上面的配置我们知道, dataSource.xml是读取数据源,且mybatisConfig.xml是在dataSource.xml被加载。

2.4 开发dao

我们把dao和相应的xml文件放在一起,约定俗成。这样省去了配置dao和xml的对应关系。


2.5 启动类的配置


2.6 我们的数据源的切换是在service层,这里我使用Spring AOP的思想,利用切面在Service层切换数据源。

(1).首先定义数据源的枚举


(2)、然后分别为每个数据源定义个注解,例如AccountDatasource和ItemCenterDataSource,代码如下:


(3).定义DynamicDataSource,该类继承AbstractRoutingDataSource,重写determineCurrentLookupKey:


(4)、定义数据源切面:


(5)、在Service层加上相应的数据源注解


以上就是全部过程。

猜你喜欢

转载自blog.csdn.net/pfnie/article/details/79604347