Spring多数据源配置方式一

spring多数据源配置方式一,为每个数据源单独创建数据源、sessionFactory、事物管理等

一、数据源配置

1. 数据源1配置文件

<?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:context="http://www.springframework.org/schema/context"  
    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.xsd  
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
     <!-- 数据库连接池配置 -->
     <bean id="dataSourceTask" name="dataSourceTask" class="com.mchange.v2.c3p0.ComboPooledDataSource">  
        <!-- 指定连接数据库的驱动-->  
        <property name="driverClass" value="${db.task.jdbc.driverClassName}"/>  
        <!-- 指定连接数据库的URL-->  
        <property name="jdbcUrl" value="${db.task.jdbc.url}"/>  
        <!-- 指定连接数据库的用户名-->  
        <property name="user" value="${db.task.jdbc.user}"/>  
        <!-- 指定连接数据库的密码-->  
        <property name="password" value="${db.task.jdbc.password}"/>  
        <!-- 指定连接池中保留的最大连接数. Default:15-->  
        <property name="maxPoolSize" value="${db.task.jdbc.maxPoolSize}"/>  
        <!-- 指定连接池中保留的最小连接数-->  
        <property name="minPoolSize" value="${db.task.jdbc.minPoolSize}"/>  
        <!-- 指定连接池的初始化连接数  取值应在minPoolSize 与 maxPoolSize 之间.Default:3-->  
        <property name="initialPoolSize" value="${db.task.jdbc.initialPoolSize}"/>  
        <!-- 最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。 Default:0-->  
        <property name="maxIdleTime" value="${db.task.jdbc.maxIdleTime}"/>  
        <!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数. Default:3-->  
        <property name="acquireIncrement" value="${db.task.jdbc.acquireIncrement}"/>  
        <!-- JDBC的标准,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements属于单个connection
        	而不是整个连接池所以设置这个参数需要考虑到多方面的因数.如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default:0-->  
        <property name="maxStatements" value="${db.task.jdbc.maxStatements}"/>  
        <!-- 每60秒检查所有连接池中的空闲连接.Default:0 -->  
        <property name="idleConnectionTestPeriod" value="${db.task.jdbc.idleConnectionTestPeriod}"/>
        <!--连接池获取新连接的时间,超时后将 抛出SQLException,如设为0则无限期等待。单位毫秒。Default: 0 -->
		<property name="checkoutTimeout" value="${db.task.jdbc.checkoutTimeout}"/>  
    </bean>
    
    <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
     <bean id="sqlSessionFactoryTask" class="org.mybatis.spring.SqlSessionFactoryBean">
     	<property name="dataSource" ref="dataSourceTask" />
     	<!-- 自动扫描mapping.xml文件 -->
     	<property name="mapperLocations" value="classpath:/mapper_task/*.xml"/>
     </bean>
     
     <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
     	<property name="basePackage" value="com.johnfnash.learn.pro.dashboardserver.dao.task" />
     	<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryTask"/>
     </bean>
     
     <!-- 配置事物管理器 -->
     <bean id="transactionManagerTask" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
     	<property name="dataSource" ref="dataSourceTask" />
     </bean>
     
     <!-- 拦截器方式配置事物 -->
     <tx:advice id="transactionAdviceTask" transaction-manager="transactionManagerTask">
		<tx:attributes>
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="insert*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			
			<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="select*" propagation="SUPPORTS" read-only="true" />
		</tx:attributes>     	
     </tx:advice>
     
     <!-- spring aop事物管理 -->
     <aop:config>
     	<aop:pointcut id="transactionPointcutTask" 
     		expression="execution(* com.johnfnash.learn.pro.dashboardserver.service..*Impl.*(..))" />
     	<aop:advisor pointcut-ref="transactionPointcutTask" advice-ref="transactionAdviceTask" />
     </aop:config>
