spring+mybatis多数据源的使用

在项目中使用多个数据源,并且在各数据源之间进行读写操作。

在项目database.properties文件里配置多个数据源。

<?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:p="http://www.springframework.org/schema/p"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:mvc="http://www.springframework.org/schema/mvc" 
  xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xsi:schemaLocation="http://www.springframework.org/schema/beans  
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
                        http://www.springframework.org/schema/context  
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd  
                        http://www.springframework.org/schema/mvc  
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
                        http://www.springframework.org/schema/aop
                        http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 自动扫描 -->
    <context:component-scan base-package="com.operation.implement" />
    <!-- 引入配置文件 -->
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:database.properties" />
    </bean>

    <bean id="dataSourceOperation" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="${operation.url}" />
        <property name="username" value="${operation.username}" />
        <property name="password" value="${operation.password}" />
        <!-- 初始化连接大小 -->
        <property name="initialSize" value="${operation.initialSize}"></property>
        <!-- 连接池最大数量 -->
        <property name="maxActive" value="${operation.maxActive}"></property>
        <!-- 连接池最小空闲 -->
        <property name="minIdle" value="${operation.minIdle}"></property>
        <!-- 超过时间限制是否回收 -->
        <property name="removeAbandoned" value="true" />
        <!-- 超时时间;单位为秒。360秒=6分钟 -->
        <property name="removeAbandonedTimeout" value="360" />
        <!-- 关闭abanded连接时输出错误日志 -->
        <property name="logAbandoned" value="true" />
    </bean>
    
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />
        <!-- 初始化连接大小 -->
        <property name="initialSize" value="${initialSize}"></property>
        <!-- 连接池最大数量 -->
        <property name="maxActive" value="${maxActive}"></property>
        <!-- 连接池最小空闲 -->
        <property name="minIdle" value="${minIdle}"></property>
        <!-- 超过时间限制是否回收 -->
        <property name="removeAbandoned" value="true" />
        <!-- 超时时间;单位为秒。360秒=6分钟 -->
        <property name="removeAbandonedTimeout" value="360" />
        <!-- 关闭abanded连接时输出错误日志 -->
        <property name="logAbandoned" value="true" />
    </bean>

    <bean id="sqlSessionFactoryOperation" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSourceOperation" />      
        <property name="mapperLocations" value="classpath:com/operation/data/operation/*.xml"></property>
    </bean>
    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />      
        <property name="mapperLocations" value="classpath:com/operation/data/test/*.xml"></property>
    </bean>
    
     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.operation.dao.operation" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryOperation"/>
    </bean>
    
     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.operation.dao.test" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>
    
    <bean id="sqlSessionOperation" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype">
        <constructor-arg index="0" ref="sqlSessionFactoryOperation"/>
    </bean>
    
     <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype">
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>

    <bean id="transactionManagerOperation" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSourceOperation" />
    </bean>
    <tx:annotation-driven transaction-manager="transactionManagerOperation" proxy-target-class="true" />
    
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
</beans>

以上是spring-mybatis.xml文件的配置文件。dataSourceOperation和dataSource是两个不同的数据源。

<bean id="sqlSessionFactoryOperation" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSourceOperation" />      
        <property name="mapperLocations" value="classpath:com/operation/data/operation/*.xml"></property>
    </bean>
    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />      
        <property name="mapperLocations" value="classpath:com/operation/data/test/*.xml"></property>
    </bean>

可以看出mybatis配置的sql文件在不同的包下。

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.operation.dao.operation" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryOperation"/>
    </bean>
    
     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.operation.dao.test" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>

MapperScannerConfigurer将扫描basePackage所指定的包下的所有接口类(包括子类),如果它们在SQL映射文件中定义过,则将它们动态定义为一个Spring Bean,这样,我们在Service中就可以直接注入映射接口的bean。

MapperScannerConfigurer的理解,参考https://www.cnblogs.com/wade-luffy/p/6094236.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值