首页 > 其他分享 >Springboot+MybatisPlus多数据源比对数据

Springboot+MybatisPlus多数据源比对数据

时间:2022-08-26 17:11:43浏览次数:75  
标签:MybatisPlus Springboot 数据源 private baomidou org import com ds

欢迎关注博主公众号「Java大师」, 专注于分享Java领域干货文章https://www.javaman.cn/

基于不同的数据源进行数据比对,比较两个数据库的表之间数据的总量,以及处理后的总量,按天进行比对,将比对结果输出到核对表中。

一、工程目录

在这里插入图片描述

二、Mybatisplus多数据源配置

1、pom.xml
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.oracle.database.jdbc/ojdbc6 -->
<dependency>
    <groupId>com.oracle.database.jdbc</groupId>
    <artifactId>ojdbc6</artifactId>
    <version>11.2.0.4</version>
</dependency>

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
    <version>3.3.2</version>
</dependency>
2、application.yml
spring:
  datasource:
    dynamic:
      primary: tfss   #默认数据源
      strict: false
      datasource:
        ttba: #数据源1
          username: ttba
          password: ttba
          url: jdbc:oracle:thin:@127.0.0.1:1521:ORCL
          driver-class-name: oracle.jdbc.driver.OracleDriver
        tfss: #数据源2
          username: tfss
          password: tfss
          url: jdbc:oracle:thin:@127.0.0.1:1521:ORCL
          driver-class-name: oracle.jdbc.driver.OracleDriver
mybatis:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl  #开启MybatisPlus输出

三、业务代码

1、MybatisPlusConfig
package com.ds.check.config;

import com.baomidou.mybatisplus.extension.incrementer.OracleKeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
	添加oracle的主键策略
*/
@Configuration
public class MybatisPlusConfig {
    @Bean
    public OracleKeyGenerator oracleKeyGenerator(){
        return new OracleKeyGenerator();
    }
}
2、实体类
核对结果表
@Data
@TableName("ST_ZFQLC_CHECK_DATA")
@KeySequence("SEQ_ZFQLC_CHECK_DATA")
public class ZfqlcCheckData {
    @TableId(value = "Id",type = IdType.INPUT)
    private Integer id;
    private String xtbaTable;
    private String zfqlcTable;
    private String transTable;
    private Integer zfqlcSl;
    private Integer transSl;
    private Integer xtbaSl;
    private String cycle;
配置表
package com.ds.check.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.KeySequence;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

@Data
@TableName("ST_ZFQLC_CHECK_TABLE")
public class ZfqlcCheckTable {
    private String xtbaTable;
    private String zfqlcTable;
    private String transTable;
}
3、mapper接口
package com.ds.check.mapper;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

@Mapper
@DS("tfss")  //DS关键字配置数据源
public interface TransMapper extends BaseMapper {
    Integer selectTotal(@Param("tableName") String tableName);
}
package com.ds.check.mapper;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

@Mapper
@DS("ttba") //DS关键字配置数据源
public interface XtbaMapper extends BaseMapper {
    Integer selectTotal(@Param("tableName") String tableName);
}
package com.ds.check.mapper;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ds.check.entity.ZfqlcCheckData;
import org.apache.ibatis.annotations.Mapper;

@Mapper
@DS("ttfs")
public interface ZfqlcCheckDataMapper extends BaseMapper<ZfqlcCheckData> {
}
package com.ds.check.mapper;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ds.check.entity.ZfqlcCheckData;
import com.ds.check.entity.ZfqlcCheckTable;
import org.apache.ibatis.annotations.Mapper;

@Mapper
@DS("ttfs")
public interface ZfqlcCheckTableMapper extends BaseMapper<ZfqlcCheckTable> {
}
package com.ds.check.mapper;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

@Mapper
@DS("ttfs")
public interface ZfqlcMapper extends BaseMapper {
    Integer selectTotal(@Param("tableName") String tableName);
}
4、mapper.xml
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.ds.check.mapper.ZfqlcMapper">

    <!-- selectByName -->
    <select id="selectTotal" resultType="java.lang.Integer">
        select count(*) from ${tableName} where to_char(zxgxsj,'yyyymmdd')=to_char(sysdate-1,'yyyymmdd')
    </select>

</mapper>

四、主程序入口

package com.ds.check;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
import com.ds.check.entity.ZfqlcCheckData;
import com.ds.check.entity.ZfqlcCheckTable;
import com.ds.check.mapper.*;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)
@MapperScan(value = "com.ds.check.mapper")
@Slf4j
//实现CommandLineRunner类似于普通java程序的main函数入口
public class CheckApplication implements CommandLineRunner {

