开题
写开题主要是用于记录为什么有这样的需求,而产生这样的用法.在项目中有时候会存在用户需要把不同的数据导入到同一个数据库的问题.
可能存在多个系统,要把其他系统的一部分数据导入到自己的系统,这样就会产生一个系统连接多个数据库的问题.
使用
一个系统连接多个数据库就是需要配置多个数据源.而如果使用Mybatis的话,就需要使用SqlSessionFactoryBean,这样就需要配置多个SqlSessionFactoryBean,通过SqlSessionFactoryBean和对应的DataSource对应起来,就能实现访问多个数据库,而且互不干扰.
因为每一个DataSource对应一个SqlSessionFactoryBean.
具体的xml配置文件如下:
<beans xmlns="http://www.springframework.org/schema/beans"
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">
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:disk.properties"/>
</bean>
<!-- 设置数据源连接池,可设置多个 -->
<bean id="DataSourceMybatis" class="org.apache.tomcat.jdbc.pool.DataSource">
<property name="poolProperties">
<bean class="org.apache.tomcat.jdbc.pool.PoolProperties">
<property name="url" value="${jdbc.url}"/>
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
<property name="initialSize" value="${jdbc.initialSize}"/>
<property name="maxActive" value="${jdbc.maxActive}"/>
<property name="maxIdle" value="${jdbc.maxIdle}"/>
<!-- 空闲连接数量 -->
<property name="maxWait" value="${jdbc.maxWait}"/>
<property name="testOnBorrow" value="true"/>
<property name="testOnReturn" value="true"/>
<property name="testWhileIdle" value="true"/>
<property name="validationQuery" value="SELECT 1"/>
<property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}"/>
<property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}"/>
</bean>
</property>
</bean>
<!-- 设置数据源连接池,可设置多个 -->
<bean id="DataSourceAccess" class="org.apache.tomcat.jdbc.pool.DataSource">
<property name="poolProperties">
<bean class="org.apache.tomcat.jdbc.pool.PoolProperties">
<property name="url" value="${access.url}"/>
<property name="driverClassName" value="${access.driver}"/>
<property name="username" value=""/>
<property name="password" value="5566"/>
<property name="initialSize" value="10"/>
<property name="maxActive" value="50"/>
<property name="maxIdle" value="10"/>
<!-- 空闲连接数量 -->
<property name="maxWait" value="6000"/>
<property name="timeBetweenEvictionRunsMillis" value="28790000"/>
<property name="minEvictableIdleTimeMillis" value="100000"/>
</bean>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="DataSourceMybatis"/>
</bean>
<!--***************开始配置Mybatis*********************-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="DataSourceMybatis"/>
<property name="typeAliasesPackage" value="xxx.xxx.xxx.xxx.entity"/>
<property name="mapperLocations">
<array>
<value>classpath:mapper/oracle/*.xml</value>
</array>
</property>
</bean>
<bean id="accessSqlSessonFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="DataSourceAccess"/>
<property name="typeAliasesPackage" value="xxx.xxx.xxx.xxx.entity.entity"/>
<property name="mapperLocations">
<array>
<value>classpath:mapper/access/*.xml</value>
</array>
</property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="find*" propagation="REQUIRED" read-only="true"/>
<tx:method name="count*" propagation="REQUIRED" read-only="true"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED" isolation="READ_COMMITTED"/>
<tx:method name="update*" propagation="REQUIRED" isolation="READ_COMMITTED"/>
<tx:method name="*" propagation="REQUIRED" read-only="true" isolation="READ_COMMITTED"/>
</tx:attributes>
</tx:advice>
<aop:config proxy-target-class="true">
<aop:pointcut id="tranMethod" expression="execution(* xxx.xxx.xxx.mapper.service..*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="tranMethod"/>
</aop:config>
<!-- 定义扫描mapper的包 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="xxx.xxx.xxx.mapper"/>
<property name="markerInterface" value="xxx.xxx.xxx.xxxx.xxxMapper"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="accessSqlSessonFactory"/>
<property name="basePackage" value="xxx.xxx.xxx.mapper"/>
<property name="markerInterface" value="xxx.xxx.xxx.xxxx.xxxMapper"/>
</bean>
</beans>
上面配置主要就是配置了2个数据源DataSource:
<!-- 设置数据源连接池,可设置多个 -->
<bean id="DataSourceMybatis" class="org.apache.tomcat.jdbc.pool.DataSource">
<property name="poolProperties">
<bean class="org.apache.tomcat.jdbc.pool.PoolProperties">
<property name="url" value="${jdbc.url}"/>
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
<property name="initialSize" value="${jdbc.initialSize}"/>
<property name="maxActive" value="${jdbc.maxActive}"/>
<property name="maxIdle" value="${jdbc.maxIdle}"/>
<!-- 空闲连接数量 -->
<property name="maxWait" value="${jdbc.maxWait}"/>
<property name="testOnBorrow" value="true"/>
<property name="testOnReturn" value="true"/>
<property name="testWhileIdle" value="true"/>
<property name="validationQuery" value="SELECT 1"/>
<property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}"/>
<property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}"/>
</bean>
</property>
</bean>
<!-- 设置数据源连接池,可设置多个 -->
<bean id="DataSourceAccess" class="org.apache.tomcat.jdbc.pool.DataSource">
<property name="poolProperties">
<bean class="org.apache.tomcat.jdbc.pool.PoolProperties">
<property name="url" value="${access.url}"/>
<property name="driverClassName" value="${access.driver}"/>
<property name="username" value=""/>
<property name="password" value="5566"/>
<property name="initialSize" value="10"/>
<property name="maxActive" value="50"/>
<property name="maxIdle" value="10"/>
<!-- 空闲连接数量 -->
<property name="maxWait" value="6000"/>
<property name="timeBetweenEvictionRunsMillis" value="28790000"/>
<property name="minEvictableIdleTimeMillis" value="100000"/>
</bean>
</property>
</bean>
然后因为有2个数据源就需要配置2个SqlSessionFactoryBean:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="DataSourceMybatis"/>
<property name="typeAliasesPackage" value="xxx.xxx.xxx.xxx.entity"/>
<property name="mapperLocations">
<array>
<value>classpath:mapper/oracle/*.xml</value>
</array>
</property>
</bean>
<bean id="accessSqlSessonFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="DataSourceAccess"/>
<property name="typeAliasesPackage" value="xxx.xxx.xxx.xxx.entity.entity"/>
<property name="mapperLocations">
<array>
<value>classpath:mapper/access/*.xml</value>
</array>
</property>
</bean>
2个DataSource对应2个SqlSessionFactoryBean,通过dataSource属性对应起来:
<property name="dataSource" ref="DataSourceMybatis"/>
<property name="dataSource" ref="DataSourceAccess"/>
通过上面配置基本数据源对应就完成了.
下面是配置Mybatis的一些配置指定xml文件的位置
<property name="mapperLocations">
<array>
<value>classpath:mapper/access/*.xml</value>
</array>
</property>
配置Mybatis的xml对应java代码的接口映射文件位置.这样配置以后就不用在代码里使用SqlSessionTemplate写对应xml里面的名字了,可以直接使用java方法名对应xml文件里面的id
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="xxx.xxx.xxx.mapper"/>
<property name="markerInterface" value="xxx.xxx.xxx.xxxx.xxxMapper"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="accessSqlSessonFactory"/>
<property name="basePackage" value="xxx.xxx.xxx.mapper"/>
<property name="markerInterface" value="xxx.xxx.xxx.xxxx.xxxMapper"/>
</bean>
其中markerInterface是告诉Mybatis之映射xxxxMapper的子类.其他类忽略
basePackage标示要扫描的包,如果配置了markerInterface 则basePackage的范围可以配置的广一些.
以上就是一些小记