spring mybatis多数据源实践

参考这篇文章:http://mybatis-user.963551.n3.nabble.com/How-to-connect-to-several-databases-using-MyBatis-Spring-Integration-td4026491.html

具体实现步骤如下:

1:定义annotation,eclipse的右键菜单new里边能直接创建annotation

package cn.yunfox.healthplus.repositories;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

@Component
@Repository
public @interface HealthPlusRepository {

}

package cn.yunfox.healthplus.repositories;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

@Component
@Repository
public @interface MobileMallRepository {

}

2:修改spring mybatis配置:

	<!-- mybatis starts -->
	<context:property-placeholder location="classpath:Mysql.properties" />
	<!-- HealthPlus -->
	<beans:bean id="dataSourceHealthPlus"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<beans:property name="driverClassName" value="${jdbc.driverClassName}" />
		<beans:property name="url" value="${jdbc.urlhealthplus}" />
	</beans:bean>
	
	<!-- define the SqlSessionFactory -->
	<beans:bean id="sqlSessionFactoryHealthPlus" class="org.mybatis.spring.SqlSessionFactoryBean">
		<beans:property name="dataSource" ref="dataSourceHealthPlus" />
		<beans:property name="configurationProperties">
			<beans:props>
				<beans:prop key="cacheEnabled">false</beans:prop>
			</beans:props>
		</beans:property>
		<beans:property name="typeAliasesPackage" value="cn.yunfox.healthplus.mybatis.domain" />
	</beans:bean>

	<!-- scan for mappers and let them be autowired -->
	<beans:bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<beans:property name="basePackage" value="cn.yunfox.healthplus.mybatis.persistence" />
		<beans:property name="annotationClass" value="cn.yunfox.healthplus.repositories.HealthPlusRepository"/>
        <beans:property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryHealthPlus"/>
	</beans:bean>
	
	<!-- MobileMall -->
	<beans:bean id="dataSourceMobileMall"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<beans:property name="driverClassName" value="${jdbc.driverClassName}" />
		<beans:property name="url" value="${jdbc.urlmobilemall}" />
	</beans:bean>

	<!-- define the SqlSessionFactory -->
	<beans:bean id="sqlSessionFactoryMobileMall" class="org.mybatis.spring.SqlSessionFactoryBean">
		<beans:property name="dataSource" ref="dataSourceMobileMall" />
		<beans:property name="configurationProperties">
			<beans:props>
				<beans:prop key="cacheEnabled">false</beans:prop>
			</beans:props>
		</beans:property>
		<beans:property name="typeAliasesPackage" value="cn.yunfox.mobilemall.mybatis.domain" />
	</beans:bean>

	<!-- scan for mappers and let them be autowired -->
	<beans:bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<beans:property name="basePackage" value="cn.yunfox.mobilemall.mybatis.persistence" />
		<beans:property name="annotationClass" value="cn.yunfox.healthplus.repositories.MobileMallRepository"/>
        <beans:property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryMobileMall"/>
	</beans:bean>
	<!-- mybatis ends -->


3:修改pom.xml的mybatis配置:

                <dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.2.0</version>
		</dependency>
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.2.1</version>
		</dependency>


4:在mapper的接口里边加上对应的annotationClass即可:

package cn.yunfox.mobilemall.mybatis.persistence;

import java.util.List;

import cn.yunfox.healthplus.repositories.MobileMallRepository;
import cn.yunfox.mobilemall.mybatis.domain.AliNotify;
import cn.yunfox.mobilemall.mybatis.domain.QueryByScope;

@MobileMallRepository
public interface AliNotifyMapper {
	void insertAliNotify( AliNotify alinotify );
}

5:一些相关修改

需要修改对应的dataSource名称,比如oauth2配置里边的

<bean id="tokenStore" class="org.springframework.security.oauth2.provider.token.JdbcTokenStore">
 		<constructor-arg ref="dataSourceMab" />
	</bean>

对于不能区分dataSource的,加@Resource(name = "dataSourcexxxxx"),例如

@Configuration

public class DataConfig {

	@Inject
	@Resource(name = "dataSourceHealthPlus")
	private DataSource dataSource;
	
	/**
	 * Allows repositories to access RDBMS data using the JDBC API.
	 */
	@Bean
	public JdbcTemplate jdbcTemplate() {
		return new JdbcTemplate(dataSource);
	}
	
	/**
	 * Allows transactions to be managed against the RDBMS using the JDBC API.
	 */
	@Bean
	public PlatformTransactionManager transactionManager() {
		return new DataSourceTransactionManager(dataSource);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值