最近写了一个小的web应用,需要Srping Mybatis多数据源搭配工作,根据但数据源配置的情况,结合网络上众多大虾们的文章指导,完成了spring mybatis多数据源的配置。并在tomcat6+jee6下正常运行。以下是具体的配置情况。
1、applicationContext.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:context="http://www.springframework.org/schema/context"
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/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-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:annotation-config />
<!-- 对包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
<context:component-scan base-package="com.jobfairs">
<context:include-filter type="regex" expression="com.jobfairs.service.*.*" />
<context:include-filter type="regex" expression="com.jobfairs.controller.*.*" />
</context:component-scan>
<!-- 数据源properties文件 -->
<context:property-placeholder location="classpath:/mybatis-oracle.properties" />
<!-- 数据源1 -->
<bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${dataSource.driver}" />
<property name="url" value="${dataSource.url}" />
<property name="username" value="${dataSource.username}" />
<property name="password" value="${dataSource.password}" />
<!-- Connection Pooling Info -->
<property name="initialSize" value="5" />
<property name="maxActive" value="100" />
<property name="maxIdle" value="30" />
<property name="maxWait" value="500" />
<property name="poolPreparedStatements" value="true" />
<property name="maxOpenPreparedStatements" value="10" />
<property name="removeAbandonedTimeout" value="300" />
<property name="logAbandoned" value="true" />
<!-- <property name="validationQuery" value="select 1 FROM DUAL" /> -->
<property name="testOnBorrow" value="true" />
<property name="testWhileIdle" value="true" />
<property name="testOnReturn" value="true" />
</bean>
<!-- 数据源2 -->
<bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${dataSource2.driver}" />
<property name="url" value="${dataSource2.url}" />
<property name="username" value="${dataSource2.username}" />
<property name="password" value="${dataSource2.password}" />
<!-- Connection Pooling Info -->
<property name="initialSize" value="5" />
<property name="maxActive" value="100" />
<property name="maxIdle" value="30" />
<property name="maxWait" value="500" />
<property name="poolPreparedStatements" value="true" />
<property name="maxOpenPreparedStatements" value="10" />
<property name="removeAbandonedTimeout" value="300" />
<property name="logAbandoned" value="true" />
<!-- <property name="validationQuery" value="select 1 FROM DUAL" /> -->
<property name="testOnBorrow" value="true" />
<property name="testWhileIdle" value="true" />
<property name="testOnReturn" value="true" />
</bean>
<!-- 一个SqlSessionFactory 1 -->
<bean id="SqlSessionFactory1" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:/mybatis-config.xml" />
<property name="dataSource" ref="dataSource1" />
</bean>
<!-- 一个SqlSessionFactory 2。 -->
<bean id="SqlSessionFactory2" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:/mybatis-config1.xml" />
<property name="dataSource" ref="dataSource2" />
</bean>
<!-- 事务管理器 1-->
<bean id="TransactionManager1" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource1" />
</bean>
<!-- 事务管理器2 -->
<bean id="TransactionManager2" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource2" />
</bean>
<!-- Mybatis 通用DAO Bean 1-->
<bean id="mybatisDao" class="com.jobfairs.common.GeneralDao">
<property name="sqlSessionFactory" ref="SqlSessionFactory1"></property>
</bean>
<!-- Mybatis 通用DAO Bean 2-->
<bean id="mybatisDao1" class="com.jobfairs.common.GeneralDao1">
<property name="sqlSessionFactory" ref="SqlSessionFactory2"></property>
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver" />
<!--配置视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="cache" value="false" />
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
<property name="viewClass">
<value>org.springframework.web.servlet.view.InternalResourceView</value>
</property>
</bean>
<!-- 注解支持 -->
<mvc:annotation-driven />
<bean class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<aop:aspectj-autoproxy/>
<bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />
</beans>
2、mybatis-oracle.properties
#data source 1
dataSource.driver=oracle.jdbc.driver.OracleDriver
dataSource.url=jdbc:oracle:thin:@192.168.253.3:1521:orcl
dataSource.username=jobfairs
dataSource.password=123456
#data Source 2
dataSource2.driver=oracle.jdbc.driver.OracleDriver
dataSource2.url=jdbc:oracle:thin:@192.168.253.16:1521:orcl
dataSource2.username=xajobinspur
dataSource2.password=xajobinspur
3、mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties>
<property name="dialect" value="oracle"/>
</properties>
<settings>
<!-- 这个配置使全局的映射器启用或禁用缓存 -->
<setting name="cacheEnabled" value="true" />
<!-- 允许 JDBC 支持生成的键。需要适合的驱动。如果设置为 true 则这个设置强制生成的键被使用 -->
<setting name="useGeneratedKeys" value="false" />
<!-- 配置默认的执行器。SIMPLE 执行器没有什么特别之处。REUSE 执行器重用预处理语句。-->
<!-- BATCH 执行器重用语句和批量更新 -->
<setting name="defaultExecutorType" value="REUSE" />
<!-- 全局启用或禁用延迟加载。当禁用时,所有关联对象都会即时加载。 -->
<setting name="lazyLoadingEnabled" value="true" />
<!-- 设置超时时间,它决定驱动等待一个数据库响应的时间。 -->
<setting name="defaultStatementTimeout" value="25000" />
</settings>
<!-- 别名配置 -->
<typeAliases>
<typeAlias type="com.jobfairs.domain.Users" alias="users"/>
</typeAliases>
<!-- 指定映射器路径 -->
<mappers>
<mapper resource="com/jobfairs/domain/maps/UsersMapper.xml"/>
</mappers>
</configuration>
4、mybatis-config1.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties>
<property name="dialect" value="oracle"/>
</properties>
<!-- 别名配置 -->
<typeAliases>
<typeAlias type="com.jobfairs.domain.NewJob" alias="newJob"/>
</typeAliases>
<!-- 指定映射器路径 -->
<mappers>
<mapper resource="com/jobfairs/domain/maps/NewJobMapper.xml"/>
</mappers>
</configuration>
5、NewJobMapper.xml
根据自己的业务编写
6、UsersMapper.xml
根据自己的业务编写
7、service的编写
不同数据源的,直接注入对应的dao。
来源:oschina
链接:https://my.oschina.net/u/1038891/blog/176463