1.spring配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xmlns:mvc="http://www.springframework.org/schema/mvc"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context-3.1.xsd
  11. http://www.springframework.org/schema/tx
  12. http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
  13. http://www.springframework.org/schema/mvc
  14. http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
  15. <!-- 自动扫描 -->
  16. <context:component-scan base-package="com.cn" />
  17. <!-- 引入配置文件 -->
  18. <!--<bean id="propertyConfigurer"
  19. class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  20. <property name="location" value="classpath:jdbc.properties" />
  21. </bean>-->
  22.  
  23. <bean id="dataSourceOracle" class="com.atomikos.jdbc.AtomikosDataSourceBean"
  24. init-method="init" destroy-method="close">
  25. <property name="uniqueResourceName" value="oracle_ds" />
  26. <property name="xaDataSourceClassName"
  27. value="oracle.jdbc.xa.client.OracleXADataSource" />
  28. <property name="xaProperties">
  29. <props>
  30. <prop key="URL">jdbc:oracle:thin:@192.168.222.1:1521:orcl</prop>
  31. <prop key="user">scott</prop>
  32. <prop key="password">orcl</prop>
  33. </props>
  34. </property>
  35. <property name="minPoolSize" value="10" />
  36. <property name="maxPoolSize" value="100" />
  37. <property name="borrowConnectionTimeout" value="30" />
  38. <property name="maintenanceInterval" value="60" />
  39. </bean>
  40.  
  41. <bean id="dataSourceMysql" class="com.atomikos.jdbc.AtomikosDataSourceBean"
  42. init-method="init" destroy-method="close">
  43. <property name="uniqueResourceName" value="mysql_ds" />
  44. <property name="xaDataSourceClassName"
  45. value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" />
  46. <property name="xaProperties">
  47. <props>
  48. <prop key="url">jdbc:mysql://localhost:3306/test</prop>
  49. <prop key="user">root</prop>
  50. <prop key="password"></prop>
  51. </props>
  52. </property>
  53. <property name="minPoolSize" value="10" />
  54. <property name="maxPoolSize" value="100" />
  55. <property name="borrowConnectionTimeout" value="30" />
  56. <property name="maintenanceInterval" value="60" />
  57. </bean>
  58.  
  59. <bean id="sqlSessionFactoryOracle" class="org.mybatis.spring.SqlSessionFactoryBean">
  60. <property name="dataSource" ref="dataSourceOracle" />
  61. <!-- 自动扫描mapping.xml文件 -->
  62. <property name="mapperLocations">
  63. <array>
  64. <value>classpath:com/cn/dao/dept/DeptDaoMapper.xml</value>
  65. </array>
  66. </property>
  67. </bean>
  68. <bean id="sqlSessionFactoryMysql" class="org.mybatis.spring.SqlSessionFactoryBean">
  69. <property name="dataSource" ref="dataSourceMysql" />
  70. <!-- 自动扫描mapping.xml文件 -->
  71. <property name="mapperLocations">
  72. <array>
  73. <value>classpath:com/cn/dao/brand/BrandDaoMapper.xml</value>
  74. </array>
  75. </property>
  76. </bean>
  77.  
  78. <!--去dao层扫描对应的接口,接口上边要添加MapperScan注解-->
  79. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  80. <property name="basePackage" value="com.cn.dao.dept" />
  81. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryOracle"></property>
  82. </bean>
  83. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  84. <property name="basePackage" value="com.cn.dao.brand" />
  85. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryMysql"></property>
  86. </bean>
  87.  
  88. <!-- atomikos事务管理器 -->
  89. <bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"
  90. init-method="init" destroy-method="close">
  91. <description>UserTransactionManager</description>
  92. <property name="forceShutdown">
  93. <value>true</value>
  94. </property>
  95. </bean>
  96.  
  97. <bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
  98. <property name="transactionTimeout" value="300" />
  99. </bean>
  100.  
  101. <!-- spring 事务管理器 -->
  102. <bean id="transactionManager"
  103. class="org.springframework.transaction.jta.JtaTransactionManager">
  104. <property name="transactionManager" ref="atomikosTransactionManager"/>
  105. <property name="userTransaction" ref="atomikosUserTransaction" />
  106. <property name="allowCustomIsolationLevels" value="true"/>
  107. </bean>
  108.  
  109. <!-- 使用annotation定义事务,对于要加入事物的类,只需对该类加 @Transactional -->
  110. <tx:annotation-driven transaction-manager="transactionManager" />
  111.  
  112. <!--<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
  113. destroy-method="close">
  114. <property name="driverClassName" value="${driverClassName}" />
  115. <property name="url" value="${url}" />
  116. <property name="username" value="${username}" />
  117. <property name="password" value="${password}" />
  118. &lt;!&ndash; 初始化连接大小 &ndash;&gt;
  119. <property name="initialSize" value="${initialSize}"></property>
  120. &lt;!&ndash; 连接池最大数量 &ndash;&gt;
  121. <property name="maxActive" value="${maxActive}"></property>
  122. &lt;!&ndash; 连接池最大空闲 &ndash;&gt;
  123. <property name="maxIdle" value="${maxIdle}"></property>
  124. &lt;!&ndash; 连接池最小空闲 &ndash;&gt;
  125. <property name="minIdle" value="${minIdle}"></property>
  126. &lt;!&ndash; 获取连接最大等待时间 &ndash;&gt;
  127. <property name="maxWait" value="${maxWait}"></property>
  128. </bean>-->
  129.  
  130. <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
  131. <!--<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  132. <property name="dataSource" ref="dataSource" />
  133. &lt;!&ndash;<property name="configLocation" value="classpath:mybatis-config.xml"/>&ndash;&gt;
  134. &lt;!&ndash; 自动扫描mapping.xml文件 &ndash;&gt;
  135. <property name="mapperLocations" value="classpath:com/cn/**/*.xml"></property>
  136. </bean>-->
  137.  
  138. <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
  139. <!--<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  140. <property name="basePackage" value="com.cn.dao" />
  141. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
  142. </bean>-->
  143.  
  144. <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
  145. <!--<bean id="transactionManager"
  146. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  147. <property name="dataSource" ref="dataSource" />
  148. </bean>
  149. -->
  150. </beans>

