mybatis + springMVC 多数据源,及事务配置

时间:2024-10-23 08:26:50

之前写过一种多数据源配置的方式,但是那种方式对代码入侵性比较大,详情请查阅

mybatis + spring 多数据源跨库查询

最近在做 springMVC 搭建时,更改了新的实现,并且提供多数据源中单个数据源事务的支持


下面直接放完整代码:

配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http:///2001/XMLSchema-instance"
  3. xmlns="/xml/ns/javaee"
  4. xsi:schemaLocation="/xml/ns/javaee /xml/ns/javaee/web-app_3_1.xsd"
  5. id="WebApp_ID" version="3.1">
  6. <display-name>testweb</display-name>
  7. <welcome-file-list>
  8. <welcome-file></welcome-file>
  9. <welcome-file></welcome-file>
  10. <welcome-file></welcome-file>
  11. <welcome-file></welcome-file>
  12. <welcome-file></welcome-file>
  13. <welcome-file></welcome-file>
  14. </welcome-file-list>
  15. <!-- webAppRootKey -->
  16. <context-param>
  17. <param-name>webAppRootKey</param-name>
  18. <param-value></param-value>
  19. </context-param>
  20. <!-- ========== Log4j 监听器 ========== -->
  21. <listener>
  22. <listener-class>.Log4jConfigListener</listener-class>
  23. </listener>
  24. <context-param>
  25. <param-name>log4jConfigLocation</param-name>
  26. <param-value>classpath:config/log4j/</param-value>
  27. </context-param>
  28. <!-- spring mvc -->
  29. <servlet>
  30. <servlet-name>springmvc</servlet-name>
  31. <servlet-class></servlet-class>
  32. <init-param>
  33. <param-name>contextConfigLocation</param-name>
  34. <param-value>classpath:config/spring/</param-value>
  35. </init-param>
  36. <load-on-startup>1</load-on-startup>
  37. </servlet>
  38. <servlet-mapping>
  39. <servlet-name>springmvc</servlet-name>
  40. <url-pattern>/</url-pattern>
  41. </servlet-mapping>
  42. <!-- ========== spring 配置文件 ========== -->
  43. <context-param>
  44. <param-name>contextConfigLocation</param-name>
  45. <param-value>
  46. classpath:config/spring/
  47. </param-value>
  48. </context-param>
  49. <listener>
  50. <listener-class></listener-class>
  51. </listener>
  52. <!-- ========== session 超时 ========== -->
  53. <session-config>
  54. <session-timeout>60</session-timeout>
  55. </session-config>
  56. <error-page>
  57. <error-code>404</error-code>
  58. <location>/page/common/error/</location>
  59. </error-page>
  60. <error-page>
  61. <error-code>500</error-code>
  62. <location>/page/common/error/</location>
  63. </error-page>
  64. </web-app>

配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="/schema/beans"
  3. xmlns:xsi="http:///2001/XMLSchema-instance" xmlns:context="/schema/context"
  4. xmlns:tx="/schema/tx" xmlns:mvc="/schema/mvc"
  5. xsi:schemaLocation="/schema/beans
  6. /schema/beans/
  7. /schema/context
  8. /schema/context/
  9. /schema/tx
  10. /schema/tx/
  11. /schema/mvc
  12. /schema/mvc/">
  13. <!-- 注册HandlerMapper、HandlerAdapter两个映射类 -->
  14. <mvc:annotation-driven />
  15. <context:component-scan base-package=".*"
  16. use-default-filters="false">
  17. <context:include-filter type="annotation"
  18. expression="" />
  19. </context:component-scan>
  20. <!-- 访问静态资源 -->
  21. <mvc:default-servlet-handler />
  22. <!-- 视图解析器 -->
  23. <bean
  24. class="">
  25. <property name="prefix" value="/page/"></property>
  26. <property name="suffix" value=".jsp"></property>
  27. </bean>
  28. </beans>

