SpringBoot基于mybatis-plus实现定时任务管理
时间: 2023-06-02 12:02:07 浏览: 198
SpringBoot是一个非常流行的Java Web框架,而mybatis-plus是一个非常强大的ORM框架,两者合起来可以实现非常好用的定时任务管理。
下面是基于SpringBoot和mybatis-plus实现定时任务管理的步骤:
1. 添加依赖
在pom.xml文件中添加以下依赖:
```xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
</dependencies>
```
2. 配置数据库
在application.properties文件中添加以下配置:
```
# 数据库配置
spring.datasource.url=jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# mybatis-plus配置
mybatis-plus.mapper-locations=classpath:/mapper/*.xml
# Quartz配置
org.quartz.scheduler.instanceName = QuartzScheduler
org.quartz.scheduler.instanceId = AUTO
org.quartz.threadPool.threadCount=10
org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.dataSource=myDS
org.quartz.jobStore.tablePrefix=QRTZ_
org.quartz.dataSource.myDS.driver=com.mysql.jdbc.Driver
org.quartz.dataSource.myDS.URL=jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf-8&useSSL=false
org.quartz.dataSource.myDS.user=root
org.quartz.dataSource.myDS.password=123456
org.quartz.dataSource.myDS.maxConnections=5
```
3. 创建实体类和Mapper
创建一个定时任务的实体类,并在其中定义定时任务的属性:
```java
@Data
public class JobEntity {
/**
* 任务ID
*/
private String jobId;
/**
* 任务名称
*/
private String jobName;
/**
* 任务分组
*/
private String jobGroup;
/**
* 任务执行类
*/
private String jobClass;
/**
* 任务描述
*/
private String jobDescription;
/**
* 任务状态 0禁用 1启用
*/
private Integer jobStatus;
/**
* 任务表达式
*/
private String jobExpression;
/**
* 创建时间
*/
private Date createTime;
/**
* 修改时间
*/
private Date modifyTime;
}
```
创建一个定时任务的Mapper,并继承mybatis-plus的BaseMapper:
```java
@Repository
public interface JobMapper extends BaseMapper<JobEntity> {
}
```
4. 创建定时任务工具类
创建一个定时任务的工具类,用于实现定时任务的添加、删除、暂停和恢复等操作:
```java
@Service
public class QuartzService {
@Autowired
private Scheduler scheduler;
/**
* 添加任务
*
* @param jobEntity
*/
public void addJob(JobEntity jobEntity) throws SchedulerException, ClassNotFoundException {
// 获取任务类
Class<? extends Job> jobClass = (Class<? extends Job>) Class.forName(jobEntity.getJobClass());
// 创建任务
JobDetail jobDetail = JobBuilder.newJob(jobClass)
.withIdentity(jobEntity.getJobName(), jobEntity.getJobGroup())
.withDescription(jobEntity.getJobDescription())
.storeDurably(true)
.build();
// 创建触发器
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity(jobEntity.getJobName(), jobEntity.getJobGroup())
.withSchedule(CronScheduleBuilder.cronSchedule(jobEntity.getJobExpression()))
.build();
// 将任务和触发器添加到调度器
scheduler.scheduleJob(jobDetail, trigger);
}
/**
* 删除任务
*
* @param jobId
* @param jobGroup
*/
public void deleteJob(String jobId, String jobGroup) throws SchedulerException {
JobKey jobKey = new JobKey(jobId, jobGroup);
scheduler.deleteJob(jobKey);
}
/**
* 暂停任务
*
* @param jobId
* @param jobGroup
*/
public void pauseJob(String jobId, String jobGroup) throws SchedulerException {
JobKey jobKey = new JobKey(jobId, jobGroup);
scheduler.pauseJob(jobKey);
}
/**
* 恢复任务
*
* @param jobId
* @param jobGroup
*/
public void resumeJob(String jobId, String jobGroup) throws SchedulerException {
JobKey jobKey = new JobKey(jobId, jobGroup);
scheduler.resumeJob(jobKey);
}
}
```
5. 创建定时任务控制器
创建一个定时任务的控制器,用于实现定时任务的增删改查等操作:
```java
@RestController
@RequestMapping("/job")
public class JobController {
@Autowired
private JobMapper jobMapper;
@Autowired
private QuartzService quartzService;
/**
* 添加任务
*
* @param jobEntity
*/
@PostMapping("/add")
public void addJob(@RequestBody JobEntity jobEntity) throws SchedulerException {
// 添加任务
quartzService.addJob(jobEntity);
// 保存任务到数据库
jobMapper.insert(jobEntity);
}
/**
* 删除任务
*
* @param jobId
* @param jobGroup
*/
@DeleteMapping("/delete")
public void deleteJob(String jobId, String jobGroup) throws SchedulerException {
// 删除任务
quartzService.deleteJob(jobId, jobGroup);
// 从数据库中删除任务
QueryWrapper<JobEntity> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("job_id", jobId);
queryWrapper.eq("job_group", jobGroup);
jobMapper.delete(queryWrapper);
}
/**
* 暂停任务
*
* @param jobId
* @param jobGroup
*/
@PutMapping("/pause")
public void pauseJob(String jobId, String jobGroup) throws SchedulerException {
// 暂停任务
quartzService.pauseJob(jobId, jobGroup);
// 更新任务状态
JobEntity jobEntity = new JobEntity();
jobEntity.setJobId(jobId);
jobEntity.setJobGroup(jobGroup);
jobEntity.setJobStatus(0);
jobMapper.updateById(jobEntity);
}
/**
* 恢复任务
*
* @param jobId
* @param jobGroup
*/
@PutMapping("/resume")
public void resumeJob(String jobId, String jobGroup) throws SchedulerException {
// 恢复任务
quartzService.resumeJob(jobId, jobGroup);
// 更新任务状态
JobEntity jobEntity = new JobEntity();
jobEntity.setJobId(jobId);
jobEntity.setJobGroup(jobGroup);
jobEntity.setJobStatus(1);
jobMapper.updateById(jobEntity);
}
/**
* 获取任务列表
*
* @return
*/
@GetMapping("/list")
public List<JobEntity> listJob() {
return jobMapper.selectList(null);
}
}
```
至此,基于SpringBoot和mybatis-plus实现定时任务管理的步骤就完成了。我们可以通过访问控制器提供的接口来实现定时任务的添加、删除、暂停和恢复等操作。
阅读全文