1.开两个相同的idea项目
2.配置oracle的依赖
<?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.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ckf</groupId>
<artifactId>login_wx</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>login_wx</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.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<!--模板引擎-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- 引入阿里数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.14</version>
</dependency>
<!-- mysql依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.42</version>
<scope>runtime</scope>
</dependency>
<!-- Oracle 连接驱动依赖 -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency>
<!-- mybatisPlus 核心库 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.0</version>
</dependency>
<!--生成实体成fet set-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- pagehelper 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
<!--junit 测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.配置application.yml数据源
server:
port: 8090
# Spring Boot 的数据源配置
spring:
datasource:
driver-class-name: oracle.jdbc.driver.OracleDriver
url: jdbc:oracle:thin:@127.0.0.1:1521:orcl
username: scott
password: Aaaa8888
# mybatis-plus相关配置
mybatis-plus:
# xml扫描,多个目录用逗号或者分号分隔(告诉 Mapper 所对应的 XML 文件位置)
mapper-locations: classpath:mapper/*.xml
# 以下配置均有默认值,可以不设置
global-config:
db-config:
#主键类型 AUTO:"数据库ID自增" INPUT:"用户输入ID",ID_WORKER:"全局唯一ID (数字类型唯一ID)", UUID:"全局唯一ID UUID";
id-type: auto
#字段策略 IGNORED:"忽略判断" NOT_NULL:"非 NULL 判断") NOT_EMPTY:"非空判断"
field-strategy: NOT_EMPTY
#数据库类型
db-type: MYSQL
# 指定实体类的包
type-aliases-package: com.jsxy.entity
configuration:
# 是否开启自动驼峰命名规则映射:从数据库列名到Java属性驼峰命名的类似映射
map-underscore-to-camel-case: true
# 如果查询结果中包含空值的列,则 MyBatis 在映射的时候,不会映射这个字段
call-setters-on-nulls: true
# 这个配置会将执行的sql打印出来,在开发或测试的时候可以用
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
# 定时任务配置
my:
test:
startTime: "0 46 22 * * ?"
4.编写java代码
线程池类
package com.jsxy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.ThreadPoolExecutor;
/**
* @author Administrator
* @ClassName ThreadPoolTaskConfig
* @description: 线程池配置
* @date 2024年02月02日
* @version: 1.0
*/
@Configuration
@EnableAsync
public class ThreadPoolTaskConfig {
/**
* 默认情况下,在创建了线程池后,线程池中的线程数为0,当有任务来之后,就会创建一个线程去执行任务,
* 当线程池中的线程数目达到corePoolSize后,就会把到达的任务放到缓存队列当中;
* 当队列满了,就继续创建线程,当线程数量大于等于maxPoolSize后,开始使用拒绝策略拒绝
*/
/**
* 核心线程数(默认线程数)
*/
private static final int corePoolSize = 20;
/**
* 最大线程数
*/
private static final int maxPoolSize = 150;
/**
* 允许线程空闲时间(单位:默认为秒)
*/
private static final int keepAliveTime = 10;
/**
* 缓冲队列大小
*/
private static final int queueCapacity = 200;
/**
* 线程池名前缀
*/
private static final String threadNamePrefix = "Async-Service-";
@Bean("taskExecutor") // bean的名称,默认为首字母小写的方法名
public ThreadPoolTaskExecutor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(corePoolSize);
executor.setMaxPoolSize(maxPoolSize);
executor.setQueueCapacity(queueCapacity);
executor.setKeepAliveSeconds(keepAliveTime);
executor.setThreadNamePrefix(threadNamePrefix);
// 线程池对拒绝任务的处理策略
// CallerRunsPolicy:由调用线程(提交任务的线程)处理该任务
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
// 初始化
executor.initialize();
return executor;
}
}
定时任务类
package com.jsxy;
import com.jsxy.entity.User;
import com.jsxy.servic.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author Administrator
* @ClassName TestScheduling
* @description: TODO
* @date 2024年02月02日
* @version: 1.0
*/
@Component
public class TestScheduling {
@Autowired
private UserService userService;
@Async(value="taskExecutor")
@Scheduled(cron="${my.test.startTime}")
public void printVal(){
//TODO 业务逻辑代码
User user = new User();
user.setName("王小王");
user.setAdress("江苏盱眙");
user.setAge(28);
user.setSex("男");
Date data = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dataStr = sdf.format(data);
user.setCreateTime(dataStr);
userService.insertUser(user);
}
}
接口类
package com.jsxy.servic.impl;
import com.jsxy.entity.User;
import com.jsxy.mapper.UserMapper;
import com.jsxy.servic.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class UserServiceImpl implements UserService {
@Autowired
UserMapper userMapper;
@Override
public Integer insertUser(User user) {
return userMapper.insertUser(user);
}
}
package com.jsxy.servic;
import com.jsxy.entity.User;
public interface UserService {
Integer insertUser(User user);
}
package com.jsxy.mapper;
import com.jsxy.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface UserMapper {
Integer insertUser(@Param("user") User user);
}
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.jsxy.mapper.UserMapper">
<insert id="insertUser1" parameterType="com.jsxy.entity.User">
insert into USERS(name, age, adress, sex, create_time)
values (#{user.name}, #{user.age}, #{user.adress}, #{user.sex}, #{user.createTime})
</insert>
<insert id="insertUser" parameterType="com.jsxy.entity.User">
MERGE INTO USERS t
USING (SELECT #{user.name} as name,
#{user.age} as age,
#{user.adress} as adress,
#{user.sex} as sex,
#{user.createTime} as create_time
from dual) s
ON (t.name = s.name and t.age = s.age and t.adress = s.adress)
WHEN MATCHED THEN
UPDATE SET t.create_time = s.create_time
WHEN NOT MATCHED THEN
INSERT (name, age, adress, sex, create_time)
VALUES (#{user.name}, #{user.age}, #{user.adress}, #{user.sex}, #{user.createTime})
</insert>
</mapper>
启动类
package com.jsxy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
/**
* @author Administrator
* @ClassName SchedulingApplication
* @description: TODO
* @date 2024年02月02日
* @version: 1.0
*/
@EnableAsync
@EnableScheduling
@SpringBootApplication
public class SchedulingApplication {
public static void main(String[] args) {
SpringApplication.run(SchedulingApplication.class, args);
}
}
oracle建表
-- Create table
create table USERS
(
name VARCHAR2(20),
age NUMBER,
adress VARCHAR2(20),
sex VARCHAR2(2),
create_time VARCHAR2(20)
)
tablespace USERS
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 64K
next 1M
minextents 1
maxextents unlimited
);
-- Add comments to the table
comment on table USERS
is '用户表';
-- Add comments to the columns
comment on column USERS.name
is '姓名';
comment on column USERS.age
is '年龄';
comment on column USERS.adress
is '地址';
comment on column USERS.sex
is '性别';
comment on column USERS.create_time
is '入表时间';