基于 Spring + Atomikos + Mybatis的多数据源配置demo
1.spring配置文件
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 自动扫描 -->
<context:component-scan base-package="com.cn" />
<!-- 引入配置文件 -->
<!--<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
</bean>--> <bean id="dataSourceOracle" class="com.atomikos.jdbc.AtomikosDataSourceBean"
init-method="init" destroy-method="close">
<property name="uniqueResourceName" value="oracle_ds" />
<property name="xaDataSourceClassName"
value="oracle.jdbc.xa.client.OracleXADataSource" />
<property name="xaProperties">
<props>
<prop key="URL">jdbc:oracle:thin:@192.168.222.1:1521:orcl</prop>
<prop key="user">scott</prop>
<prop key="password">orcl</prop>
</props>
</property>
<property name="minPoolSize" value="10" />
<property name="maxPoolSize" value="100" />
<property name="borrowConnectionTimeout" value="30" />
<property name="maintenanceInterval" value="60" />
</bean> <bean id="dataSourceMysql" class="com.atomikos.jdbc.AtomikosDataSourceBean"
init-method="init" destroy-method="close">
<property name="uniqueResourceName" value="mysql_ds" />
<property name="xaDataSourceClassName"
value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" />
<property name="xaProperties">
<props>
<prop key="url">jdbc:mysql://localhost:3306/test</prop>
<prop key="user">root</prop>
<prop key="password"></prop>
</props>
</property>
<property name="minPoolSize" value="10" />
<property name="maxPoolSize" value="100" />
<property name="borrowConnectionTimeout" value="30" />
<property name="maintenanceInterval" value="60" />
</bean> <bean id="sqlSessionFactoryOracle" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSourceOracle" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations">
<array>
<value>classpath:com/cn/dao/dept/DeptDaoMapper.xml</value>
</array>
</property>
</bean>
<bean id="sqlSessionFactoryMysql" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSourceMysql" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations">
<array>
<value>classpath:com/cn/dao/brand/BrandDaoMapper.xml</value>
</array>
</property>
</bean> <!--去dao层扫描对应的接口,接口上边要添加MapperScan注解-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.cn.dao.dept" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryOracle"></property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.cn.dao.brand" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryMysql"></property>
</bean> <!-- atomikos事务管理器 -->
<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"
init-method="init" destroy-method="close">
<description>UserTransactionManager</description>
<property name="forceShutdown">
<value>true</value>
</property>
</bean> <bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
<property name="transactionTimeout" value="300" />
</bean> <!-- spring 事务管理器 -->
<bean id="transactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManager" ref="atomikosTransactionManager"/>
<property name="userTransaction" ref="atomikosUserTransaction" />
<property name="allowCustomIsolationLevels" value="true"/>
</bean> <!-- 使用annotation定义事务,对于要加入事物的类,只需对该类加 @Transactional -->
<tx:annotation-driven transaction-manager="transactionManager" /> <!--<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<!– 初始化连接大小 –>
<property name="initialSize" value="${initialSize}"></property>
<!– 连接池最大数量 –>
<property name="maxActive" value="${maxActive}"></property>
<!– 连接池最大空闲 –>
<property name="maxIdle" value="${maxIdle}"></property>
<!– 连接池最小空闲 –>
<property name="minIdle" value="${minIdle}"></property>
<!– 获取连接最大等待时间 –>
<property name="maxWait" value="${maxWait}"></property>
</bean>--> <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<!--<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!–<property name="configLocation" value="classpath:mybatis-config.xml"/>–>
<!– 自动扫描mapping.xml文件 –>
<property name="mapperLocations" value="classpath:com/cn/**/*.xml"></property>
</bean>--> <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<!--<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.cn.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>--> <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<!--<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
-->
</beans>
2.项目结构


3.mybatis映射文件

