idea中定时+多数据源配置

发布时间:2023年12月18日

因项目要求,需要定时从达梦数据库中取数据,并插入或更新到ORACLE数据库中

1.pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.18-SNAPSHOT</version>
        <relativePath/>
    </parent>

    <groupId>com.example</groupId>
    <artifactId>pro</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>pro</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        
        <dependency>
            <groupId>com.oracle.database.jdbc</groupId>
            <artifactId>ojdbc8</artifactId>
            <scope>runtime</scope>
        </dependency>
        
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.2</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.oracle.database.nls/orai18n -->
        <dependency>
            <groupId>com.oracle.database.nls</groupId>
            <artifactId>orai18n</artifactId>
            <version>19.7.0.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>2.5.7</version>
        </dependency>

        <!-- 达梦数据库驱动  -->
        <dependency>
            <groupId>dm.jdbc</groupId>
            <artifactId>DmJdbcDriver18</artifactId>
            <version>18</version>
        </dependency>

        <!--苞米豆的多数据源配置依赖-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
            <version>3.4.1</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
            <version>3.4.1</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>

            <!-- generator插件 -->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.7</version>
                <configuration>
                    <configurationFile>
                        ${basedir}/src/main/resources/generator/generatorConfig.xml
                    </configurationFile>
                    <overwrite>true</overwrite>
                    <verbose>true</verbose>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>8.0.16</version>
                    </dependency>

                    <!-- 达梦数据库驱动  -->
                    <dependency>
                        <groupId>dm.jdbc</groupId>
                        <artifactId>DmJdbcDriver18</artifactId>
                        <version>18</version>
                    </dependency>

                    <dependency>
                        <groupId>tk.mybatis</groupId>
                        <artifactId>mapper</artifactId>
                        <version>4.1.5</version>
                    </dependency>
                </dependencies>
            </plugin>

        </plugins>
    </build>
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <releases>
                <enabled>false</enabled>
            </releases>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <releases>
                <enabled>false</enabled>
            </releases>
        </pluginRepository>
    </pluginRepositories>

</project>

2.yml 文件

server:
  port: 9321

spring:
  datasource:
    dynamic:
      primary: master #设置默认的数据源或者数据源组,默认值即为master
      strict: true  #设置严格模式,默认false不启动,启动后在未匹配到指定数据源时,会抛出异常,不启动则使用默认数据源
      datasource:
        master: #主数据源
          url: jdbc:dm://127.0.0.1:5236/test?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8
          username: SYSDBA
          password: SYSDBA
          driver-class-name: dm.jdbc.driver.DmDriver
        db2 : #子数据源
          url: jdbc:oracle:thin:@localhost:1521:ORCL?useUnicode=true&characterEncoding=ZHS16GBK
          username: avic
          password: avic
          driver-class-name: oracle.jdbc.driver.OracleDriver

mybatis:
  mapper-locations: classpath:/mapper/*.xml # 配置MyBatis-Plus扫描Mapper文件的位置
  type-aliases-package: com.example.pro.pojo # 创建别名的类所在的包

logging:
  level:
    com:
      example:
        pro: debug
  file:
    name: ./logs/my.log
  pattern:
    console: "%d{yyyy/MM/dd-HH:mm:ss} [%thread] %-5level %logger- %msg%n"
    file: "%d{yyyy/MM/dd-HH:mm} [%thread] %-5level %logger- %msg%n"

3.mapper

主数据源是达梦的,达梦mapper调用不需要额外的配置,正常使用即可

从数据源是oracle的,需要额外进行配置 @DS(value = "从数据源")

package com.example.pro.mapper;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.example.pro.pojo.AvicUser;
import org.apache.ibatis.annotations.Mapper;

import java.util.ArrayList;
import java.util.List;

/**
 * OracleMapper
 */

//@Repository
@Mapper
public interface OracleMapper {

    @DS(value = "db2")
    List<AvicUser> getList();

    @DS(value = "db2")
    int insetList(ArrayList<AvicUser> avicList);

    @DS(value = "db2")
    void insertObj(AvicUser avicUser);
}

4.定时任务

package com.example.pro.timer;

import com.example.pro.service.IOperationService;
import com.example.pro.service.IOracleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 定时任务 , 每天 晚上 11点 执行
 */

@Slf4j
@Component
public class Schedule {

    @Autowired
    private IOperationService operationService;

    @Autowired
    private IOracleService oracleService;

    public Schedule() {
        log.debug("创建计划任务类对象:Schedule");
    }

    /*@Scheduled(cron = "0 0 23 * * ? ")
     每天晚上11点开始执行任务*/

    //@Scheduled(fixedRate = 1 * 60 * 1000)
    @Scheduled(cron = "0/300 * * * * ?")   //30秒执行一次
    public  void timedTask() {
        log.debug("开始执行处理定时的计划任务……");
        operationService.angelhood();
        //oracleService.getDmlist();
        //oracleService.getOraclelist();
        log.debug("处理定时的计划任务执行完成!");
    }
}

5.启动类

package com.example.pro;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})//多数据源时是使用
@EnableAspectJAutoProxy //开启Spring Boot对AOP的支持
@MapperScan("com.example.pro.mapper")//包扫描
@EnableScheduling//开启定时
public class ProApplication {

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

}

6.打包+运行指令

因项目需要 package 打包成了 war 包

nohup java -jar pro-0.0.1-SNAPSHOT.war > oracle.log 2>& 1&

?

文章来源:https://blog.csdn.net/m0_69712602/article/details/134995707
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。