springMvc+mybatis多数据源配置
2016-07-13 18:10
453 查看
1、配置多个数据源
这里使用DruidDataSource 作为数据库连接池
https://github.com/AlibabaTech/druid/wiki
新建 dataSource-main.xml
这里使用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"/>
相关文章推荐
- 深入分析Java I/O工作机制
- Java 线程池详解
- java.lang.Integer.toHexString(b[n] & 0XFF)中0XFF使用的必要性
- java线程池主线程等待子线程执行完成后再继续处理后面工作
- Java多线程sleep(),join(),interrupt(),wait(),notify()
- JAVA8之方法引用和构造引用
- 如何停止java线程
- 深入浅出Java多线程(1)-方法 join
- 创建Java多线程的多种方法ouchuquan
- java批量生成excel代码分享
- 对于java中的"\"和"/" 区别
- spring mvc常用的注解
- java 的 AccessController.doPrivileged使用
- java.util.zip
- JAVA利用Zip4j解压缩【转】
- java.util.ResourceBundle使用详解
- java获取指定路径下的指定文件/java.io.File.listFiles(FilenameFilter filter)
- 正确遍历删除List中的元素
- java编程中Properties类的具体作用和使用
- Spring 框架简介