Spring Mvc 加Mybatis的多数据库访问源配置访问过程如下:
- 在applicationContext.xml进行配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
default-autowire="byType"> <aop:aspectj-autoproxy />
<context:annotation-config />
<context:component-scan base-package="com.dbOne.*.*" />
<!-- <context:component-scan base-package="com.rrt.*.view, com.rrt.*.service, com.rrt.frame.aspect" />
-->
<!-- ============================== 数据库一配置 ==================================== -->
<bean id="dbOneDataSource" name="dbOneDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@192.168.31.20:1521:orcl</value>
</property>
<property name="username">
<value>dbOneUserName</value>
</property>
<property name="password">
<value>dbOnePassword</value>
</property>
</bean> <bean id="rrtSqlSessionFactory" name="rrtSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="rrtDataSource" />
<property name="configLocation" value="classpath:setup/mybatis-config-dbOne.xml" />
</bean> <bean id="rrtMapperScannerConfigurer" name="rrtMapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactory" ref="rrtSqlSessionFactory" />
<property name="basePackage" value="com.rrt.*.dao" />
</bean> <bean id="rrtTransactionManager" name="rrtTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="rrtDataSource" />
</bean> <tx:annotation-driven transaction-manager="rrtTransactionManager" />
<!-- ============================== 数据库二配置 ==================================== -->
<bean id="centerDataSource" name="centerDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@192.168.31.21:1521:orcl</value>
</property>
<property name="username">
<value>dbTwoUserName</value>
</property>
<property name="password">
<value>dbTwoPassword</value>
</property>
</bean> <bean id="centerSqlSessionFactory" name="centerSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="centerDataSource" />
<property name="configLocation" value="classpath:setup/mybatis-config-Two.xml" />
</bean> <bean id="centerMapperScannerConfigurer" name="centerMapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactory" ref="centerSqlSessionFactory" />
<property name="basePackage" value="com.center.*.dao" />
</bean> <bean id="centerTransactionManager" name="centerTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="centerDataSource"></property>
</bean> <tx:annotation-driven transaction-manager="centerTransactionManager" /> <!-- 验证码配置 -->
<bean id="captchaProducer" class="com.google.code.kaptcha.impl.DefaultKaptcha">
<property name="config">
<bean class="com.google.code.kaptcha.util.Config">
<constructor-arg type="java.util.Properties">
<value>
kaptcha.border=no
kaptcha.textproducer.char.space=3
kaptcha.textproducer.char.string=235689
kaptcha.noise.color=yellow
kaptcha.image.width=80
kaptcha.image.height=25
kaptcha.textproducer.font.size=15
kaptcha.noise.impl=com.google.code.kaptcha.impl.NoNoise
kaptcha.textproducer.font.names=Courier, Arial
kaptcha.obscurificator.impl=com.google.code.kaptcha.impl.ShadowGimpy
</value>
</constructor-arg>
</bean>
</property>
</bean> <!-- memcached缓存客户端配置 -->
<bean id="memcachedPool" class="com.danga.MemCached.SockIOPool"
factory-method="getInstance" init-method="initialize">
<constructor-arg>
<value>neeaMemcachedPool</value>
</constructor-arg>
<property name="servers">
<list>
<value>192.168.31.20:11211</value>
</list>
</property>
<property name="initConn">
<value>20</value>
</property>
<property name="minConn">
<value>10</value>
</property>
<property name="maxConn">
<value>50</value>
</property>
<property name="nagle">
<value>false</value>
</property>
<property name="socketTO">
<value>3000</value>
</property>
</bean>
<bean id="memcachedClient" class="com.danga.MemCached.MemCachedClient">
<constructor-arg>
<value>neeaMemcachedPool</value>
</constructor-arg>
</bean>
</beans>
- mybatis-config-One.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>
<settings>
<setting name="cacheEnabled" value="true"/>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="multipleResultSetsEnabled" value="true"/>
<setting name="useColumnLabel" value="true"/>
<setting name="useGeneratedKeys" value="true"/>
<setting name="defaultExecutorType" value="SIMPLE"/>
<setting name="defaultStatementTimeout" value="25000"/>
<setting name="autoMappingBehavior" value="PARTIAL"/>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings> <typeAliases>
<typeAlias type="com.rrtong.login.entity.Login" alias="Login" />
</typeAliases> <mappers>
<mapper resource="./sqlmap/User.xml" />
</mappers>
</configuration>
其中
User.xml的内容为
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.rrtong.login.dao.LoginDao">
<select id="getLoginInfo" parameterType="String" resultType="Login">
select
userName,
userPwd
From person
where code = #{userCode}
</select>
</mapper>
特别说明:两个数据源配置的<property name="basePackage" value="com.rrt.*.dao" />必须不一样,特别是在使用通配符时 ,需要注意不能范围重叠。
- mybatis-config-Two.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>
<settings>
<setting name="cacheEnabled" value="true"/>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="multipleResultSetsEnabled" value="true"/>
<setting name="useColumnLabel" value="true"/>
<setting name="useGeneratedKeys" value="true"/>
<setting name="defaultExecutorType" value="SIMPLE"/>
<setting name="defaultStatementTimeout" value="25000"/>
<setting name="autoMappingBehavior" value="PARTIAL"/>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings> <typeAliases>
<typeAlias type="com.rrtong.common.entity.Menu" alias="Menu" />
</typeAliases> <mappers>
<mapper resource="./sqlmap/Menu.xml" />
</mappers>
</configuration>
其中Menu.xml的内容为:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.rrtong.common.dao.MenuDao">
<select id="getMenuList" resultType="Menu">
select menuId, title, url
from MENU
where PARTENTID = 0
</select>
<select id="getSubMenuList" parameterType="String" resultType="Menu">
select menuId, title, url
from MENU
where PARTENTID = #{parentId}
</select>
</mapper>