码迷,mamicode.com
首页 > 编程语言 > 详细

配置多数据源 spring boot

时间:2019-10-10 14:08:53      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:where   uil   pac   ram   datalist   time()   art   manage   select   

一、使用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;

//表示这个类为一个配置类
@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();
}
}

配置多数据源 spring boot

标签:where   uil   pac   ram   datalist   time()   art   manage   select   

原文地址:https://www.cnblogs.com/FlyingTanks/p/11647561.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!