</beans>
2. 数据源2配置文件 datasource_environ.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:context="http://www.springframework.org/schema/context"  
    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.xsd  
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
     <!-- 数据库连接池配置 -->
     <bean id="dataSourceLocal" name="dataSourceEnv" class="com.mchange.v2.c3p0.ComboPooledDataSource">  
        <!-- 指定连接数据库的驱动-->  
        <property name="driverClass" value="${db.lcmyw.jdbc.driverClassName}"/>  
        <!-- 指定连接数据库的URL-->  
        <property name="jdbcUrl" value="${db.lcmyw.jdbc.url}"/>  
        <!-- 指定连接数据库的用户名-->  
        <property name="user" value="${db.lcmyw.jdbc.user}"/>  
        <!-- 指定连接数据库的密码-->  
        <property name="password" value="${db.lcmyw.jdbc.password}"/>  
        <!-- 指定连接池中保留的最大连接数. Default:15-->  
        <property name="maxPoolSize" value="${db.lcmyw.jdbc.maxPoolSize}"/>  
        <!-- 指定连接池中保留的最小连接数-->  
        <property name="minPoolSize" value="${db.lcmyw.jdbc.minPoolSize}"/>  
        <!-- 指定连接池的初始化连接数  取值应在minPoolSize 与 maxPoolSize 之间.Default:3-->  
        <property name="initialPoolSize" value="${db.lcmyw.jdbc.initialPoolSize}"/>  
        <!-- 最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。 Default:0-->  
        <property name="maxIdleTime" value="${db.lcmyw.jdbc.maxIdleTime}"/>  
        <!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数. Default:3-->  
        <property name="acquireIncrement" value="${db.lcmyw.jdbc.acquireIncrement}"/>  
        <!-- JDBC的标准,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements属于单个connection
        	而不是整个连接池所以设置这个参数需要考虑到多方面的因数.如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default:0-->  
        <property name="maxStatements" value="${db.lcmyw.jdbc.maxStatements}"/>  
        <!-- 每60秒检查所有连接池中的空闲连接.Default:0 -->  
        <property name="idleConnectionTestPeriod" value="${db.lcmyw.jdbc.idleConnectionTestPeriod}"/>
        <!--连接池获取新连接的时间,超时后将 抛出SQLException,如设为0则无限期等待。单位毫秒。Default: 0 -->
		<property name="checkoutTimeout" value="${db.lcmyw.jdbc.checkoutTimeout}"/>  
    </bean>
    
    <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
     	<property name="dataSource" ref="dataSourceLocal" />
     	<!-- 自动扫描mapping.xml文件 -->
     	<property name="mapperLocations" value="classpath:/mapper_env/*.xml"/>
     </bean>
     
     <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
     	<property name="basePackage" value="com.johnfnash.learn.pro.dashboardserver.dao.env" />
     	<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
     </bean>
     
     <!-- 配置事物管理器 -->
     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
     	<property name="dataSource" ref="dataSourceLocal" />
     </bean>
     
     <!-- 拦截器方式配置事物 -->
     <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="insert*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			
			<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="select*" propagation="SUPPORTS" read-only="true" />
		</tx:attributes>     	
     </tx:advice>
     
     <!-- spring aop事物管理 -->
     <aop:config>
     	<aop:pointcut id="transactionPointcut" 
     		expression="execution(* com.johnfnash.learn.pro.dashboardserver.service..*Impl.*(..))" />
     	<aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
     </aop:config>
</beans>

注意:使用这种方式时,设置sessionFactory时,为不同的数据库设置不同的 mapperLocations, spring扫描mapper接口的 MapperScannerConfigurer设置成不同的包名
示例如下:



二、优缺点

优点:同一service层中可以直接使用不同数据源的Mapper,灵活;数据源为不同类型时,可以为每一个数据源单独设置数据库方言

缺点:需要为每一个数据源创建sessionFactory、事物管理等,比较麻烦

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值