基于 Spring + Atomikos + Mybatis的多数据源配置demo的更多相关文章
- 基于 Spring + Atomikos + Mybatis的多数据源配置(含有BaseDao,BaseService)
1.spring配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=" ...
- spring boot +mybatis+druid 多数据源配置
因为我的工程需要在两个数据库中操作数据,所以要配置两个数据库,我这里没有数据源没有什么主从之分,只是配合多数据源必须要指定一个主数据源,所以我就把 操作相对要对的那个数据库设置为主数据(dataBas ...
- spring,mybatis,多数据源配置
spring.xml配置 <!-- 对数据源进行事务管理 --> <bean id="transactionManager" class="org.sp ...
- Spring Boot 2.x基础教程:MyBatis的多数据源配置
前两天,我们已经介绍了关于JdbcTemplate的多数据源配置以及Spring Data JPA的多数据源配置,接下来具体说说使用MyBatis时候的多数据源场景该如何配置. 添加多数据源的配置 先 ...
- Spring Boot 2.x 多数据源配置之 MyBatis 篇
场景假设:现有电商业务,商品和库存分别放在不同的库 配置数据库连接 app: datasource: first: driver-class-name: com.mysql.cj.jdbc.Drive ...
- Spring Boot + Mybatis 实现动态数据源
动态数据源 在很多具体应用场景的时候,我们需要用到动态数据源的情况,比如多租户的场景,系统登录时需要根据用户信息切换到用户对应的数据库.又比如业务A要访问A数据库,业务B要访问B数据库等,都可以使用动 ...
- 基于Spring框架的简单多数据源切换解决办法
基于Spring框架的简单多数据源切换解决办法 Spring框架JDBC包提供了一个抽象类AbstractRoutingDataSource提供了动态切换数据库的基础方法.我们仅仅需要实现一个简单的数 ...
- 13、Spring Boot 2.x 多数据源配置
1.13 Spring Boot 2.x 多数据源配置 完整源码: Spring-Boot-Demos
- Spring Boot 2.x基础教程:Spring Data JPA的多数据源配置
上一篇我们介绍了在使用JdbcTemplate来做数据访问时候的多数据源配置实现.接下来我们继续学习如何在使用Spring Data JPA的时候,完成多数据源的配置和使用. 添加多数据源的配置 先在 ...
随机推荐
- 学到了武沛齐讲的Day12-5
字节为8个位为单位 而计算机存储是位为单位储存.(有点意思) 开始老美用的是ascii码(1个字节), utf8: 3字节 gbk : 2字节 目前的用于实用的 Unicode 版本对 ...
- 32 | 为什么还有kill不掉的语句?
在 MySQL 中有两个 kill 命令:一个是 kill query + 线程 id,表示终止这个线程中正在执行的语句:一个是 kill connection + 线程 id,这里 connecti ...
- 064_将 Linux 系统中 UID 大于等于 1000 的普通用户都删除
#!/bin/bash#先用 awk 提取所有 uid 大于等于 1000 的普通用户名称#再使用 for 循环逐个将每个用户删除即可 user=$(awk -F: '$3>=1000{prin ...
- 块状链表 codevs 2333弹飞绵羊
块状链表,分块处理,先预处理每一个点跳到下一个块 跳到哪,步数.然后修改的时候,修该那一个块即可 #include<cstdio>#include<cmath>int a[20 ...
- P1899 魔法物品
题目描述 //又是一个好(nan)题好(nan)题 //首先,普通物品一开始就卖掉就可以,因为它不会增值 //至于魔法物品 //如果一个魔法物品使用了卷轴后的价值减去买卷轴的钱还不如鉴定前的价值高,那 ...
- Windows10下安装Ubuntu18.04LTS详细教程
这篇文章分享自己在Windows10系统下安装VMware虚拟机,然后在VMware中安装Ubuntu 18.04 LTS的详细过程.之所以选择在虚拟机中安装Ubuntu,主要是可以不影响自己电脑的正 ...
- 【原】Python基础-序列
1 序列 在Python中,最基本的数据结构是序列,序列中每个元素被分配一个编号,也称为索引.第一个索引为0,第二个则是1,以此类推.序列中最后一个元素被标为-1,倒数第二个元素被标为-2,以此类推. ...
- Leetcode题目437:路径总和III(递归-简单)
题目描述: 给定一个二叉树,它的每个结点都存放着一个整数值. 找出路径和等于给定数值的路径总数. 路径不需要从根节点开始,也不需要在叶子节点结束,但是路径方向必须是向下的(只能从父节点到子节点). 二 ...
- 2018-2019-2 20165222《网络对抗技术》Exp9 Web安全基础
1.实践过程记录 1.字符串型注入. 2.整数型注入 3.注入语句查看其他内容 4.xss是一种漏洞,这种漏洞允许用户输入脚本并且浏览器提交的时候不加编码.这种东西是最为流行并且有害的web应用的问题 ...
- 控制 Python 工具箱中的许可行为
def isLicensed(self): """Allow the tool to execute, only if the ArcGIS 3D Analyst 扩展模 ...