spring+mybatis 多数据库配置
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring+mybatis 多数据库配置相关的知识,希望对你有一定的参考价值。
这几天需要在ssm项目中配置多一个数据库,就去网上找多数据源的例子,发现都没有成功就自己试了下自己写的
原理:
创建两个数据源,两个seesionFactory,两个事务管理器,然后两个事务管理器分别配置aop,扫描不同的package(网上看到的都是使用注解方式,这里使用aop方式)
1.环境:
Mybatis + springmvc +spring + druid + log4j
如果没使用druid 的话将将数据源的bean 修改回org.apache.commons.dbcp.BasicDataSource 或者 pool之类的
2.jar包:
mysql-connector-java-5.1.10-bin.jar
ojdbc14_11g.jar
sqljdbc4.jar
分别是 mysql oracle sql server 数据库的驱动包
云盘地址:http://pan.baidu.com/s/1qXrPza8
3.配置文件
mvc-context.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd "> <!-- 开启注解 --> <context:component-scan base-package="cn.firstflag.crm.*" /> <mvc:annotation-driven /> </beans>
root-context.xml oracle数据库的数据源
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd "> <!-- 加载db.properties --> <bean id="configBean" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"><value>/WEB-INF/config/db.properties</value></property> </bean> <!-- 配置DataSource数据源 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <property name="driverClassName" value="${mysql.driverClassName}" /> <property name="url" value="${mysql.url}" /> <property name="username" value="${mysql.username}" /> <property name="password" value="${mysql.password}" /> <property name="maxActive" value="${mysql.maxActive}" /> <property name="maxIdle" value="${mysql.maxIdle}" /> <property name="maxWait" value="${mysql.maxWait}" /> <property name="defaultAutoCommit" value="${mysql.defaultAutoCommit}" /> <property name="removeAbandoned" value="${mysql.removeAbandoned}" /> <property name="removeAbandonedTimeout" value="${mysql.removeAbandonedTimeout}" /> </bean> <!-- 创建SqlSessionFactory,同时指定数据源 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="/WEB-INF/config/local-config/mybatis-config.xml"></property> </bean> <!-- 扫描dao --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.xxxx.crm.dao"></property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> <!-- ================================事务相关控制================================================= --> <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 注解方式配置事物 --> <!-- <tx:annotation-driven transaction-manager="transactionManager" /> --> <!-- 拦截器方式配置事物 --> <tx:advice id="transactionAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="append*" propagation="REQUIRED" /> <tx:method name="insert*" propagation="REQUIRED" /> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="modify*" propagation="REQUIRED" /> <tx:method name="edit*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="remove*" propagation="REQUIRED" /> <tx:method name="repair" propagation="REQUIRED" /> <tx:method name="delAndRepair" propagation="REQUIRED" /> <tx:method name="get*" propagation="SUPPORTS" /> <tx:method name="find*" propagation="SUPPORTS" /> <tx:method name="load*" propagation="SUPPORTS" /> <tx:method name="search*" propagation="SUPPORTS" /> <tx:method name="datagrid*" propagation="SUPPORTS" /> <tx:method name="*" propagation="SUPPORTS" /> </tx:attributes> </tx:advice> <!-- 设置只拦截oracle数据库管理的service包 --> <aop:config> <aop:pointcut id="localServiceTran" expression="execution(* cn.firstflag.crm.service.*.*(..))"/> <aop:advisor pointcut-ref="localServiceTran" advice-ref="transactionAdvice" /> </aop:config> </beans>
root-context.xml oracle数据库的数据源
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd "> <!-- 加载db.properties --> <bean id="configBean" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"><value>/WEB-INF/config/db.properties</value></property> </bean> <!-- 配置DataSource数据源 --> <bean id="dataSource_oa" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="driverClassName" value="${oa.driverClassName}" /> <property name="url" value="${oa.url}" /> <property name="username" value="${oa.username}" /> <property name="password" value="${oa.password}" /> <property name="maxActive" value="${oa.maxActive}" /> <property name="maxIdle" value="${oa.maxIdle}" /> <property name="maxWait" value="${oa.maxWait}" /> <property name="defaultAutoCommit" value="${oa.defaultAutoCommit}" /> <property name="removeAbandoned" value="${oa.removeAbandoned}" /> <property name="removeAbandonedTimeout" value="${oa.removeAbandonedTimeout}" /> </bean> <!-- 创建SqlSessionFactory,同时指定数据源 --> <bean id="sqlSessionFactory_oa" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource_oa" /> <property name="configLocation" value="/WEB-INF/config/oa-config/mybatis-config-oa.xml"></property> </bean> <!-- 扫描dao --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.firstflag.crm.oa.dao"></property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory_oa"/> </bean> <!-- ================================事务相关控制================================================= --> <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource_oa"></property> </bean> <!-- 注解方式配置事物 --> <!-- <tx:annotation-driven transaction-manager="transactionManager" /> --> <!-- 拦截器方式配置事物 --> <tx:advice id="transactionAdvice_oa" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="append*" propagation="REQUIRED" /> <tx:method name="insert*" propagation="REQUIRED" /> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="modify*" propagation="REQUIRED" /> <tx:method name="edit*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="remove*" propagation="REQUIRED" /> <tx:method name="repair" propagation="REQUIRED" /> <tx:method name="delAndRepair" propagation="REQUIRED" /> <tx:method name="get*" propagation="SUPPORTS" /> <tx:method name="find*" propagation="SUPPORTS" /> <tx:method name="load*" propagation="SUPPORTS" /> <tx:method name="search*" propagation="SUPPORTS" /> <tx:method name="datagrid*" propagation="SUPPORTS" /> <tx:method name="*" propagation="SUPPORTS" /> </tx:attributes> </tx:advice> <!-- 设置只拦截sqlserver数据库管理的service包 --> <aop:config> <aop:pointcut id="localServiceTran_oa" expression="execution(* cn.xxxx.crm.oa.service.*.*(..))"/> <aop:advisor pointcut-ref="localServiceTran_oa" advice-ref="transactionAdvice_oa" /> </aop:config> </beans>
db.properties
local.environment=oracle mysql.driverClassName=oracle.jdbc.driver.OracleDriver mysql.url=jdbc\:oracle\:thin\:@10.0.1.35\:1521\:orcl mysql.password=qifeng2015 mysql.username=crm mysql.maxActive=5 mysql.maxIdle=3 mysql.maxWait=1000 mysql.defaultAutoCommit=true mysql.removeAbandoned=true mysql.removeAbandonedTimeout=60 oa.environment=mssql oa.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver oa.url=jdbc:sqlserver://10.0.21.229:1433;DatabaseName=v3x oa.username=sa oa.password=1 oa.maxActive=5 oa.maxIdle=3 oa.maxWait=1000 oa.defaultAutoCommit=true oa.removeAbandoned=true oa.removeAbandonedTimeout=60
5.其他问题
有个要注意的是sql server的url 是 jdbc:sqlserver://ip:port;DatabaseName=xxx 这种格式,而不是jdbc:sqlserver://ip:port;DatabaseName=xxx
还有driverClassName 为com.microsoft.sqlserver.jdbc.SQLServerDriver,而不是com.microsoft.jdbc.sqlserver.SQLServerDriver
显示classnotfound的话请使用附件的sqlserver驱动包
以上是关于spring+mybatis 多数据库配置的主要内容,如果未能解决你的问题,请参考以下文章
spring整合myBatis配置数据源老报错,求大神帮忙看看
MyBatis整合Spring开发+MyBatis多对多关联查询