最近寫了一個小的web應用,須要Srping Mybatis多數據源搭配工做,根據但數據源配置的狀況,結合網絡上衆多大蝦們的文章指導,完成了spring mybatis多數據源的配置。並在tomcat6+jee6下正常運行。如下是具體的配置狀況。
一、applicationContext.xml web
<?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> spring
<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>
二、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
三、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>
四、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>
五、NewJobMapper.xml
根據本身的業務編寫
六、UsersMapper.xml
根據本身的業務編寫
七、service的編寫
不一樣數據源的,直接注入對應的dao。
sql