关闭

spring mybatis多数据源实践

856人阅读 评论(0) 收藏 举报
分类:

参考这篇文章: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);
	}
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:354858次
    • 积分:4582
    • 等级:
    • 排名:第6675名
    • 原创:167篇
    • 转载:54篇
    • 译文:0篇
    • 评论:59条
    最新评论