spring + mybatis 配置多数据源

1.这里说到的是非主从关系的多数据源,就是两个完全独立的数据库


2.下面说一下配置过程,主要参考资料:

http://www.cnblogs.com/digdeep/p/4512368.html


首先是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:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
	xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
		http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd"
	default-lazy-init="true">

	<description>Spring Configuration</description>
	
    <!-- 加载配置属性文件 -->
	<context:property-placeholder ignore-unresolvable="true" location="classpath:application.properties" />
	
	<context:component-scan base-package="com.cf.rs,com.cf.bus">
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>
	
	<!-- 第一个数据源  start -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
        <property name="dataSource" ref="dataSource"/>
        <property name="typeAliasesPackage" value="com.cf.rs"/>
        <property name="typeAliasesSuperType" value="com.cf.rs.core.persistence.BaseEntity"/>
        <property name="mapperLocations" value="classpath:/mappings/**/*.xml"/>
		<property name="configLocation" value="classpath:/mybatis-config.xml"></property>
    </bean>
    
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
        <property name="basePackage" value="com.cf.rs"/>
        <property name="annotationClass" value="com.cf.rs.core.persistence.annotation.MyBatisDao"/>
    </bean>
    
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
	
	<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
	
	<bean id="dataSource" parent="baseDataSource">
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>


	<!-- 第二个数据源  start-->
	<bean id="sqlSessionFactory2" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource2"/>
		<property name="typeAliasesPackage" value="com.cf.bus"/>
		<property name="typeAliasesSuperType" value="com.cf.bus.core.persistence.BaseEntity"/>
		<property name="mapperLocations" value="classpath:/mappings2/**/*.xml"/>
		<property name="configLocation" value="classpath:/mybatis-config2.xml"></property>
	</bean>

	<bean id="mapperScannerConfigurer2" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory2" />
		<property name="basePackage" value="com.cf.bus"/>
		<property name="annotationClass" value="com.cf.bus.core.persistence.annotation.MyBatisDao"/>
	</bean>

	<bean id="transactionManager2" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource2" />
	</bean>

	<tx:annotation-driven transaction-manager="transactionManager2" proxy-target-class="true"/>
	<!-- MyBatis end -->

	<bean id="dataSource2" parent="baseDataSource">
		<property name="url" value="${jdbc.url2}" />
		<property name="username" value="${jdbc.username2}" />
		<property name="password" value="${jdbc.password2}" />
	</bean>
	<!-- 2  end-->

	<!-- 基础数据源配置  start-->
	<bean id="baseDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
		<property name="driverClassName" value="${jdbc.driver}" />

		<property name="initialSize" value="${jdbc.pool.init}" />
		<property name="minIdle" value="${jdbc.pool.minIdle}" />
		<property name="maxActive" value="${jdbc.pool.maxActive}" />

		<property name="maxWait" value="60000" />

		<property name="timeBetweenEvictionRunsMillis" value="60000" />

		<property name="minEvictableIdleTimeMillis" value="300000" />

		<property name="filters" value="stat" />
	</bean>

	<!-- 基础数据源配置  end-->

	
</beans>

注意两个数据源配置种几乎所有属性节点内容都不同,要仔细看,mybatis的配置文件和映射文件还有dao接口都是各自数据源独享的,

其中有两个MyBatisDao的自定义注解,不同数据源下面的dao接口都要添加各自对应的注解,两个注解内容一样,内容如下:

package com.cf.bus.core.persistence.annotation;

import org.springframework.stereotype.Component;

import java.lang.annotation.*;

/**
 * 标识MyBatis的DAO,方便{@link org.mybatis.spring.mapper.MapperScannerConfigurer}的扫描。 
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Component
public @interface MyBatisDao {
	
	/**
	 * The value may indicate a suggestion for a logical component name,
	 * to be turned into a Spring bean in case of an autodetected component.
	 * @return the suggested component name, if any
	 */
	String value() default "";

}

dao接口


package com.cf.bus.sc.dao;


import com.cf.bus.sc.entity.SC;
import com.cf.bus.core.persistence.CrudDao;
import com.cf.bus.core.persistence.annotation.MyBatisDao;


/**
 * DAO接口
 */
@MyBatisDao     // 这里添加自定义注解,这样会被相应的MyBatis扫描
public interface SCDao extends CrudDao<SC> {

}



其他的跟正常使用mybatis一样,以上就是spring + mybatis 配置多个数据源,有问题欢迎一起交流。





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringMyBatis是两个非常流行的Java开发框架,可以很好地支持双数据源事务。 在Spring配置多数据源,我们需要使用`@Configuration`注解将Java类标记为配置类,并通过`@Bean`注解来声明并注册多个数据源。对于每个数据源,我们需要配置对应的数据源对象、事务管理器和MyBatis的SqlSessionFactoryBean。然后,使用`@Primary`注解标记一个数据源为主数据源,它将作为默认的数据源。 接下来,在事务的配置中,我们需要使用`@EnableTransactionManagement`注解开启Spring的事务管理功能,并使用`@Transactional`注解来标记需要进行事务管理的方法。在方法内部,我们可以使用`TransactionTemplate`或`PlatformTransactionManager`进行事务的控制。 在使用MyBatis时,我们可以通过`@MapperScan`注解来自动扫描并注册MyBatis的Mapper接口。在进行事务配置时,可以使用`@Transactional`注解来标记需要进行事务管理的方法,从而确保在执行数据库操作时,能够正确地开启、提交或回滚事务。 在使用双数据源时,我们需要在方法或类的上方使用`@Transactional(value = "transactionManager")`注解来指定需要使用哪个数据源的事务管理器。这样,在执行相关操作时,Spring就会根据指定的数据源来管理事务,保证数据的一致性。 总结一下,SpringMyBatis提供了完善的支持来实现双数据源事务。我们可以通过合理的配置和注解来实现多个数据源的管理,并使用事务注解来控制事务的开启、提交和回滚。这样,就可以在一个应用程序中同时访问和操作多个数据源,保证数据的一致性和完整性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值