天天看点

SpringBootDay02一:springboot自动装配的原理二:springboot整合定时器Quartz三:springboot整合分页插件四:使用thymeleaf模板    

一:springboot自动装配的原理

1.springboot主启动类的  @SpringBootApplication 注解是重要的注解 它会自动扫描当前主启动类所在的包以及子包。如果想要扫描其他包,需要在主启动类上手动进行声明

SpringBootDay02一:springboot自动装配的原理二:springboot整合定时器Quartz三:springboot整合分页插件四:使用thymeleaf模板    

@SpringBootApplication 注解中包含了许多其他重要的注解

SpringBootDay02一:springboot自动装配的原理二:springboot整合定时器Quartz三:springboot整合分页插件四:使用thymeleaf模板    
SpringBootDay02一:springboot自动装配的原理二:springboot整合定时器Quartz三:springboot整合分页插件四:使用thymeleaf模板    

在自动配置包注解中,重要的是 Registrar 类 这个类会自动扫描声明的需要扫描的包。

SpringBootDay02一:springboot自动装配的原理二:springboot整合定时器Quartz三:springboot整合分页插件四:使用thymeleaf模板    
SpringBootDay02一:springboot自动装配的原理二:springboot整合定时器Quartz三:springboot整合分页插件四:使用thymeleaf模板    

2.springboot的自动装配主类

实现springboot自动装配的类在 @SpringBootApplication 注解中的 @EnableAutoConfiguration注解中的 AutoConfigurationImportSelector 类中 的 getAutoConfigurationEntry 方法中。

当springboot项目启动的时候,会根据pom.xml中导入的依赖来进行自动加载,先会加载所有的类,然后会排除一部分。

SpringBootDay02一:springboot自动装配的原理二:springboot整合定时器Quartz三:springboot整合分页插件四:使用thymeleaf模板    
SpringBootDay02一:springboot自动装配的原理二:springboot整合定时器Quartz三:springboot整合分页插件四:使用thymeleaf模板    

在spring-boot-autoconfigure中的spring.factories中会声明在springboot项目启动的时候需要自动加载的所有类

SpringBootDay02一:springboot自动装配的原理二:springboot整合定时器Quartz三:springboot整合分页插件四:使用thymeleaf模板    

二:springboot整合定时器Quartz

定时任务,指当经过某一时刻后或者到达某一个时间的时候执行的任务。

1.现在都引入依赖

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

2.创建一个任务类,在任务类的上方加入@Component 注解 将此类交给spring容器管理,

在需要定时执行的方法上加入  @Scheduled(cron = "0/5 * * * * ?")注解 指定当前方法执行的时机。

package com.hyn.springboot03.task;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 定时任务类
 */
@Component
public class MyTask {

    // 定时任务注解,多长时间执行一次方法
    @Scheduled(cron = "0/5 * * * * ?")
    private void task01() {
        System.out.println("这是第一个定时任务,每隔五秒执行一次");
    }
}
           

3. 在主配置类上加入 @EnableScheduling 注解 开启定时任务,当springboot项目运行的时候,就会将创建的定时任务启动

// 启动定时任务类的注解
@EnableScheduling
           

三:springboot整合分页插件

1.引入分页插件的依赖

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.3.0</version>
</dependency>
           

2.在service层中,在执行dao接口的方法前执行分页的操作

package com.hyn.springboot03.service;

import com.github.pagehelper.PageHelper;
import com.hyn.springboot03.dao.UserDao;
import com.hyn.springboot03.entity.Page;
import com.hyn.springboot03.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public List<User> doQueryAll(Page page) {

        PageHelper.startPage(page.getPageNo(),page.getPageSize());
        return userDao.doQueryAll();
    }
}
           

这样返回的结果集就是已经进行分页后的结果,pagehelper执行的是物理分页,会在sql语句中自动的拼接上limit关键词。

四:使用thymeleaf模板

springboot对jsp并没有进行支持,但是加入了对于thymeleaf模板引擎的支持,我们使用springboot创建项目的时候,可以直接创建html文件即可。并且要将其放入在templates包下。

SpringBootDay02一:springboot自动装配的原理二:springboot整合定时器Quartz三:springboot整合分页插件四:使用thymeleaf模板    

1.引入依赖

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

2.在templates包下创建html文件,templates包相当于之前web项目的WEB-INF文件夹 无法通过浏览器地址直接进行访问,只能通过controller层进行跳转。

想要使用 thymeleaf模板引擎的标签时,需要在html页面中修改<html>标签 

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>Hello Thymeleaf</h1>
</body>
</html>
           

3.使用thymeleaf模板引擎展示从数据空查询的数据

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

    <table>
        <tr>
            <th>id</th>
            <th>姓名</th>
            <th>密码</th>
        </tr>
        <tr th:each="user: ${userList}">
            <td th:text="${user.id}"></td>
            <td th:text="${user.name}"></td>
            <td th:text="${user.password}"></td>
        </tr>
    </table>
</body>
</html>
           

注意:如果在templates包下想要再创建一个子包用来存放html页面时,必须加上thymeleaf解析器

thymeleaf默认会有解析器的前缀  /templates/ 和后置 .html ,所以当html页面直接存放在templates包下的时候 controller层可以直接 使用  return "页面名" 进行访问

aaa