SpringBoot配置多数据源

目的

需求背景
      需统计区域拉新人员的业绩,即统计通过该途径注册的会员的消费情况。需获取 两个数据库实例下数据库表数据,然后通过关联字段将两张表的数据整合一起。

a表的数据

会员id区域活动名称
1华南乳饮推广001
2华北面膜推广002
b表的数据

会员id订单id发货时间销售金额
1000012020-11-06 15:05:3615.90
1000022020-11-02 15:05:3649.90
2000032020-11-04 15:05:3659.90
3000042020-11-03 15:05:3639.90
最终要获取的数据

会员id区域活动名称订单id发货时间销售金额
1华南乳饮推广001000012020-11-06 15:05:3615.90
1华南乳饮推广001000022020-11-02 15:05:3649.90
2华北面膜推广002000032020-11-04 15:05:3659.90

SpringBoot配置多数据源

PS:两个实例都是mysql数据库。
创建SpringBoot项目

项目创建完后导入依赖

<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
	<scope>runtime</scope>
</dependency>
配置数据源1
package com.pec.reportdata.config;

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.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
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;

import javax.sql.DataSource;

/**
 * 配置crm数据源
 */
@Configuration
@MapperScan(basePackages = "com.pec.reportdata.mapper.crm", sqlSessionTemplateRef = "crmSqlSessionTemplate")

public class DataSourceCrm {
    @Bean(name = "crmDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.crm")
    @Primary
    public DataSource crmDataSource(){
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "crmSqlSessionFactory")
    @Primary
    public SqlSessionFactory crmSqlSessionFactory(@Qualifier("crmDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/crm/*.xml"));
        return bean.getObject();
    }

    @Bean(name = "crmTransactionManager")
    @Primary
    public DataSourceTransactionManager crmTransactionManager(@Qualifier("crmDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "crmSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate crmSqlSessionTemplate(@Qualifier("crmSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}


配置数据源2
package com.pec.reportdata.config;

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.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = "com.pec.reportdata.mapper.jfsc", sqlSessionTemplateRef = "jfscSqlSessionTemplate")
public class DataSourceJfsc {
    @Bean(name = "jfscDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.jfsc")
    public DataSource jfscDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "jfscSqlSessionFactory")
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("jfscDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/jfsc/*.xml"));
        return bean.getObject();
    }

    @Bean(name = "jfscTransactionManager")
    public DataSourceTransactionManager jfscTransactionManager(@Qualifier("jfscDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "jfscSqlSessionTemplate")
    public SqlSessionTemplate jfscSqlSessionTemplate(@Qualifier("jfscSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

application.yml配置

ps:此处url必须使用 jdbc-url

server:
  port: 8092
spring:
  datasource:
    crm:
      jdbc-url: jdbc:mysql://xxx:3306/xxx?serverTimezone=GMT
      driver-class-name: com.mysql.cj.jdbc.Driver
      username: xxx
      password: xxx
    jfsc:
      jdbc-url: jdbc:mysql://xxx:3306/xxx?serverTimezone=GMT
      driver-class-name: com.mysql.cj.jdbc.Driver
      username: xxx
      password: xxx
项目结构目录

在这里插入图片描述

整合数据逻辑

		//会员id 订单id 发货时间 实付金额 集合
		List<Order> orderList = jfscMapper.getOrderListEvery();
        //会员id,区域,活动名 集合
        List<Order> reportData = crmMapper.getReportData(orderList);
        // 通过id去重(其实没重复的,因为已经通过id,区域,活动名分组了)
        Map<Integer, Order> map = reportData.stream()
                .collect(Collectors.toMap(Order::getMemberId, Function.identity(), (key1, key2) -> key2));
  	   //合并,将两个list集合根据id进行合并
        orderList.forEach(x->{
            if(map.containsKey(x.getMemberId())){
                Order order = map.get(x.getMemberId());
                x.setTag(order.getTag());
                x.setActivityName(order.getActivityName());
            }
        });
        //做一轮判断,如果集合对象中tag,activity_name为null则说明不是区域注册人员的订单,去掉这些。
        for(int i=0;i<orderList.size();i++){
            if(orderList.get(i).getTag()==null || orderList.get(i).getActivityName()==null){
                orderList.remove(i--);
            }
        }
       log.info("最终清洗出来的数据有"+orderList.size()+"条");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值