配置


  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="/schema/beans"
  3. xmlns:xsi="http:///2001/XMLSchema-instance" xmlns:aop="/schema/aop"
  4. xmlns:context="/schema/context" xmlns:tx="/schema/tx"
  5. xsi:schemaLocation="/schema/beans
  6. /schema/beans/spring-beans-4.
  7. /schema/aop /schema/aop/spring-aop-4.
  8. /schema/context /schema/context/spring-context-4.
  9. /schema/tx /schema/tx/spring-tx-4.">
  10. <!-- 读取数据源配置文件 -->
  11. <bean id="dataSoucresConfig"
  12. class="">
  13. <property name="locations"
  14. value="classpath:config/properties/" />
  15. </bean>
  16. <!-- 使用annotation自动注册bean,并保证@Required,@Autowired的属性被注入 -->
  17. <context:annotation-config />
  18. <context:component-scan base-package=".*">
  19. <context:exclude-filter type="annotation"
  20. expression="" />
  21. </context:component-scan>
  22. <!-- 自动AOP切面 -->
  23. <aop:aspectj-autoproxy />
  24. <bean id="baseDataSource" class=".v2."
  25. destroy-method="close">
  26. <property name="driverClass" value="${}"/>
  27. <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
  28. <property name="acquireIncrement" value="${}"/>
  29. <!--初始化时获取连接个数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
  30. <property name="initialPoolSize" value="${}"/>
  31. <!--最大空闲时间內未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
  32. <property name="maxIdleTime" value="${}"/>
  33. <!--连接池中保留的最大连接数。Default: 15 -->
  34. <property name="maxPoolSize" value="${}"/>
  35. <!--连接池中保留的最小连接数。-->
  36. <property name="minPoolSize" value="${}"/>
  37. <!--两次连接中间隔时间,单位毫秒。Default: 1000 -->
  38. <property name="acquireRetryDelay" value="${}"/>
  39. <!--定义在从数据库获取新连接失败后重复尝试的次数。Default: 30 -->
  40. <property name="acquireRetryAttempts" value="${}"/>
  41. <!-- 为 true 时 pool向数据库请求连接失败后标记整个pool为block并close -->
  42. <property name="breakAfterAcquireFailure" value="${}"/>
  43. </bean>
  44. <!-- ========== 数据源一:mvc DBSource ========== -->
  45. <bean id="mvcDBSource" parent="baseDataSource">
  46. <property name="jdbcUrl" value="${}"/>
  47. <property name="user" value="${}"/>
  48. <property name="password" value="${}" />
  49. </bean>
  50. <bean id="mvcSqlSession" class="">
  51. <property name="dataSource" ref="mvcDBSource" />
  52. <property name="configLocation" value="classpath:config/mybatis/" />
  53. <property name="mapperLocations">
  54. <list>
  55. <value>classpath*:config/mybatis/sqlMapper/common/*.xml</value>
  56. <value>classpath*:config/mybatis/sqlMapper/webview/*.xml</value>
  57. <value>classpath*:config/mybatis/sqlMapper/setting/*.xml</value>
  58. <value>classpath*:config/mybatis/sqlMapper/open/*.xml</value>
  59. </list>
  60. </property>
  61. </bean>
  62. <!-- 事务 -->
  63. <bean id="transationManager" class="">
  64. <property name="dataSource" ref="mvcDBSource" />
  65. <qualifier value="mvcTran"/>
  66. </bean>
  67. <tx:annotation-driven transaction-manager="transationManager" />
  68. <!-- ========== 数据源二:mvc DBSource ========== -->
  69. <bean id="testDBSource" parent="baseDataSource">
  70. <property name="jdbcUrl" value="${}"/>
  71. <property name="user" value="${}"/>
  72. <property name="password" value="${}" />
  73. </bean>
  74. <bean id="testSqlSession" parent="mvcSqlSession">
  75. <property name="dataSource" ref="testDBSource" />
  76. </bean>
  77. <!-- 事务 -->
  78. <bean id="testTransactionManager" class="">
  79. <property name="dataSource" ref="testDBSource" />
  80. <qualifier value="testTran"/>
  81. </bean>
  82. <tx:annotation-driven transaction-manager="testTransactionManager" />
  83. </beans>


注意: 与  中 <context:component-scan> 的配置,两者需要扫描的注解不同,如果有冲突,就会导致 事务注解不生效。


dao 实现类

baseDao

  1. public class BaseDaoSupportImpl extends SqlSessionDaoSupport implements IBaseDaoSupport {
  2. /**
  3. * mybatis-spring-1. 版本的 sqlSessionTemplate 注入有所改动,必须重写次方法
  4. */
  5. public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
  6. super.setSqlSessionFactory(sqlSessionFactory);
  7. }
  8. protected <S> S getMapper(Class<S> clazz) {
  9. return getSqlSession().getMapper(clazz);
  10. }
  11. }


数据源一

  1. /**
  2. * 多数据源之 mvc dao
  3. * @author Administrator
  4. *
  5. */
  6. @Repository("mvcDaoSupport")
  7. public class MvcDaoSupportImpl extends BaseDaoSupportImpl implements IMvcDaoSupport {
  8. @Resource(name="mvcSqlSession")
  9. public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
  10. super.setSqlSessionFactory(sqlSessionFactory);
  11. }
  12. }

数据源二

  1. /**
  2. * 多数据源之 mvc dao
  3. * @author Administrator
  4. *
  5. */
  6. @Repository("testDaoSupport")
  7. public class TestDaoSupportImpl extends BaseDaoSupportImpl implements ITestDaoSupport {
  8. @Resource(name="testSqlSession")
  9. public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
  10. super.setSqlSessionFactory(sqlSessionFactory);
  11. }
  12. }

使用方法,所有service 继承自 baseService

  1. public class BaseServiceSupportImpl implements IBaseServiceSupport {
  2. protected Logger logger = (this.getClass());
  3. @Resource(name = "mvcDaoSupport")
  4. protected IBaseDaoSupport mvcDao;
  5. @Resource(name = "testDaoSupport")
  6. protected IBaseDaoSupport testDao;
  7. }

测试事务类

  1. /**
  2. * 事务保存测试
  3. */
  4. @Transactional("testTran")
  5. public void saveMore() throws Exception{
  6. (mapperName + "mvcInsert", null);
  7. (mapperName + "testInsert", null);
  8. List<String> testList = new ArrayList<String>();
  9. ((3));
  10. }

注意,同一时间内,只能保证一个数据源的事务