zoukankan      html  css  js  c++  java
  • 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的配置需要删除了不然会影响新的配置

  • 相关阅读:
    关于Update语句在不同数据库中的差别
    MSIL指令速查表
    一个对于博客园的建议
    代码风格关于if语句
    关于Page.cs文件中注释的一点补充
    在Java、C#和C++中遍历集合
    BPEL4WS的开源Java实现
    【Linux】linux固定ip
    【Linux】【MySQL】MySQL主从数据库
    wpf 写个简单的控件吧
  • 原文地址:https://www.cnblogs.com/xiaoping1993/p/7754013.html
Copyright © 2011-2022 走看看