目录
一、自动化处理
1.1 什么是自动化处理
1.2 SpringTask介绍
二、SpringTask的基本使用
2.1 引入依赖
2.2 通过控制台加入注解启用SpringTask
2.3 使用Cron表达式规定时间
2.4 通过@Schedule(Cron表达式) 实现定时任务(每两秒执行一次)
三、实战
3.1 创建一个交互表
3.2 引入mybatis-plus 并配置数据库
3.3 模拟访问的Controller
3.4 设置定时任务
一、自动化处理
1.1 什么是自动化处理
自动化处理是指使用软件工具或程序自动执行原本需要人工干预的任务。这些任务可以是重复性的、耗时的或者需要高度准确性的操作。通过自动化,不仅可以提高工作效率和准确性,还可以释放人力资源以专注于更高价值的工作。
1.2 SpringTask介绍

二、SpringTask的基本使用
2.1 引入依赖
由于springTask 是SpringFramWork包的内容,所以不需要进行引入新的依赖。
2.2 通过控制台加入注解启用SpringTask
@SpringBootApplication
@EnableScheduling
public class SpringTaskApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringTaskApplication.class, args);
    }
}
2.3 使用Cron表达式规定时间

如果不会使用Cron表达式的使用可以直接使用cron的生成网站
https://cron.qqe2.com/

常用cron表达式:

2.4 通过@Schedule(Cron表达式) 实现定时任务(每两秒执行一次)
@Component
@Slf4j
public class springTaskTest {
//    每两秒执行一次
    @Scheduled(cron = "0/2 * * * * ?")
    public void AutoTask(){
        log.info("自动化代码执行中");
    }
}

三、实战
要求实现一个用户与AI助手对话交互表,要求一个用户一天最多能对话200次,并且为了控制并发量,每个用户在一分钟之内最多进行对话十次。
3.1 创建一个交互表
CREATE TABLE user_request_log (
    user_id BIGINT NOT NULL,
    request_date DATE NOT NULL,
    total_requests INT DEFAULT 200,
    minute_requests INT DEFAULT 10,
    minute_start_time DATETIME,
    PRIMARY KEY (user_id, request_date),
    INDEX idx_minute_start_time (minute_start_time)
);
user_id: 用户ID,作为主键的一部分,类型为BIGINT。
request_date: 当天的日期,作为主键的一部分,类型为DATE。
total_requests: 当天的总请求次数,类型为INT,默认值为0。
minute_requests: 当前分钟的请求次数,类型为INT,默认值为0。
minute_start_time: 当前分钟开始的时间戳,类型为DATETIME。- 主键由
user_id和request_date组成,以确保每个用户每天的记录唯一。- 添加了一个索引
idx_minute_start_time以加快按minute_start_time查询的速度。
3.2 引入mybatis-plus 并配置数据库
依赖:
        <!--        数据库依赖-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.5</version>
            <exclusions>
                <exclusion>
                    <groupId>org.mybatis</groupId>
                    <artifactId>mybatis-spring</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>3.0.3</version>
        </dependency>
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>application.yml
spring:
  # 数据源配置
  datasource:
    url: jdbc:mysql://localhost:3306/ap_security?characterEncoding=utf-8&serverTimezone=UTC
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver
  application:
    name: SpringTask使用mybatis-plus快速生成实体与架构

3.3 模拟访问的Controller
@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserRequestLogMapper userRequestLogMapper;
//    模拟进行对话
    @GetMapping("/chat")
    public String Chat(){
        UserRequestLog userRequestLog = userRequestLogMapper.selectById(1);
//        当有次数时候才能进行对话
        if (userRequestLog.getTotalRequests()>0 && userRequestLog.getMinuteRequests()>0){
//            减去数量
            userRequestLog.setMinuteRequests(userRequestLog.getMinuteRequests()-1);
            userRequestLog.setTotalRequests(userRequestLog.getTotalRequests()-1);
            userRequestLogMapper.updateById(userRequestLog);
            return "对话成功";
        }else {
            return "您暂时已经没有对话次数了";
        }
    }
}
3.4 设置定时任务
@Component
@Slf4j
public class springTaskTest {
    @Autowired
    UserRequestLogMapper userRequestLogMapper;
    //    每一分钟执行一次
    @Scheduled(cron = "0 0/1 * * * ?")
    public void AutoTask(){
        log.info("执行增加分钟对话次数");
        UserRequestLog userRequestLog = userRequestLogMapper.selectById(1);
        userRequestLog.setMinuteRequests(20);
        userRequestLogMapper.updateById(userRequestLog);
    }
    //    每天凌晨3点执行一次
    @Scheduled(cron = "0 0 3 * * ?")
    public  void DayAuto(){
        log.info("执行增加天数的总次数");
        UserRequestLog userRequestLog = userRequestLogMapper.selectById(1);
        userRequestLog.setTotalRequests(200);
        userRequestLogMapper.updateById(userRequestLog);
    }
}测试:


进行增加分钟次数:






![[C++]多态与虚函数](https://i-blog.csdnimg.cn/direct/a17bada2b7934967944d6f8985a5545c.png)