    @Autowired
    private ZfqlcCheckDataMapper zfqlcCheckDataMapper;
    @Autowired
    private ZfqlcCheckTableMapper zfqlcCheckTableMapper;

    @Autowired
    private XtbaMapper xtbaMapper;
    @Autowired
    private ZfqlcMapper zfqlcMapper;
    @Autowired
    private TransMapper transMapper;


    public static void main(String[] args) {
        SpringApplication.run(CheckApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE,-1);
        String yesterday = new SimpleDateFormat( "yyyyMMdd").format(cal.getTime());
        System.out.println(yesterday);
        List<ZfqlcCheckTable> list = zfqlcCheckTableMapper.selectList(null);
        for (ZfqlcCheckTable zfqlcCheckTable : list) {
            log.info("开始处理记录:"+zfqlcCheckTable.getXtbaTable()+","+zfqlcCheckTable.getZfqlcTable()+","+zfqlcCheckTable.getTransTable());
            Integer totalXtba = xtbaMapper.selectTotal(zfqlcCheckTable.getXtbaTable());
            Integer totalZfqlc = zfqlcMapper.selectTotal(zfqlcCheckTable.getZfqlcTable());
            Integer totaltrans = transMapper.selectTotal(zfqlcCheckTable.getTransTable());
            ZfqlcCheckData zfqlcCheckData = new ZfqlcCheckData();
            zfqlcCheckData.setXtbaTable(zfqlcCheckTable.getXtbaTable());
            zfqlcCheckData.setZfqlcTable(zfqlcCheckTable.getZfqlcTable());
            zfqlcCheckData.setTransTable(zfqlcCheckTable.getTransTable());
            zfqlcCheckData.setXtbaSl(totalXtba);
            zfqlcCheckData.setZfqlcSl(totalZfqlc);
            zfqlcCheckData.setTransSl(totaltrans);
            zfqlcCheckData.setCycle(yesterday);
            zfqlcCheckDataMapper.insert(zfqlcCheckData);
        }
    }
}

标签:MybatisPlus,Springboot,数据源,private,baomidou,org,import,com,ds
From: https://www.cnblogs.com/dalaba/p/16628200.html

相关文章

  • 【尚硅谷】SpringBoot2零基础入门教程(spring boot2干货满满)雷神讲授
    https://www.bilibili.com/video/BV19K4y1L7MT?spm_id_from=333.337.search-card.all.click&vd_source=b0cee746b0adbaa67743475d986bd0f8P1: P2:spring生态圈<--1:1......
  • springboot:@RequestBody 注解只能处理json格式的请求字符串吗?
    原来@RequestBody注解常用来处理content-type是application/json编码的内容,而不能用来处理application/x-www-form-urlcoded编码的内容。参考:https://blog.csdn.n......
  • 使用函数计算自定义运行时快速部署一个 SpringBoot 项目 | 文末有礼
    作者:谱一段风华笔墨什么是函数计算阿里云函数计算FC是事件驱动的全托管计算服务。使用函数计算,您无需采购与管理服务器等基础设施,只需编写并上传代码。函数计算为您准......
  • SparkSQL支持的数据源
    1.SparkSQL支持的数据源HiveScala内存中数据--集合支持从RDD读取数据作SQL操作支持从外部存储文件读取数据json,csv,普通结构文本文件支持从关系型数据库读取数据处理......
  • springboot集成slf4j配置日志
    slf4j简介slf4j是对所有日志框架制定的一种规范、标准、接口,而不是一个框架的具体实现。springboot集成slf4j的简单示例springboot内部已经集成slf4j。@SpringBootAppl......
  • springboot项目的部署
    方式1:打包成jar包pom.xml中指定项目的打包方式<!--可省略--><packaging>jar</packaging>使用maven进行打包将jar包放到任意一个目录下,执行如下命令nohup命......
  • SpringBoot - 文件上传原理
    文件上传原理来个例子客户端<formrole="form"th:action="@{/upload}"method="post"enctype="multipart/form-data"><divclass="form-group"><label......
  • 【文件下载】SpringBoot文件下载
    1.将文件以流的形式一次性读取到内存,通过响应输出流输出到前端/***@parampath想要下载的文件的路径*@paramresponse*@功能描述下载文件:*/@RequestMapping("......
  • SpringBoot修改启动端口server.port方式
     springboot服务修改端口的方式1、修改application.yml文件这是最简单的方式 2、以jdk参数方式启动java-Dserver.port=3000-jarxx.jar 3、启动参数java......
  • Springboot接入xxl-job
    环境springboot:2.7.2xxl-job-admin:2.3.0xxl-job:2.3.1搭建xxl-job-admin初始化数据库脚本:xxl-job/tables_xxl_job.sqlatmaster·xuxueli/xxl-job(github.com)使......