idea中定时+多数据源配置

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

1.pom.xml



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.7.18-SNAPSHOT
        
    

    com.example
    pro
    0.0.1-SNAPSHOT
    war
    pro
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
        
            com.oracle.database.jdbc
            ojdbc8
            runtime
        
        
        
            org.projectlombok
            lombok
            true
        
        
        
            org.springframework.boot
            spring-boot-starter-tomcat
            provided
        
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            org.springframework
            spring-jdbc
        

        
            org.springframework.boot
            spring-boot-starter-aop
        

        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.2.2
        

        
        
            com.oracle.database.nls
            orai18n
            19.7.0.0
        

        
            org.springframework.boot
            spring-boot-autoconfigure
            2.5.7
        

        
        
            dm.jdbc
            DmJdbcDriver18
            18
        

        
        
            com.baomidou
            dynamic-datasource-spring-boot-starter
            3.4.1
        
        
            com.baomidou
            dynamic-datasource-spring-boot-starter
            3.4.1
        

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            

            
            
                org.mybatis.generator
                mybatis-generator-maven-plugin
                1.3.7
                
                    
                        ${basedir}/src/main/resources/generator/generatorConfig.xml
                    
                    true
                    true
                
                
                    
                        mysql
                        mysql-connector-java
                        8.0.16
                    

                    
                    
                        dm.jdbc
                        DmJdbcDriver18
                        18
                    

                    
                        tk.mybatis
                        mapper
                        4.1.5
                    
                
            

        
    
    
        
            spring-milestones
            Spring Milestones
            https://repo.spring.io/milestone
            
                false
            
        
        
            spring-snapshots
            Spring Snapshots
            https://repo.spring.io/snapshot
            
                false
            
        
    
    
        
            spring-milestones
            Spring Milestones
            https://repo.spring.io/milestone
            
                false
            
        
        
            spring-snapshots
            Spring Snapshots
            https://repo.spring.io/snapshot
            
                false
            
        
    


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 getList();

    @DS(value = "db2")
    int insetList(ArrayList 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&

 idea中定时+多数据源配置_第1张图片

你可能感兴趣的:(java,多数据源,oracle,dm,cron,war)