java大师博客网站

Spring Boot 2.0 教程

  • springboot集成springsecurity 1、整合springsecurity 添加pom.xml <dependency> <groupId>org.springframework.boot</grou...
    • 7月前
  • 4、验证码功能 1、编写工具类生成4位随机数 该工具类主要生成从0-9,a-z,A-Z范围内产生的4位随机数 /** * 产生4位随机字符串 */ public static String getCheckCode() { String base...
    • 7月前
  • 3、打印启动信息 3.1 spring Bean实例化流程 基本流程: 1、Spring容器在进行初始化时,会将xml或者annotation配置的bean的信息封装成一个BeanDefinition对象(每一个bean标签或者@bean注解都封...
    • 7月前
  • 前言 在实际项目研发中,需要针对不同的运行环境,如开发环境、测试环境、生产环境等,每个运行环境的数据库...等配置都不相同,每次发布测试、更新生产都需要手动修改相关系统配置。这种方式特别麻烦,费时费力,而且出错概率大。 Spring Boot为我...
    • 8月前
  • springboot在线人数统计 笔者做了一个网站,需要统计在线人数。 在线有两种: 一、如果是后台系统如果登录算在线,退出的时候或者cookie、token失效的时候就算下线 二、如果是网站前台,访问的时候就算在线 今天我们来讲一下第2种情况,...
    • 8月前
  • 手写springboot starter 一、创建自己的springboot-starter 好的,下面是手写一个自己的Spring Boot Starter自动装配的流程和代码: 1、创建一个Maven项目,命名为my-spring-boot-...
    • 1年前
  • 实际开发中, @RestControllerAdvice和 @ControllerAdvice怎么使用? 在实际开发中,@RestControllerAdvice 和 @ControllerAdvice 的使用场景有所不同。@RestCont...
    • 1年前
  • 2 springsecurity-jwt整合 欢迎关注博主公众号「Java大师」, 专注于分享Java领域干货文章http://www.javaman.cn/sb2/jwt 2.1整合springsecurity 1) <depende...
    • 1年前
  • springboot处理blog字段 欢迎关注博主公众号「Java大师」, 专注于分享Java领域干货文章https://www.javaman.cn/ 1、数据库表结构 其中content为longblob字段,代表存入的内容 CREA...
    • 1年前
  • Springboot+MybatisPlus多数据源比对数据 欢迎关注博主公众号「Java大师」, 专注于分享Java领域干货文章https://www.javaman.cn/ 一、工程目录 二、Mybatisplus多数据源配置 1、po...
    • 1年前
Springboot+MybatisPlus多数据源比对数据 - java大师  java大师博客网站

Springboot+MybatisPlus多数据源比对数据

欢迎关注博主公众号「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);
        }
    }
}


全部评论: 0

    我有话说: