配置多数据源 spring boot

元气小坏坏 提交于 2019-12-01 02:17:06

一、使用jdbc 配置多数据源

1.yml数据源配置

 

 

 

2.配置类

package com.v246.common.config.datasource;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Qualifier;
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.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

//表示这个类为一个配置类
@Configuration
public class DatasourceConfig {

    // 将这个对象放入Spring容器中
    @Bean("datavisualDataSource")
    // 表示这个数据源是默认数据源
    @Primary
    @Qualifier("datavisualDataSource")
    // 读取application.properties中的配置参数映射成为一个对象
    // prefix表示参数的前缀
    @ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.datavisual")
    public DataSource datavisualDataSource(){
        return DruidDataSourceBuilder.create().build();
    }

    @Bean("vlmsDataSource")
    @Qualifier("vlmsDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.vlms")
    public DataSource vlmsDataSource(){
        return DruidDataSourceBuilder.create().build();
    }





    @Bean("jdbcTemplate")
    public JdbcTemplate datavisualJdbcOperations(@Qualifier("datavisualDataSource") DataSource datavisualDataSource) {
        return new JdbcTemplate(datavisualDataSource);
    }
    @Bean("vlmsJdbcTemplate")
    public JdbcOperations vlmsJdbcOperations(@Qualifier("vlmsDataSource") DataSource vlmsDataSource) {
        return new JdbcTemplate(vlmsDataSource);
    }





    @Bean
    @Primary
    public PlatformTransactionManager datavisualTransactionManager(@Qualifier("datavisualDataSource") DataSource datavisualDataSource) {
        return new DataSourceTransactionManager(datavisualDataSource);
    }
    @Bean
    public PlatformTransactionManager vlmsTransactionManager(@Qualifier("vlmsDataSource") DataSource vlmsDataSource) {
        return new DataSourceTransactionManager(vlmsDataSource);
    }


    @Bean(name = "sqlSessionFactory")
    // 表示这个数据源是默认数据源
    @Primary
    // @Qualifier表示查找Spring容器中名字为DataSource的对象
    public SqlSessionFactory test1SqlSessionFactory(@Qualifier("datavisualDataSource") DataSource datasource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(datasource);
        bean.setMapperLocations(
                // 设置mybatis的xml所在位置
                new PathMatchingResourcePatternResolver().getResources("classpath*:mapping/mapper/*.xml"));
        return bean.getObject();
    }


    @Bean(name = "vlmsSqlSessionFactory")
    // @Qualifier表示查找Spring容器中名字为DataSource的对象
    public SqlSessionFactory vlmsSqlSessionFactory(@Qualifier("vlmsDataSource") DataSource datasource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(datasource);
        bean.setMapperLocations(
                // 设置mybatis的xml所在位置
                new PathMatchingResourcePatternResolver().getResources("classpath*:mapping/test01/*.xml"));
        return bean.getObject();
    }
}

3.测试类

public class SqlserverServiceTest {
     @Resource
     JdbcTemplate vlmsJdbcTemplate;

     @Autowired
     NamedDb db;

    @Test
    public void testTime() {


        Map<String,Object> map = vlmsJdbcTemplate.queryForMap("SELECT * FROM wx_user WHERE user_id='***'");
        System.out.println(map);
        Record rc = db.findFirst("SELECT * FROM SYSC03 WHERE CGRDM='***'");
        System.out.println(rc);

    }


}

 

二、使用mybatis 配置多数据源

1.yml数据源配置

 

 

 2.prom.xml配置

        <!--引入dynamic-datasource-spring-boot-starter。-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
            <version>2.5.6</version>
        </dependency>

3.测试类

// 默认为主数据源  使用@DS("数据源名称")则使用新其他数据源@DS("vlms") 
public interface BiZcPlanShipmentMapper extends IBaseMapper<BiZcPlanShipment> {

    Page<Record> getDataList(@Param("filterPage")Page<Record> page);

}

第二种mybatis 配置方法可参考 https://mp.baomidou.com/guide/dynamic-datasource.html

 

 

 

package com.v246.common.config.datasource;import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;import org.apache.ibatis.session.SqlSessionFactory;import org.mybatis.spring.SqlSessionFactoryBean;import org.springframework.beans.factory.annotation.Qualifier;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.core.JdbcOperations;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.jdbc.datasource.DataSourceTransactionManager;import org.springframework.transaction.PlatformTransactionManager;import javax.sql.DataSource;//表示这个类为一个配置类@Configurationpublic class DatasourceConfig {    // 将这个对象放入Spring容器中@Bean("datavisualDataSource")    // 表示这个数据源是默认数据源@Primary    @Qualifier("datavisualDataSource")    // 读取application.properties中的配置参数映射成为一个对象    // prefix表示参数的前缀@ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.datavisual")    public DataSource datavisualDataSource(){        return DruidDataSourceBuilder.create().build();}    @Bean("vlmsDataSource")    @Qualifier("vlmsDataSource")    @ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.vlms")    public DataSource vlmsDataSource(){        return DruidDataSourceBuilder.create().build();}    @Bean("jdbcTemplate")    public JdbcTemplate datavisualJdbcOperations(@Qualifier("datavisualDataSource") DataSource datavisualDataSource) {        return new JdbcTemplate(datavisualDataSource);}    @Bean("vlmsJdbcTemplate")    public JdbcOperations vlmsJdbcOperations(@Qualifier("vlmsDataSource") DataSource vlmsDataSource) {        return new JdbcTemplate(vlmsDataSource);}    @Bean    @Primarypublic PlatformTransactionManager datavisualTransactionManager(@Qualifier("datavisualDataSource") DataSource datavisualDataSource) {        return new DataSourceTransactionManager(datavisualDataSource);}    @Beanpublic PlatformTransactionManager vlmsTransactionManager(@Qualifier("vlmsDataSource") DataSource vlmsDataSource) {        return new DataSourceTransactionManager(vlmsDataSource);}    @Bean(name = "sqlSessionFactory")    // 表示这个数据源是默认数据源@Primary// @Qualifier表示查找Spring容器中名字为DataSource的对象public SqlSessionFactory test1SqlSessionFactory(@Qualifier("datavisualDataSource") DataSource datasource)            throws Exception {        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(datasource);bean.setMapperLocations(                // 设置mybatis的xml所在位置new PathMatchingResourcePatternResolver().getResources("classpath*:mapping/mapper/*.xml"));        return bean.getObject();}    @Bean(name = "vlmsSqlSessionFactory")    // @Qualifier表示查找Spring容器中名字为DataSource的对象public SqlSessionFactory vlmsSqlSessionFactory(@Qualifier("vlmsDataSource") DataSource datasource)            throws Exception {        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(datasource);bean.setMapperLocations(                // 设置mybatis的xml所在位置new PathMatchingResourcePatternResolver().getResources("classpath*:mapping/test01/*.xml"));        return bean.getObject();}}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!