您的位置:首页 > 编程语言 > Java开发

springMvc+mybatis多数据源配置

2016-07-13 18:10 453 查看
1、配置多个数据源

这里使用DruidDataSource 作为数据库连接池
https://github.com/AlibabaTech/druid/wiki
新建 dataSource-main.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" default-autowire="byName">
<!--配置不同的数据库连接信息-->
<bean id="mysqlDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="driverClassName"><value>${jdbc.driverClassName}</value></property>
<property name="url"><value>${jdbc.url}</value></property>
<property name="username"><value>${jdbc.username}</value></property>
<property name="password"><value>${jdbc.password}</value></property>
<property name="maxActive"><value>${jdbc.maxActive}</value></property>
<property name="maxWait"><value>${jdbc.maxWait}</value></property>
<property name="maxIdle"><value>${jdbc.maxIdle}</value></property>
<property name="initialSize"><value>${jdbc.initSize}</value></property>
<property name="removeAbandoned"><value>true</value></property>
<property name="removeAbandonedTimeout"><value>180</value></property>
<property name="validationQuery"><value>${jdbc.validationQuery}</value></property>
</bean>

<bean id="mysqlResourceSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" autowire="byName">
<property name="mapperLocations" value="classpath:sqlmap/*.xml" /><!--扫描指定的xml文件-->
<property name="dataSource" ref="mysqlDataSource" />
</bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.xxx.api.dao" /><!--扫描指定的dao-->
<property name="sqlSessionFactoryBeanName" value="mysqlResourceSqlSessionFactory" />
</bean>

<bean id="mysqlTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="mysqlDataSource" />
</bean>

<tx:advice id="mysqlTxAdvice" transaction-manager="mysqlTransactionManager">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="do*" propagation="REQUIRED" />
<tx:method name="create*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="batch*" propagation="REQUIRED" />
<tx:method name="make*" propagation="REQUIRED" />
<tx:method name="close*" propagation="REQUIRED" />
<tx:method name="reset*" propagation="REQUIRED" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:aspectj-autoproxy />
<aop:config>
<aop:pointcut id="mysqlAllManagerMethod" expression="execution(* com.xxx.api.dao.*.*(..))" /><!--扫描指定的dao-->
<aop:advisor advice-ref="mysqlTxAdvice" pointcut-ref="mysqlAllManagerMethod" />
</aop:config>
</beans>
新建dataSource-branch.xml文件
<pre name="code" class="html"><?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" default-autowire="byName">
<!--配置不同的数据库连接信息-->
<bean id="branchDataSource" class="com.epeit.api.dto.MyBasicDataSource" init-method="init" destroy-method="close">
<property name="driverClassName"><value>${jdbc.driverClassName}</value></property>
<property name="url"><value>${jj.jdbc.url}</value></property>
<property name="username"><value>${jj.jdbc.username}</value></property>
<property name="password"><value>${jj.jdbc.password}</value></property>
<property name="maxActive"><value>${jdbc.maxActive}</value></property>
<property name="maxWait"><value>${jdbc.maxWait}</value></property>
<property name="maxIdle"><value>${jdbc.maxIdle}</value></property>
<property name="initialSize"><value>${jdbc.initSize}</value></property>
<property name="removeAbandoned"><value>true</value></property>
<property name="removeAbandonedTimeout"><value>180</value></property>
<property name="validationQuery"><value>${jdbc.validationQuery}</value></property>
</bean>

<bean id="branchSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" autowire="byName">
<property name="mapperLocations" value="classpath:otherSqlMap/*.xml" /><!--配置扫描不同的xm-->
<property name="dataSource" ref="branchDataSource" />
</bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.xxx.api.otherDao" /><!--配置扫描不同的dao-->
<property name="sqlSessionFactoryBeanName" value="branchSqlSessionFactory" />
</bean>

<bean id="branchTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="branchDataSource" />
</bean>

<tx:advice id="branchServerTxAdvice" transaction-manager="branchTransactionManager">
<tx:attrib
4000
utes>
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="do*" propagation="REQUIRED" />
<tx:method name="create*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="batch*" propagation="REQUIRED" />
<tx:method name="make*" propagation="REQUIRED" />
<tx:method name="close*" propagation="REQUIRED" />
<tx:method name="reset*" propagation="REQUIRED" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:aspectj-autoproxy />
<aop:config>
<aop:pointcut id="branchAllManagerMethod" expression="execution(* com.xxx.api.otherDao.*.*(..))" />
<aop:advisor advice-ref="branchServerTxAdvice" pointcut-ref="branchAllManagerMethod" />
</aop:config>
</beans>



</pre><pre>
2、在applicationContext.xml中引入这两个配置好的xml文件


<import resource="dataSource-main.xml"/>
<import resource="dataSource-branch.xml"/>






                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: