Spring Mybatis多数据源配置范例

时间:2023-03-09 17:52:28
Spring Mybatis多数据源配置范例
 <?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:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 框架 -->
<bean id="messageFactory" class="catf.core.message.factory.MessageFactory">
<constructor-arg index="0" name="resource" value="catf/resource/mapper/msg/catf_messages_mapper.xml"/>
</bean> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:spring/handler.properties</value>
<value>classpath*:/config/扩展api/expand-base-config.properties</value>
<value>classpath:spring/ffmpeg.properties</value>
<value>classpath:公共数据/mysql配置.properties</value>
</list>
</property>
<property name="fileEncoding" value="utf-8" />
</bean> <bean id="dataSourceParent" abstract="true" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="filters" value="stat" />
<property name="maxActive" value="20" />
<property name="initialSize" value="1" />
<property name="maxWait" value="60000" />
<property name="minIdle" value="1" />
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="poolPreparedStatements" value="true" />
<property name="maxOpenPreparedStatements" value="20" />
<property name="asyncInit" value="true" />
</bean> <bean id="dataSourceMaserati" parent="dataSourceParent">
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean> <bean id="dataSourceBentley" parent="dataSourceParent">
<property name="url" value="${jdbc2.url}" />
<property name="username" value="${jdbc2.username}" />
<property name="password" value="${jdbc2.password}" />
</bean> <bean id="dataSourceSwitcher" class="catf.component.mybatis.manager.ThreadLocalRountingDataSource">
<property name="targetDataSources">
<map>
<entry key="dataSourceMarserati" value-ref="dataSourceMaserati"/>
<entry key="dataSourceBentley" value-ref="dataSourceBentley"/>
</map>
</property>
<property name="defaultTargetDataSource" ref="dataSourceMaserati"/>
</bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSourceSwitcher"/>
<property name="typeAliasesPackage" value="catf.component.mybatis"/>
<property name="mapperLocations" value="classpath:catf/component/mybatis/mapper/mybatisMapper.xml"/>
</bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
p:basePackage="catf.component.mybatis.dao"
p:sqlSessionFactoryBeanName="sqlSessionFactory">
</bean> <!-- 4. 事务管理 : DataSourceTransactionManager -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSourceSwitcher"/>
</bean> <!-- 5. 使用声明式事务 -->
<tx:annotation-driven transaction-manager="txManager" /> <bean id="handlerStart" class="catf.core.handler.frame.spring.HandlerStart">
<property name="frameMessageHandler" value="${handler.message.frame}"/>
<property name="tstExecMessageHandler" value="${handler.message.tst.exec}"/>
<property name="interpreterHandler" value="${handler.interpreter}"/>
<property name="loaderHandler" value="${handler.loader}"/>
<property name="fileOperateHandler" value="${handler.file.operate}"/>
</bean>
<bean id="ffmpegConfig" class="catf.executor.record.model.FFmpegConfig">
<property name="windowsCmd" value="${ffmpeg.win}"/>
<property name="linuxCmd" value="${ffmpeg.lx}"/>
<property name="macOSCmd" value="${ffmpeg.mac}"/>
</bean> <context:component-scan
base-package="catf.core.thread.pool,catf.core.handler.frame.spring,catf.core.message.service,catf.core.xml.service">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
<context:exclude-filter type="regex" expression="catf.core.handler.frame.spring.HandlerStart"/>
</context:component-scan>
<context:component-scan
base-package="catf.interpreter, catf.loader, catf.executor">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
<context:exclude-filter type="regex" expression="catf.executor.record.model.FFmpegConfig"/>
</context:component-scan> <!-- component -->
<context:component-scan
base-package="catf.component">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
<context:exclude-filter type="regex" expression="catf.executor.record.model.FFmpegConfig"/>
</context:component-scan>
<aop:aspectj-autoproxy proxy-target-class="true" /> <!-- 扩展组件 -->
<context:component-scan
base-package="catf.expand.component.base">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan>
<context:component-scan
base-package="catf.expand">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan>
<context:component-scan
base-package="catf.expand.component.web">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan>
<context:component-scan
base-package="weshare.token">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan> </beans>

mysql配置.properties

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://20.1.1.11:16306/maserati?tinyInt1isBit=false&autoReconnect=true
jdbc.username=root
jdbc.password=root jdbc2.driver=com.mysql.cj.jdbc.Driver
jdbc2.url=jdbc:mysql://20.1.1.11:16306/bentley?tinyInt1isBit=false&autoReconnect=true
jdbc2.username=root
jdbc2.password=root
ThreadLocalRountingDataSource.class
 package catf.component.mybatis.manager;

 import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

 public class ThreadLocalRountingDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return DataSourcesTypeManager.get();
} }
DataSourcesTypeManager .class
 package catf.component.mybatis.manager;

 public class DataSourcesTypeManager {

     private static final ThreadLocal<String> dataSourceTypes = new ThreadLocal<>();

     public static String get() {
return dataSourceTypes.get();
} /**
* 设置数据源
*/
public static void set(String dataSourceType) {
dataSourceTypes.set(dataSourceType);
} /**
* 清除dataSourceKey的值
*/
public static void remove() {
dataSourceTypes.remove();
} }