开发者

idea中定时及多数据源配置方法

开发者 https://www.devze.com 2023-12-20 10:17 出处:网络 作者: 新子-存在了
目录1.pom.XML2.yml 文件3.mapper4.定时任务5.启动类6.打包+运行指令因项目要求,需要定时从达梦数据库中取数据,并插入或更新到oracle数据库中
目录
  • 1.pom.XML
  • 2.yml 文件
  • 3.mapper
  • 4.定时任务
  • 5.启动类
  • 6.打包+运行指令

因项目要求,需要定时从达梦数据库中取数据,并插入或更新到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>
        </dependenjavascriptcy>
        <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>
         http://www.devze.com       <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.ArrayLis编程t;
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")//包扫描
@EnableSc编程heduling//开启定时
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中定时及多数据源配置方法

到此这篇关于idea中定时+多数据源配置的文章就介绍到这了,更多相关idea多数据源配置内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号