基于 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}" />
        &lt;!&ndash; 初始化连接大小 &ndash;&gt;
        <property name="initialSize" value="${initialSize}"></property>
        &lt;!&ndash; 连接池最大数量 &ndash;&gt;
        <property name="maxActive" value="${maxActive}"></property>
        &lt;!&ndash; 连接池最大空闲 &ndash;&gt;
        <property name="maxIdle" value="${maxIdle}"></property>
        &lt;!&ndash; 连接池最小空闲 &ndash;&gt;
        <property name="minIdle" value="${minIdle}"></property>
        &lt;!&ndash; 获取连接最大等待时间 &ndash;&gt;
        <property name="maxWait" value="${maxWait}"></property>
    </bean>-->

    <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
    <!--<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        &lt;!&ndash;<property name="configLocation"  value="classpath:mybatis-config.xml"/>&ndash;&gt;
        &lt;!&ndash; 自动扫描mapping.xml文件 &ndash;&gt;
        <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映射文件

时间: 2025-01-05 11:50:20

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

spring,mybatis,多数据源配置

spring.xml配置 <!-- 对数据源进行事务管理 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="multipleDataSource"></pr

基于spring的aop实现多数据源动态切换

https://lanjingling.github.io/2016/02/15/spring-aop-dynamicdatasource/ 基于spring的aop实现多数据源动态切换 发表于 2016-02-15   |   分类于 spring  | 一.多数据源动态切换原理 项目中我们经常会遇到多数据源的问题,尤其是数据同步或定时任务等项目更是如此:又例如:读写分离数据库配置的系统. 1.多数据源设置: 1)静态数据源切换:一般情况下,我们可以配置多个数据源,然后为每个数据源写一套对应的

项目构建之maven篇:8.maven发布web工程及基于spring mvc,jetty实现的用户管理demo

web工程目录结构 pom/pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&qu

Mybatis+Druid多数据源配置

在日常开发中我们可能会用到多数据源开发,什么是多数据源? 简单来讲的话,就是一个项目连接多个数据库.当然只是可能会用到,我暂时没见过应用场景,但是还是了解学习一下 此项目可以基于上一个简单集成项目进行简单的修改,就能实现多数据源了. application.yml配置 我们在上一个项目的基础上进行修改,实现多数据源配置 spring: datasource: type: com.alibaba.druid.pool.DruidDataSource url: jdbc:mysql://localh

基于Spring+SpringMVC+Mybatis的Web系统搭建

主要的后端架构:Spring+SpringMVC+Mybatis+Shiro+Maven  IDE:IntelliJ IDEA 15.0.2 jdk:1.8.0_66 系统完整源码 https://github.com/Wellat/Factor 系统目录结构 跑起来效果 搭建步骤 1.用Idea创建maven项目 2.配置pom.xml文件,添加依赖 1 <?xml version="1.0" encoding="UTF-8"?> 2 <proj

Spring Boot之JdbcTemplate多数据源配置与使用

之前在介绍使用JdbcTemplate和Spring-data-jpa时,都使用了单数据源.在单数据源的情况下,Spring Boot的配置非常简单,只需要在application.properties文件中配置连接参数即可.但是往往随着业务量发展,我们通常会进行数据库拆分或是引入其他数据库,从而我们需要配置多个数据源,下面基于之前的JdbcTemplate和Spring-data-jpa例子分别介绍两种多数据源的配置方式. 多数据源配置 创建一个Spring配置类,定义两个DataSource

Spring配置文件中的C3P0数据源配置和事务配置(采用mysql)

直接上代码 <?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:aop="http://www.springframework.org/s

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

1.spring配置文件 classpath:/system.properties classpath:/jdbc.properties classpath:/system.properties UTF-8 mysql xph datasource ${mysql_url_xph} ${mysql_user_xph} ${mysql_password_xph} mysql qs datasource ${mysql_url_qs} ${mysql_user_qs} ${mysql_passwor

基于maven的SpringMVC,Spring,MyBatis的全注解配置

首先创建maven项目 1.maven添加各种依赖包 <!-- 数据库连接池 --> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> <!-- Mybatis与Spring的包 --> <dependency>