SpringBoot整合Thymeleaf模板基本分页查询
导入mybatisplus,thymeleaf,druid依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.21</version>
</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
在启动配置类,配置分页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<Dept> deptPage = deptService.page(new Page<>(1, 3));
long total = deptPage.getTotal();
System.out.println(total);
List<Dept> list = deptPage.getRecords();
model.addAttribute("total",total);
model.addAttribute("list",list);
return "thymeleaf02";
}
@GetMapping
public String hello(){
return "thymeleaf01";
}
}
对应html 页面,在templates根目录下
<!DOCTYPE html>
<html lang="zh_CH">
<head>
<meta charset="UTF-8">
<title>分页查询</title>
</head>
<body>
<div><a href="/dept/page">Dept 分页查询</a></div>
</body>
</html>
<!DOCTYPE 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>