springboot+mybatis配置多数据源

前景:mvn+springboot+mybatis+spring默认的数据源配置(类似spring.datasource)

随着项目的深入可能会遇到配置多数据源的问题

配置数据源原理:提供不同的dataSource,sqlSession,sqlTemplate即可

步骤:

  1)配置application.properties数据源 

server.port=8080

#主数据源配置tyzx
spring.datasource.tyzx.url=jdbc:mysql://ip/tyzx?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.tyzx.username=aaa
spring.datasource.tyzx.password=aaa
spring.datasource.tyzx.driver-class-name=com.mysql.jdbc.Driver

#次数据源配置gpsdb
spring.datasource.gpsdb.url=jdbc:mysql://ip/gpsdb?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.gpsdb.username=aaaa
spring.datasource.gpsdb.password=aaa
spring.datasource.gpsdb.driver-class-name=com.mysql.jdbc.Driver
View Code

  2)配置配置文件这里通过注解方式得到配置文件(配置sqlSessionTemplate)

数据源1

package com.ty.tyzxtj.config;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

/**
 * 添加一个新的数据源
 * @author wangjiping
 *
 */
@Configuration
@MapperScan(basePackages="com.ty.tyzxtj.mapper.gpsdb",sqlSessionTemplateRef="gpsdbSqlSessionTemplate")
public class DataSourceGpsDbConfig {
    @Bean(name="gpsdbDataSource")
    @ConfigurationProperties(prefix="spring.datasource.gpsdb")
    public DataSource gpsdbDataSource(){
        return DataSourceBuilder.create().build();
    }
    @Bean(name="gpsdbSqlSessionFactory")
    public SqlSessionFactory gpsdbSqlSessionFactory(@Qualifier("gpsdbDataSource") DataSource dataSource) throws Exception{
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath:mappers/gpsdb*.xml"));
        return bean.getObject();
    }
    @Bean(name = "gpsdbTransactionManager")
    public DataSourceTransactionManager gpsdbTransactionManager(@Qualifier("gpsdbDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
    @Bean(name="gpsdbSqlSessionTemplate")
    public SqlSessionTemplate gpsdbSqlSessionTemplate(@Qualifier("gpsdbSqlSessionFactory") SqlSessionFactory sqlSessionFactory)throws Exception{
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
View Code

数据源2

package com.ty.tyzxtj.config;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

/**
 * 添加一个新的数据源
 * @author wangjiping
 *
 */
@Configuration
@MapperScan(basePackages="com.ty.tyzxtj.mapper",sqlSessionTemplateRef="tyzxSqlSessionTemplate")
public class DataSourceTyzxConfig {
    @Bean(name="tyzxDataSource")
    @ConfigurationProperties(prefix="spring.datasource.tyzx")
    @Primary
    public DataSource tyzxDataSource(){
        return DataSourceBuilder.create().build();
    }
    @Bean(name="tyzxSqlSessionFactory")
    @Primary
    public SqlSessionFactory tyzxSqlSessionFactory(@Qualifier("tyzxDataSource") DataSource dataSource) throws Exception{
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath:mappers/*.xml"));
        return bean.getObject();
    }
    @Bean(name = "tyzxTransactionManager")
    @Primary
    public DataSourceTransactionManager tyzxTransactionManager(@Qualifier("tyzxDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
    @Bean(name="tyzxSqlSessionTemplate")
    public SqlSessionTemplate tyzxSqlSessionTemplate(@Qualifier("tyzxSqlSessionFactory") SqlSessionFactory sqlSessionFactory)throws Exception{
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
View Code

需要注意的:如果mapper.java对应的mapper.xml位置通过重新设置bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mappers/*.xml"));同时之前单一数据源对应mybatis的配置需要删除了不然会影响新的配置

posted @ 2017-10-30 12:58  王吉平  阅读(637)  评论(0编辑  收藏  举报