定时任务\swagger2\thymeleaf
一、定时任务
1、为什么使用定时任务
从业务场景看,当我们在淘宝买东西时,付完款,会出现一段时间如果没有付款,就会自动取消订单,这里肯定不能让人工操作,这时候就用到了定时任务,让定时任务来执行该业务。
2、定时有关注解
@EnableScheduling:在容器中注册定时有关的类
@Scheduled(cron="0/1 * * * * ?"):设置定时任务的开启时间,此例中时每秒一次
3、需要的依赖包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
4、时间转换的网站
https://cron.qqe2.com/
5、示例
1、在启动类上开启定时任务
@SpringBootApplication
@MapperScan(value = "com.fy.demo.dao")
@EnableScheduling
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
2、定时任务类
@Component
public class DemoTest {
//要在方法上加入@Scheduled注解
@Scheduled(cron = "0/1 * * * * ? ")
public void souts(){
System.out.println("//这是定时任务///");
}
}
二、Swagger2(api文档生成器)
1、作用
在前后端完全分离的开发中,前端业务任务人员需要知道后台接口的详细信息进行测
试,这就需要将接口的信息编辑成一个完成的文档,如果全由人工来写,累死。那就有
了文档生成器。
2、需要的依赖
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>1.9.1.RELEASE</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.6</version>
</dependency>
3、常用注释
@EnableSwagger2:在容器中注册有关的类
@Api:用在请求的类上,表示对类的说明
tags="说明该类的作用,可以在UI界面上看到的注解"
value="该参数没什么意义,在UI界面上也看到,所以不需要配置"
@ApiOperation:用在请求的方法上,说明方法的用途、作用
value="说明方法的用途、作用"
notes="方法的备注说明"
@ApiImplicitParams:用在请求的方法上,表示一组参数说明
@ApiImplicitParam:用在@ApiImplicitParams注解中,指定一个请求参数的各个方面
name:参数名
value:参数的汉字说明、解释
required:参数是否必须传
paramType:参数放在哪个地方
· header --> 请求参数的获取:@RequestHeader
· query --> 请求参数的获取:@RequestParam
· path(用于restful接口)--> 请求参数的获取:@PathVariable
· body(不常用)
· form(不常用)
dataType:参数类型,默认String,其它值dataType="Integer"
defaultValue:参数的默认值
@ApiResponses:用在请求的方法上,表示一组响应
@ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息
code:数字,例如400
message:信息,例如"请求参数没填好"
response:抛出异常的类
@ApiModel:用于调用类上,就是实体类,表示一个返回响应数据的信息
(这种一般用在post创建的时候,使用@RequestBody这样的场景,
请求参数无法使用@ApiImplicitParam注解进行描述的时候)
@ApiModelProperty:用在属性上,描述实体类的属性
注解解释部分摘自https://blog.csdn.net/jiangyu1013/article/details/83107255
4、示例
1、启动类
@SpringBootApplication
@MapperScan(value = "com.fy.demo.dao")
@EnableSwagger2
@EnableScheduling
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
2、Swagger2的配置类
随便建个包,包里建个类
@Configuration
public class SwaggerConfig {
@Bean
public Docket getDocket(){
Docket docket=new Docket(DocumentationType.SWAGGER_2)
.groupName("fy")
.apiInfo(apiInfo())
// Docket的select()方法会提供给swagger-springmvc framework的一个默认构造器(ApiSelectorBuilder),这个构造器为配置swagger提供了一系列的默认属性和便利方法。
.select()
// 包选择器
.apis(RequestHandlerSelectors.basePackage("com.fy.demo.Controller"))
// 设置那些请求路径生成接口文档
.paths(PathSelectors.any())
.build()
;
return docket;
}
public ApiInfo apiInfo(){
// 默认联系人
Contact DEFAULT_CONTACT = new Contact("冯赟", "https://blog.csdn.net/weixin_46122805?spm=1011.2124.3001.5343", "2193316784@qq.com");
// 设置接口文档信息
ApiInfo apiInfo=new ApiInfo("标题", "文档描述", "1.0", "后台服务ip:port", DEFAULT_CONTACT, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList());
;
return apiInfo;
}
}
3、要生成api文档的类
@RestController
@Api(tags = "员工信息操作接口")
public class EmpController {
@Resource
private EmpService empServiceimpl;
@ApiImplicitParams({
@ApiImplicitParam(name="page",dataType = "int",required =true,value = "当前页数"),
@ApiImplicitParam(name="pagesize",dataType = "int",required =true,value = "每页条数")
})
@GetMapping("/getInfo/{page}/{pagesize}")
public CommonResult getInfo(@PathVariable("page") int page,@PathVariable("pagesize") int pagesize){
PageInfo info = empServiceimpl.getInfo(page, pagesize);
CommonResult commonResult = new CommonResult(2000, "成功", info);
return commonResult;
}
}
注:==方法的前边尽量不要使用@RequestMapping注解,使用该注解会将post\put\delete\get等注解的信息全部生成一遍,尽量使用功能对应的注解==
4、需要在文档上说明的实体类,下边的注解设置会在实体类作为参数的地方,详细的解释实体类中的属性
@ApiModel(value = "员工信息类")
public class Emp {
@ApiModelProperty(value = "员工id")
private Integer id;
@ApiModelProperty(value = "员工名字")
private String name;
@ApiModelProperty(value = "员工职级编号")
private Integer salgradeId;
@ApiModelProperty(value = "职级表")
private Salgrade salgrade;
public Emp() {
}
5、浏览器访问查看文档,两个路径
①ip:port/swagger-ui.html
②ip:port/doc.html
三、thymeleaf模板引擎
1、什么是thymeleaf
该引擎是一款前端页面引擎,用在html文件中
2、为什么使用thymeleaf
我们如果使用jsp,jsp的运行过程是jsp--->servlet--->class--->浏览器渲染,太慢了
3、导入依赖包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
4、在html页面中引入标签库
<html xmlns:th="http://www.thymeleaf.org">
5、示例
1、html页面 index.xml
首先在页面中导入标签库:
<html xmlns:th="http://www.thymeleaf.org">
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table border="1px" width="300px">
<tr th:each="emp : ${emps}">
<td th:text="${emp.name}"></td>
<td th:text="${emp.salgradeId==5?'五级':'特级'}"></td>
</tr>
</table>
</body>
</html>
2、后端的Controller层
@Controller
@RequestMapping("/emp2")
public class EmpController2 {
@Autowired
private EmpService empServiceimpl;
@GetMapping("/Info")
public String getInfo(Model model){
List list = empServiceimpl.getInfo(1, 3).getList();
System.out.println(list);
model.addAttribute("emps",list);
//走springboot的默认视图解析器
return "index";
}
}