• Spring Mybatis多数据源配置范例


      1 <?xml version="1.0" encoding="UTF-8"?>
      2 <beans xmlns="http://www.springframework.org/schema/beans"
      3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      4        xmlns:tx="http://www.springframework.org/schema/tx"
      5        xmlns:p="http://www.springframework.org/schema/p"
      6        xmlns:context="http://www.springframework.org/schema/context"
      7        xmlns:aop="http://www.springframework.org/schema/aop"
      8        xsi:schemaLocation="
      9        http://www.springframework.org/schema/beans
     10        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
     11        http://www.springframework.org/schema/context
     12        http://www.springframework.org/schema/context/spring-context-4.3.xsd
     13        http://www.springframework.org/schema/aop
     14        http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
     15        http://www.springframework.org/schema/tx
     16        http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
     17 
     18     <!-- 框架 -->
     19     <bean id="messageFactory" class="catf.core.message.factory.MessageFactory">
     20         <constructor-arg index="0" name="resource" value="catf/resource/mapper/msg/catf_messages_mapper.xml"/>
     21     </bean>
     22 
     23     <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
     24         <property name="locations">
     25             <list>
     26                 <value>classpath:spring/handler.properties</value>
     27                 <value>classpath*:/config/扩展api/expand-base-config.properties</value>
     28                 <value>classpath:spring/ffmpeg.properties</value>
     29                 <value>classpath:公共数据/mysql配置.properties</value>
     30             </list>
     31         </property>
     32         <property name="fileEncoding" value="utf-8" />
     33     </bean>
     34 
     35 
     36     <bean id="dataSourceParent" abstract="true" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
     37         <property name="filters" value="stat" />
     38         <property name="maxActive" value="20" />
     39         <property name="initialSize" value="1" />
     40         <property name="maxWait" value="60000" />
     41         <property name="minIdle" value="1" />
     42         <property name="timeBetweenEvictionRunsMillis" value="60000" />
     43         <property name="minEvictableIdleTimeMillis" value="300000" />
     44         <property name="testWhileIdle" value="true" />
     45         <property name="testOnBorrow" value="false" />
     46         <property name="testOnReturn" value="false" />
     47         <property name="poolPreparedStatements" value="true" />
     48         <property name="maxOpenPreparedStatements" value="20" />
     49         <property name="asyncInit" value="true" />
     50     </bean>
     51 
     52     <bean id="dataSourceMaserati" parent="dataSourceParent">
     53         <property name="url" value="${jdbc.url}" />
     54         <property name="username" value="${jdbc.username}" />
     55         <property name="password" value="${jdbc.password}" />
     56     </bean>
     57 
     58     <bean id="dataSourceBentley" parent="dataSourceParent">
     59         <property name="url" value="${jdbc2.url}" />
     60         <property name="username" value="${jdbc2.username}" />
     61         <property name="password" value="${jdbc2.password}" />
     62     </bean>
     63 
     64     <bean id="dataSourceSwitcher" class="catf.component.mybatis.manager.ThreadLocalRountingDataSource">
     65         <property name="targetDataSources">
     66             <map>
     67                 <entry key="dataSourceMarserati" value-ref="dataSourceMaserati"/>
     68                 <entry key="dataSourceBentley" value-ref="dataSourceBentley"/>
     69             </map>
     70         </property>
     71         <property name="defaultTargetDataSource" ref="dataSourceMaserati"/>
     72     </bean>
     73 
     74     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
     75         <property name="dataSource" ref="dataSourceSwitcher"/>
     76         <property name="typeAliasesPackage" value="catf.component.mybatis"/>
     77         <property name="mapperLocations" value="classpath:catf/component/mybatis/mapper/mybatisMapper.xml"/>
     78     </bean>
     79 
     80     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
     81           p:basePackage="catf.component.mybatis.dao"
     82           p:sqlSessionFactoryBeanName="sqlSessionFactory">
     83     </bean>
     84 
     85     <!-- 4. 事务管理 : DataSourceTransactionManager -->
     86     <bean  id="txManager"
     87            class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
     88         <property name="dataSource" ref="dataSourceSwitcher"/>
     89     </bean>
     90 
     91     <!-- 5. 使用声明式事务 -->
     92     <tx:annotation-driven transaction-manager="txManager" />
     93 
     94     <bean id="handlerStart" class="catf.core.handler.frame.spring.HandlerStart">
     95         <property name="frameMessageHandler" value="${handler.message.frame}"/>
     96         <property name="tstExecMessageHandler" value="${handler.message.tst.exec}"/>
     97         <property name="interpreterHandler" value="${handler.interpreter}"/>
     98         <property name="loaderHandler" value="${handler.loader}"/>
     99         <property name="fileOperateHandler" value="${handler.file.operate}"/>
    100     </bean>
    101     <bean id="ffmpegConfig" class="catf.executor.record.model.FFmpegConfig">
    102         <property name="windowsCmd" value="${ffmpeg.win}"/>
    103         <property name="linuxCmd" value="${ffmpeg.lx}"/>
    104         <property name="macOSCmd" value="${ffmpeg.mac}"/>
    105     </bean>
    106 
    107     <context:component-scan
    108             base-package="catf.core.thread.pool,catf.core.handler.frame.spring,catf.core.message.service,catf.core.xml.service">
    109         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    110         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
    111         <context:exclude-filter type="regex" expression="catf.core.handler.frame.spring.HandlerStart"/>
    112     </context:component-scan>
    113     <context:component-scan
    114             base-package="catf.interpreter, catf.loader, catf.executor">
    115         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    116         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
    117         <context:exclude-filter type="regex" expression="catf.executor.record.model.FFmpegConfig"/>
    118     </context:component-scan>
    119 
    120     <!-- component -->
    121     <context:component-scan
    122             base-package="catf.component">
    123         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    124         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
    125         <context:exclude-filter type="regex" expression="catf.executor.record.model.FFmpegConfig"/>
    126     </context:component-scan>
    127     <aop:aspectj-autoproxy proxy-target-class="true" />
    128 
    129     <!-- 扩展组件 -->
    130     <context:component-scan
    131             base-package="catf.expand.component.base">
    132         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    133         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
    134     </context:component-scan>
    135     <context:component-scan
    136             base-package="catf.expand">
    137         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    138         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
    139     </context:component-scan>
    140     <context:component-scan
    141             base-package="catf.expand.component.web">
    142         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    143         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
    144     </context:component-scan>
    145     <context:component-scan
    146             base-package="weshare.token">
    147         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    148         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
    149     </context:component-scan>
    150 
    151 </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
     1 package catf.component.mybatis.manager;
     2 
     3 import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
     4 
     5 
     6 public class ThreadLocalRountingDataSource extends AbstractRoutingDataSource {
     7     @Override
     8     protected Object determineCurrentLookupKey() {
     9         return DataSourcesTypeManager.get();
    10     }
    11 
    12 }
    DataSourcesTypeManager .class
     1 package catf.component.mybatis.manager;
     2 
     3 public class DataSourcesTypeManager {
     4 
     5     private static final ThreadLocal<String> dataSourceTypes = new ThreadLocal<>();
     6 
     7     public static String get() {
     8         return dataSourceTypes.get();
     9     }
    10 
    11     /**
    12      * 设置数据源
    13      */
    14     public static void set(String dataSourceType) {
    15         dataSourceTypes.set(dataSourceType);
    16     }
    17 
    18     /**
    19      * 清除dataSourceKey的值
    20      */
    21     public static void remove() {
    22         dataSourceTypes.remove();
    23     }
    24 
    25 }
  • 相关阅读:
    HomeBrew安装MongoDB如何启动
    Express + Mongoose 极简入门
    Express + Mongoose 极简入门
    浅谈 PHP 与手机 APP 开发(API 接口开发)
    浅谈 PHP 与手机 APP 开发(API 接口开发)
    统计与分布之伯努利分布与二项分布
    统计分布之泊松分布
    统计与分布之高斯分布
    Python 2 和 3 的区别及兼容技巧
    组合与排列
  • 原文地址:https://www.cnblogs.com/gongxr/p/10784174.html
Copyright © 2020-2023  润新知