Quartz定时任务和thymeleaf模板
1.springboot整合Quartz定时任务
1.1引入Quartz依赖
<!--定时任务的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
1.2.创建一个定时任务类和定义一个定时任务
在线cron表达式
cron如下图所示
定时任务类和定时任务方法
package com.demo.aaa.orderquartz;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class OrderQuartz {
@Scheduled(cron ="0/2 * * * * ? " )
public void OrderQuartz(){
System.out.println("--------最帅的毛云飞-----------");
}
}
1.3.主启动类开起定时任务
package com.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@SpringBootApplication
@MapperScan(basePackages = {"com.demo.aaa.dao"})//为dao包下的所有接口生产实现类
@EnableScheduling//开始quartz(定时任务)注解
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
2.thymeleaf模板的使用和案例
2.1引入thymeleaf依赖
<!--添加thymeleaf模板引擎-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2.2创建一个模板实体类
package com.demo.aaa.test;
import com.demo.aaa.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.List;
@Controller
public class InforemationTest {
@Autowired
private BookService bookService;
@GetMapping("/select")
public String select(Model model){
model.addAttribute("name","最帅的毛云飞");
//selectAllBook查询全部图书的方法
List list = bookService.selectAllBook(1, 10).getList();
model.addAttribute("list",list);
return "select";
}
}
2.3html编写页面
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<span style="background-color: greenyellow;mso-cellspacing:10px;margin-left: 750px;size:8px" th:text="${name}"></span>
<table border="1" th:cellpadding="5" cellspacing="0" style="margin: auto;background-color: cyan" >
<tr>
<th>书籍名字</th>
<th>书籍作者</th>
<th>库存</th>
<th>描述</th>
<th>书籍类型编号</th>
<th>出版社</th>
<th>价格</th>
<th>状态</th>
<th>图片</th>
</tr>
<tr th:each="book : ${list}">
<td th:text="${book.bookname}">书籍名字</td>
<td th:text="${book.author}">书籍作者</td>
<td th:text="${book.count}">库存</td>
<td th:text="${book.remark}">描述</td>
<td th:text="${book.typeid!=null?book.type.typename:''}">书籍类型编号</td>
<td th:text="${book.publisher}">出版社</td>
<td th:text="${book.price}">价格</td>
<td th:text="${book.status==0?'下架':'上架'}">状态</td>
<td th:text="${book.url}">图片</td>
</tr>
</table>
</body>
</html>
2.4运行界面