zoukankan      html  css  js  c++  java
  • Spring + Mybatis 配置多个数据源

    有的时候一个项目中需要同时访问两个数据库,而mybatis的默认配置是不行的,这就需要我们修改一下mybatis的配置文件。

     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
        <property name="url" value="${center.connectionURL}"/>
        <property name="username"  value="${userName}"/>
        <property name="password" value="${password}"/>
    </bean>
    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.xxx.dao.center"/>
        <property name="sqlSessionFactoryBeanName" value="cneterSqlSessionFactory"/>
    </bean>
    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" name="cneterSqlSessionFactory">
        <property name="dataSource" ref="dataSource"></property>
        <property name="mapperLocations" value="classpath*:mapperConfig/center/*.xml"/>
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>
    
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager"/>
    <!--center db end-->
    <!--exdb-->
    <bean id="dataSourceEx" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
        <property name="url" value="${ex.connectionURL}"/>
        <property name="username"  value="${userName}"/>
        <property name="password" value="${password}"/>
    </bean>
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.xxx.dao.ex"/>
        <property name="sqlSessionFactoryBeanName" value="exSqlSessionFactory"/>
    </bean>
    <bean id="sqlSessionFactoryEx" class="org.mybatis.spring.SqlSessionFactoryBean" name="exSqlSessionFactory">
        <property name="dataSource" ref="dataSourceEx"></property>
        <property name="mapperLocations" value="classpath*:mapperConfig/ex/*.xml"/>
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>
    <bean id="transactionManagerEx" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSourceEx"/>
    </bean>

    有两个数据源的时候就必须指定org.mybatis.spring.mapper.MapperScannerConfigurer类的sqlSessionFactoryBeanName属性了。
  • 相关阅读:
    PowerShell尝试ssh登录
    PowerShell收发TCP消息包
    powershell对指定IP进行端口扫描
    PowerShell尝试登录ftp
    PowerShell批量扫描IP和端口
    《PowerShell 3.0 Advanced Admin handbook》已于今日上市
    PowerShell尝试登录SQL Server
    Docker 数据卷
    Dockerfile自定义镜像
    Docker 容器操作
  • 原文地址:https://www.cnblogs.com/Bengi/p/6814432.html
Copyright © 2011-2022 走看看