之前写过一种多数据源配置的方式,但是那种方式对代码入侵性比较大,详情请查阅
mybatis + spring 多数据源跨库查询
最近在做 springMVC 搭建时,更改了新的实现,并且提供多数据源中单个数据源事务的支持
下面直接放完整代码:
配置
-
<?xml version="1.0" encoding="UTF-8"?>
-
<web-app xmlns:xsi="http:///2001/XMLSchema-instance"
-
xmlns="/xml/ns/javaee"
-
xsi:schemaLocation="/xml/ns/javaee /xml/ns/javaee/web-app_3_1.xsd"
-
id="WebApp_ID" version="3.1">
-
<display-name>testweb</display-name>
-
<welcome-file-list>
-
<welcome-file></welcome-file>
-
<welcome-file></welcome-file>
-
<welcome-file></welcome-file>
-
<welcome-file></welcome-file>
-
<welcome-file></welcome-file>
-
<welcome-file></welcome-file>
-
</welcome-file-list>
-
-
<!-- webAppRootKey -->
-
<context-param>
-
<param-name>webAppRootKey</param-name>
-
<param-value></param-value>
-
</context-param>
-
-
<!-- ========== Log4j 监听器 ========== -->
-
<listener>
-
<listener-class>.Log4jConfigListener</listener-class>
-
</listener>
-
<context-param>
-
<param-name>log4jConfigLocation</param-name>
-
<param-value>classpath:config/log4j/</param-value>
-
</context-param>
-
-
<!-- spring mvc -->
-
<servlet>
-
<servlet-name>springmvc</servlet-name>
-
<servlet-class></servlet-class>
-
<init-param>
-
<param-name>contextConfigLocation</param-name>
-
<param-value>classpath:config/spring/</param-value>
-
</init-param>
-
<load-on-startup>1</load-on-startup>
-
</servlet>
-
<servlet-mapping>
-
<servlet-name>springmvc</servlet-name>
-
<url-pattern>/</url-pattern>
-
</servlet-mapping>
-
-
<!-- ========== spring 配置文件 ========== -->
-
<context-param>
-
<param-name>contextConfigLocation</param-name>
-
<param-value>
-
classpath:config/spring/
-
</param-value>
-
</context-param>
-
-
<listener>
-
<listener-class></listener-class>
-
</listener>
-
-
<!-- ========== session 超时 ========== -->
-
<session-config>
-
<session-timeout>60</session-timeout>
-
</session-config>
-
-
<error-page>
-
<error-code>404</error-code>
-
<location>/page/common/error/</location>
-
</error-page>
-
<error-page>
-
<error-code>500</error-code>
-
<location>/page/common/error/</location>
-
</error-page>
-
</web-app>
配置文件
-
<?xml version="1.0" encoding="UTF-8"?>
-
<beans xmlns="/schema/beans"
-
xmlns:xsi="http:///2001/XMLSchema-instance" xmlns:context="/schema/context"
-
xmlns:tx="/schema/tx" xmlns:mvc="/schema/mvc"
-
xsi:schemaLocation="/schema/beans
-
/schema/beans/
-
/schema/context
-
/schema/context/
-
/schema/tx
-
/schema/tx/
-
/schema/mvc
-
/schema/mvc/">
-
-
<!-- 注册HandlerMapper、HandlerAdapter两个映射类 -->
-
<mvc:annotation-driven />
-
<context:component-scan base-package=".*"
-
use-default-filters="false">
-
<context:include-filter type="annotation"
-
expression="" />
-
</context:component-scan>
-
-
<!-- 访问静态资源 -->
-
<mvc:default-servlet-handler />
-
-
<!-- 视图解析器 -->
-
<bean
-
class="">
-
<property name="prefix" value="/page/"></property>
-
<property name="suffix" value=".jsp"></property>
-
</bean>
-
-
</beans>
配置
-
<?xml version="1.0" encoding="UTF-8"?>
-
<beans xmlns="/schema/beans"
-
xmlns:xsi="http:///2001/XMLSchema-instance" xmlns:aop="/schema/aop"
-
xmlns:context="/schema/context" xmlns:tx="/schema/tx"
-
xsi:schemaLocation="/schema/beans
-
/schema/beans/spring-beans-4.
-
/schema/aop /schema/aop/spring-aop-4.
-
/schema/context /schema/context/spring-context-4.
-
/schema/tx /schema/tx/spring-tx-4.">
-
-
<!-- 读取数据源配置文件 -->
-
<bean id="dataSoucresConfig"
-
class="">
-
<property name="locations"
-
value="classpath:config/properties/" />
-
</bean>
-
-
<!-- 使用annotation自动注册bean,并保证@Required,@Autowired的属性被注入 -->
-
<context:annotation-config />
-
<context:component-scan base-package=".*">
-
<context:exclude-filter type="annotation"
-
expression="" />
-
</context:component-scan>
-
-
<!-- 自动AOP切面 -->
-
<aop:aspectj-autoproxy />
-
-
<bean id="baseDataSource" class=".v2."
-
destroy-method="close">
-
<property name="driverClass" value="${}"/>
-
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
-
<property name="acquireIncrement" value="${}"/>
-
<!--初始化时获取连接个数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
-
<property name="initialPoolSize" value="${}"/>
-
<!--最大空闲时间內未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
-
<property name="maxIdleTime" value="${}"/>
-
<!--连接池中保留的最大连接数。Default: 15 -->
-
<property name="maxPoolSize" value="${}"/>
-
<!--连接池中保留的最小连接数。-->
-
<property name="minPoolSize" value="${}"/>
-
-
<!--两次连接中间隔时间,单位毫秒。Default: 1000 -->
-
<property name="acquireRetryDelay" value="${}"/>
-
<!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->
-
<property name="acquireRetryAttempts" value="${}"/>
-
<!-- 为 true 时 pool向数据库请求连接失败后标记整个pool为block并close -->
-
<property name="breakAfterAcquireFailure" value="${}"/>
-
</bean>
-
-
<!-- ========== 数据源一:mvc DBSource ========== -->
-
<bean id="mvcDBSource" parent="baseDataSource">
-
<property name="jdbcUrl" value="${}"/>
-
<property name="user" value="${}"/>
-
<property name="password" value="${}" />
-
</bean>
-
-
<bean id="mvcSqlSession" class="">
-
<property name="dataSource" ref="mvcDBSource" />
-
<property name="configLocation" value="classpath:config/mybatis/" />
-
<property name="mapperLocations">
-
<list>
-
<value>classpath*:config/mybatis/sqlMapper/common/*.xml</value>
-
<value>classpath*:config/mybatis/sqlMapper/webview/*.xml</value>
-
<value>classpath*:config/mybatis/sqlMapper/setting/*.xml</value>
-
<value>classpath*:config/mybatis/sqlMapper/open/*.xml</value>
-
</list>
-
</property>
-
</bean>
-
-
<!-- 事务 -->
-
<bean id="transationManager" class="">
-
<property name="dataSource" ref="mvcDBSource" />
-
<qualifier value="mvcTran"/>
-
</bean>
-
<tx:annotation-driven transaction-manager="transationManager" />
-
-
<!-- ========== 数据源二:mvc DBSource ========== -->
-
<bean id="testDBSource" parent="baseDataSource">
-
<property name="jdbcUrl" value="${}"/>
-
<property name="user" value="${}"/>
-
<property name="password" value="${}" />
-
</bean>
-
-
<bean id="testSqlSession" parent="mvcSqlSession">
-
<property name="dataSource" ref="testDBSource" />
-
</bean>
-
-
<!-- 事务 -->
-
<bean id="testTransactionManager" class="">
-
<property name="dataSource" ref="testDBSource" />
-
<qualifier value="testTran"/>
-
</bean>
-
<tx:annotation-driven transaction-manager="testTransactionManager" />
-
-
</beans>
注意: 与 中 <context:component-scan> 的配置,两者需要扫描的注解不同,如果有冲突,就会导致 事务注解不生效。
dao 实现类
baseDao
-
public class BaseDaoSupportImpl extends SqlSessionDaoSupport implements IBaseDaoSupport {
-
-
/**
-
* mybatis-spring-1. 版本的 sqlSessionTemplate 注入有所改动,必须重写次方法
-
*/
-
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
-
super.setSqlSessionFactory(sqlSessionFactory);
-
}
-
-
protected <S> S getMapper(Class<S> clazz) {
-
return getSqlSession().getMapper(clazz);
-
}
-
-
}
-
/**
-
* 多数据源之 mvc dao
-
* @author Administrator
-
*
-
*/
-
@Repository("mvcDaoSupport")
-
public class MvcDaoSupportImpl extends BaseDaoSupportImpl implements IMvcDaoSupport {
-
-
@Resource(name="mvcSqlSession")
-
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
-
super.setSqlSessionFactory(sqlSessionFactory);
-
}
-
-
}
数据源二
-
/**
-
* 多数据源之 mvc dao
-
* @author Administrator
-
*
-
*/
-
@Repository("testDaoSupport")
-
public class TestDaoSupportImpl extends BaseDaoSupportImpl implements ITestDaoSupport {
-
-
@Resource(name="testSqlSession")
-
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
-
super.setSqlSessionFactory(sqlSessionFactory);
-
}
-
-
}
使用方法,所有service 继承自 baseService
-
public class BaseServiceSupportImpl implements IBaseServiceSupport {
-
protected Logger logger = (this.getClass());
-
-
@Resource(name = "mvcDaoSupport")
-
protected IBaseDaoSupport mvcDao;
-
-
@Resource(name = "testDaoSupport")
-
protected IBaseDaoSupport testDao;
-
-
}
测试事务类
-
/**
-
* 事务保存测试
-
*/
-
@Transactional("testTran")
-
public void saveMore() throws Exception{
-
-
(mapperName + "mvcInsert", null);
-
-
(mapperName + "testInsert", null);
-
-
List<String> testList = new ArrayList<String>();
-
-
((3));
-
-
}
注意,同一时间内,只能保证一个数据源的事务