springboot整合02

本文介绍了如何在SpringBoot项目中整合定时任务库Quartz、API文档工具Swagger以及模板引擎Thymeleaf。通过引入相关依赖、配置和编写测试代码,实现了定时任务调度、API文档的生成以及Thymeleaf模板的简单应用。
摘要由CSDN通过智能技术生成

整合定时任务

1.引入依赖`

<!--定时任务的依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-quartz</artifactId>
        </dependency>

简单测试

@Component
public class OrderQuartz {

    @Scheduled(cron = "0/2 * * * * ? ")  //定时器  这里设置两秒执行一次
    public void orderQuartz(){
        System.out.println("~~~~~~~~~~");
    }
}

2.在主启动类上加注解来开启定时驱动@EnableScheduling//开启Quarz注解的驱动
结果图

springboot整合swagger

1.引入依赖

 <!--swagger-->
        <dependency>
            <groupId>com.spring4all</groupId>
            <artifactId>swagger-spring-boot-starter</artifactId>
            <version>1.9.1.RELEASE</version>
        </dependency>
        <!--好看的ui-->
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>swagger-bootstrap-ui</artifactId>
            <version>1.9.6</version>
        </dependency>

2.创建一个swagger配置类

@Configuration //配置类
public class SwaggerConfig {

    @Bean 
    public Docket getDocket(){
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("Toyota")
                .select()
                //设置哪些包下的类生产api接口文档
                .apis(RequestHandlerSelectors.basePackage("com.ganin.controller"))
                //设置哪些请求路径生产接口文档(任意一个)
                .paths(PathSelectors.any())
                .build()
                ;
        return docket;
    }
    private ApiInfo apiInfo(){
        Contact contact = new Contact("豆加宁", "www.xxx.com", "xxx@xx.com");
        ApiInfo apiInfo = new ApiInfo("Api Documentation", "Api Documentation", "1.0", "urn:tos", contact, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList());
        return apiInfo;
    }
}

3.在主启动类上加@EnableSwagger2//开启swagger2注解开启swagger2的注解
在这里插入图片描述4.访问 swagger-bootstrap-ui 的页面 http://localhost:8080/doc.html 结果图
在这里插入图片描述

thymeleaf 模板引擎

1.在pom.xml引入依赖

 <!--thymeleaf模板引擎-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

2.创建一个hello.html页面(简单的例子)

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"> <!--把原来的html标签内容换掉-->
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <table th:width="1500" border="1px">
        <tr>
            <th>编号</th>
            <th>姓名</th>
            <th>性别</th>
            <th>邮箱</th>
            <th>部门</th>
            <th>创建时间</th>
        </tr>
        <tr align="center" th:each="list:${list}">
            <td th:text="${list.getEmpId()}"></td>
            <td th:text="${list.getEmpName()}"></td>
            <td th:text="${list.getGender()=='M'?'':''}"></td>
            <td th:text="${list.getEmail()}"></td>
            <td th:text="${list.getEmpId()==1?'开发部':'测试部'}"></td>
            <td th:text="${#dates.format(list.getCreatetime(),'yyyy-MM-dd HH:mm:ss')}"></td>
        </tr>
    </table>
</body>
</html>

controller层代码实现

@Controller
public class HelloController {
    @Autowired
    private EmpService empService;

    @GetMapping("/list")
    @ApiOperation(value = "getAll 查询所有数据")
    public String getAll(Model model){
        List<Emp> list = empService.selcetAll();
        model.addAttribute("list",list);
        return "hello";
    }
}

结果图:
在这里插入图片描述谢谢!!!!!!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值