2.项目结构

3.mybatis映射文件

基于 Spring + Atomikos + Mybatis的多数据源配置demo的更多相关文章

  1. 基于 Spring + Atomikos + Mybatis的多数据源配置(含有BaseDao,BaseService)

    1.spring配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=" ...

  2. spring boot +mybatis+druid 多数据源配置

    因为我的工程需要在两个数据库中操作数据,所以要配置两个数据库,我这里没有数据源没有什么主从之分,只是配合多数据源必须要指定一个主数据源,所以我就把 操作相对要对的那个数据库设置为主数据(dataBas ...

  3. spring,mybatis,多数据源配置

    spring.xml配置 <!-- 对数据源进行事务管理 --> <bean id="transactionManager" class="org.sp ...

  4. Spring Boot 2.x基础教程:MyBatis的多数据源配置

    前两天,我们已经介绍了关于JdbcTemplate的多数据源配置以及Spring Data JPA的多数据源配置,接下来具体说说使用MyBatis时候的多数据源场景该如何配置. 添加多数据源的配置 先 ...

  5. Spring Boot 2.x 多数据源配置之 MyBatis 篇

    场景假设:现有电商业务,商品和库存分别放在不同的库 配置数据库连接 app: datasource: first: driver-class-name: com.mysql.cj.jdbc.Drive ...

  6. Spring Boot + Mybatis 实现动态数据源

    动态数据源 在很多具体应用场景的时候,我们需要用到动态数据源的情况,比如多租户的场景,系统登录时需要根据用户信息切换到用户对应的数据库.又比如业务A要访问A数据库,业务B要访问B数据库等,都可以使用动 ...

  7. 基于Spring框架的简单多数据源切换解决办法

    基于Spring框架的简单多数据源切换解决办法 Spring框架JDBC包提供了一个抽象类AbstractRoutingDataSource提供了动态切换数据库的基础方法.我们仅仅需要实现一个简单的数 ...

  8. 13、Spring Boot 2.x 多数据源配置

    1.13 Spring Boot 2.x 多数据源配置 完整源码: Spring-Boot-Demos

  9. Spring Boot 2.x基础教程:Spring Data JPA的多数据源配置

    上一篇我们介绍了在使用JdbcTemplate来做数据访问时候的多数据源配置实现.接下来我们继续学习如何在使用Spring Data JPA的时候,完成多数据源的配置和使用. 添加多数据源的配置 先在 ...

随机推荐

  1. UOJ 449 【集训队作业2018】喂鸽子 【生成函数,min-max容斥】

    这是第100篇博客,所以肯定是要水过去的. 首先看到这种形式的东西首先min-max容斥一波,设\(f_{c,s}\)表示在\(c\)只咕咕中,经过\(s\)秒之后并没有喂饱任何一只的概率. \[ \ ...

  2. 使用MockMVC与Junit进行单体测试

    1.pom.xml追加 junit spring-test 2.测试共通类 @ContextConfiguration(locations = { "classpath:springfram ...

  3. P1065 作业调度方案——小模怡情,大模伤身

    P1065 作业调度方案 一个有点费手的“小”%%拟: 题都差点没读明白……: 每个机器所能完成的工序是不一样的: 每个物品完成工序的机器是指定的: 按照题面说的按时间轴推下去就行了: 没有时间上界有 ...

  4. yy

    sudo rm -rf /var/cache/apt/archives/python-catkin-pkg-modules_0.4.12-1_all.deb sudo rm -rf /var/cach ...

  5. (转)awk 详解

    出处:https://blog.51cto.com/yijiu/1358416 awk详解 awk是一款非常牛逼的报告生成工具,能够将文本格式化成显示为比较直观的结果 废话不多说,直接上例子 awk的 ...

  6. [Shell]Powershell反弹shell

    原作者:Cream 文章出处: 贝塔安全实验室 0x01 Powershell反弹shell Windows PowerShell 是一种命令行外壳程序和脚本环境,使命令行用户和脚本编写者可以利用 . ...

  7. JAVA基础知识|Serializable

    一.序列化和反序列化 序列化:把对象转换为字节序列的过程称为对象的序列化. 反序列化:把字节序列恢复为对象的过程称为对象的反序列化. 将内存中对象的信息保存下来,可以有很多种方式实现这一功能.其中ja ...

  8. Java知识体系思维导图

    Java知识体系,为方便预览,将思维导图上传至印象笔记,博客园直接上传图片受限于图片大小,暂时接触这么多,待以后丰富 https://app.yinxiang.com/shard/s24/nl/272 ...

  9. CodeForces - 1183E Subsequences (easy version) (字符串bfs)

    The only difference between the easy and the hard versions is constraints. A subsequence is a string ...

  10. SpringBoot-文件在线预览解决方案-基于OpenOffice及jacob

    项目中有一个需求:实现文件(主要是Office文件)的在线预览,根据前端需求,Office文件需要转换成pdf或者html方可在浏览器中打开预览,那么后端需要将文件转为pdf/格式返回地址给前端.目前 ...