SpringBoot整合Thymeleaf模板基本分页查询

SpringBoot整合Thymeleaf模板基本分页查询

导入mybatisplus,thymeleaf,druid依赖

  
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-thymeleafartifactId>
        dependency>
        
        <dependency>
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plus-boot-starterartifactId>
            <version>3.3.1version>
        dependency>
        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druid-spring-boot-starterartifactId>
            <version>1.1.21version>
        dependency>

修改端口号,配置视图解析器,配置数据源,yml文件

# 修改端口号
server:
  port: 80
# 数据源
spring:
  datasource:
    druid:
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql:///ddd?serverTimezone=GMT%2B8
      username: root
      password: root
      max-active: 50
      min-idle: 5
      initial-size: 5
# 视图解析器
  mvc:
    view:
      prefix: /static
      suffix: .html
# thymeleaf 在这里可不用配置,使用默认的就可以了

在启动配置类,配置分页Bean 对象

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class DemoApplication {

    // 分页配置
    @Bean
    public PaginationInterceptor paginationInterceptor(){
        return new PaginationInterceptor();
    }

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

创建项目架构,entity,mapper,service,controller

--------------------------------------------

entity

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;

import java.util.Date;
//实体类
@Data
@TableName("tbl_dept")
public class Dept {
    // 主键字段策略
    @TableId(value = "dept_no", type = IdType.AUTO)
    private Integer deptNo;
    private String dname;
    private String loc;

    // 普通字段策略
    @TableField("create_time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;
}

mapper

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.neuedu.entity.Dept;
@Mapper
public interface DeptMapper extends BaseMapper<Dept> {
}

service

import com.baomidou.mybatisplus.extension.service.IService;
import org.neuedu.entity.Dept;

public interface DeptService extends IService<Dept> {
}

service.impl

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.neuedu.entity.Dept;
import org.neuedu.mapper.DeptMapper;
import org.neuedu.service.DeptService;
import org.springframework.stereotype.Service;

@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper,Dept> implements DeptService {
}

controller 分页逻辑


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.neuedu.entity.Dept;
import org.neuedu.service.DeptService;
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 org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/dept")
public class ThymeleafController {

    @Autowired
    private DeptService deptService;

    @GetMapping("/page")
    public String page(Model model){
        // 获取 Page 对象
        Page<Dept> deptPage = deptService.page(new Page<>(1, 3));

        // 总条数
        long total = deptPage.getTotal();
        System.out.println(total);
        // 查询结果
        List<Dept> list = deptPage.getRecords();
        // 存入 Model 作用域
        model.addAttribute("total",total);
        model.addAttribute("list",list);
        //跳转页面
        return "thymeleaf02";
    }

    @GetMapping
    public String hello(){
        return "thymeleaf01";
    }
}

对应html 页面,在templates根目录下


<html lang="zh_CH">
<head>
    <meta charset="UTF-8">
    <title>分页查询title>
head>
<body>
    <div><a href="/dept/page">Dept分页查询a>div>
body>
html>


<html lang="zh_CH" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>分页查询title>
head>
<body>
    <h2 th:text="${total}">h2>
    <ul th:each="dept : ${list}">
        <li th:text="${dept}">li>
    ul>
body>
html>

你可能感兴趣的:(SpringBoot整合Thymeleaf模板基本分页查询)