MyBatis连接两个数据库
最近在做资源管理系统的时候,需要打通两个数据库。
背景,公司已经使用禅道来做项目管理了。在此基础上,我们用java开发了另外一个管理系统,不再基于禅道开发,因为禅道是使用php开发的。
有一个需求,打通原来的数据库,使用的数据库框架是MyBatis,需要连接多个数据库,配置如下:
这是数据库的配置:
devsys.jdbc.url=jdbc:mysql://localhost:3306/devres_manager?zeroDateTimeBehavior=CONVERT_TO_NULL&characterEncoding=UTF-8
devsys.jdbc.driver=com.mysql.jdbc.Driver
devsys.jdbc.user=root
devsys.jdbc.password=123456
zentao.jdbc.url=jdbc:mysql://192.168.0.3:3306/zentao?zeroDateTimeBehavior=CONVERT_TO_NULL
zentao.jdbc.driver=com.mysql.jdbc.Driver
zentao.jdbc.user=root
zentao.jdbc.password=123456
接着呢,是spring的配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xmlns:context="https://www.springframework.org/schema/context"
xmlns:aop="https://www.springframework.org/schema/aop"
xmlns:tx="https://www.springframework.org/schema/tx"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
https://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
https://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
https://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="com.cneeds">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--配置第一个数据库的数据源,这个数据库是开发资源管理折数据库-->
<context:property-placeholder location="classpath:dbconfig.properties"/>
<bean id="devsysDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${devsys.jdbc.user}"/>
<property name="password" value="${devsys.jdbc.password}"/>
<property name="jdbcUrl" value="${devsys.jdbc.url}"/>
<property name="driverClass" value="${devsys.jdbc.driver}"/>
</bean>
<!--Mybatis的整合-->
<bean id="devsysSqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis-devsys-config.xml"/>
<property name="dataSource" ref="devsysDataSource"/>
<property name="mapperLocations" value="classpath:devsysmapper/*.xml"/>
</bean>
<!--配置扫描器,将Mybatis接口的实现加入到ioc容器中-->
<bean id="devsysMapper" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--扫描所有的dao接口的实现。加入到ioc容器中-->
<property name="basePackage" value="com.cneeds.devsysdao"/>
<property name="sqlSessionFactoryBeanName" value="devsysSqlSession"/>
</bean>
<bean id="dataSourceTransactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="devsysDataSource"/>
</bean>
<!--配置事务增强,事务如何切入-->
<tx:advice id="txAdvice" transaction-manager="dataSourceTransactionManager">
<tx:attributes>
<!--所有方法都是事务-->
<tx:method name="*"/>
<!--以get开始的所有方法-->
<tx:method name="get*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!--开启基于注解的事务,或者使用xml配置形式的事务-->
<aop:config>
<!--切入点表达式-->
<aop:pointcut id="txPoint" expression="execution(* com.cneeds.devsysservice..*(..))"/>
<!--配置事务增强-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
</aop:config>
<!--配置第二个数据源,这个数据源是禅道的-->
<bean id="zentaoDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${zentao.jdbc.user}"/>
<property name="password" value="${zentao.jdbc.password}"/>
<property name="jdbcUrl" value="${zentao.jdbc.url}"/>
<property name="driverClass" value="${zentao.jdbc.driver}"/>
</bean>
<bean id="zentaoSqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis-zentao-config.xml"/>
<property name="dataSource" ref="zentaoDataSource"/>
<property name="mapperLocations" value="classpath:zentaomapper/*.xml"/>
</bean>
<!--配置扫描器,将Mybatis接口的实现加入到ioc容器中-->
<bean id="zentaoMapper" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--扫描所有的dao接口的实现。加入到ioc容器中-->
<property name="basePackage" value="com.cneeds.zentaodao"/>
<property name="sqlSessionFactoryBeanName" value="zentaoSqlSession"/>
</bean>
<bean id="zentaoDataSourceTransactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="zentaoDataSource"/>
</bean>
<!--配置事务增强,事务如何切入-->
<tx:advice id="zentaoTxAdvice" transaction-manager="zentaoDataSourceTransactionManager">
<tx:attributes>
<!--所有方法都是事务-->
<tx:method name="*"/>
<!--以get开始的所有方法-->
<tx:method name="get*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!--开启基于注解的事务,或者使用xml配置形式的事务-->
<aop:config>
<!--切入点表达式-->
<aop:pointcut id="zentaoTxPoint" expression="execution(* com.cneeds.zentaoservice..*(..))"/>
<!--配置事务增强-->
<aop:advisor advice-ref="zentaoTxAdvice" pointcut-ref="zentaoTxPoint"/>
</aop:config>
</beans>
这可能跟直接的mybatis配置有点不一样,mybatis的直接配置有事务管理,dataSource之类的,而集成到Spring中以后,则由Spring去管理事务了。