spring mvc 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:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task"
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.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
default-lazy-init="true">

<description>Spring公共配置</description>
<!-- 导入属性配置文件 -->
<context:property-placeholder location="classpath:property/db.properties" />


<!--################################### 平台数据源配置############################### -->
<!-- C3P0数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${mysql.driverclass}"></property>
<property name="jdbcUrl" value="${mysql.jdbcurl}"></property>
<property name="user" value="${mysql.user}"></property>
<property name="password" value="${mysql.password}"></property>
<property name="acquireIncrement" value="5"></property><!-- 当连接池中的连接用完时,C3P0一次性创建新连接的数目2 -->
<property name="initialPoolSize" value="10"></property><!-- 初始化时创建的连接数,必须在minPoolSize和maxPoolSize之间 -->
<property name="minPoolSize" value="5"></property>
<property name="maxPoolSize" value="20"></property>
<!--
最大空闲时间,超过空闲时间的连接将被丢弃 [需要注意:mysql默认的连接时长为8小时(28800)【可在my.ini中添加 wait_timeout=30(单位秒)设置连接超时】,这里设置c3p0的超时必须<28800]
-->
<property name="maxIdleTime" value="300"></property>
<property name="idleConnectionTestPeriod" value="60"></property><!-- 每60秒检查连接池中的空闲连接 -->
<property name="maxStatements" value="20"></property>
<!--
jdbc的标准参数 用以控制数据源内加载的PreparedStatement数量,但由于预缓存的Statement属
于单个Connection而不是整个连接
-->
</bean>

<!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 -->
<context:component-scan base-package="com.wxws" />
<!--开启注解 有了context:component-scan,@Required,@Autowired自动注入了
<context:annotation-config />-->


<!-- 使用annotation定义事务 -->
<!--
<tx:annotation-driven transaction-manager="transactionManager"
proxy-target-class="true" />
-->
<!-- Spring xml统一管理事务开始 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="query*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="list*" read-only="true" />
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>

<aop:config>
<!-- 多个包可以加 or execution(* cn.tempus.demo.service.*.*(..)) -->
<aop:pointcut id="allManagerMethod" expression="execution(* com.wxws.admin.*.service.*.*(..))" />
<aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice" />
</aop:config>
<!-- Spring xml统一管理事务结束 -->


<!-- MyBatis 配置开始 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 实体的包名 -->
<property name="configLocation" value="classpath:spring/platform-mybatis-config.xml" />
</bean>

<!-- 定义所要扫描的Mapper配置文件包路径,多个包用逗号或者分号隔开-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.wxws.*.*.dao" />
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean>
<!-- MyBatis 配置结束 -->


<!--################################### erp配置############################### -->
<!-- erp数据源 -->
<bean id="erpDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${mysqlerp.driverclass}"></property>
<property name="jdbcUrl" value="${mysqlerp.jdbcurl}"></property>
<property name="user" value="${mysqlerp.user}"></property>
<property name="password" value="${mysqlerp.password}"></property>
<property name="acquireIncrement" value="${acquireIncrement}"></property> <!--当连接池中的连接用完时,C3P0一次性创建新连接的数目2 -->

<property name="initialPoolSize" value="${initialPoolSize}"></property> <!--初始化时创建的连接数,必须在minPoolSize和maxPoolSize之间 -->

<property name="minPoolSize" value="${minPoolSize}"></property>
<property name="maxPoolSize" value="${maxPoolSize}"></property>
<property name="maxIdleTime" value="${maxIdleTime}"></property>
<property name="idleConnectionTestPeriod" value="${idleConnectionTestPeriod}"></property> <!--每60秒检查连接池中的空闲连接 -->
<property name="maxStatements" value="${maxStatements}"></property>
</bean>

<!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 -->
<!-- <context:component-scan base-package="com.wxws.admin" />-->
<!--开启注解 有了context:component-scan,@Required,@Autowired自动注入了
<context:annotation-config />-->

<!-- Spring xml统一管理事务开始 -->
<bean id="erpTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="erpDataSource" />
</bean>

<tx:advice id="erpTxAdvice" transaction-manager="erpTransactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="query*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="list*" read-only="true" />
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>

<aop:config>
<!-- 多个包可以加 or execution(* cn.tempus.demo.service.*.*(..)) -->
<aop:pointcut id="allManagerMethodErp" expression="execution(* com.wxws.*.service.*.*(..))" />
<aop:advisor pointcut-ref="allManagerMethodErp" advice-ref="erpTxAdvice" />
</aop:config>

<!-- MyBatis 配置开始 -->
<bean id="sqlSessionFactoryErp" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="erpDataSource" />
<!-- 实体的包名 -->
<property name="configLocation" value="classpath:spring/erp-mybatis-config.xml" />
</bean>


<!-- 定义所要扫描的Mapper配置文件包路径,多个包用逗号或者分号隔开-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.wxws.*.dao" />
[color=red]<property name="sqlSessionFactory" ref="sqlSessionFactoryErp"></property>[/color] </bean>
<!-- MyBatis 配置结束 -->
</beans>



当初我配置的时候[color=red]<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>[/color]没有配置这个属性,所以多数据源始终不起作用
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值