Springboot整合JdbcTemplate实现分页查询「建议收藏」

Springboot整合JdbcTemplate实现分页查询「建议收藏」在做SpringBoot后端项目时,我想采用后端分页的模式,后端分页是在后端先把数据处理好,再发给前端,前端只需要访问对应的页面拿相应页的数据即可。后端分页的写法中MyBatis和JPA都有现成的后端分页组件,而JdbcTemplate却没有。因此这里以实体类User为例把自己的学习过程记录下来。…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

Springboot整合JdbcTemplate实现分页查询

一、前言

在做SpringBoot后端项目时,我想采用后端分页的模式,后端分页是在后端先把数据处理好,再发给前端,前端只需要访问对应的页面拿相应页的数据即可。后端分页的写法中MyBatis和JPA都有现成的后端分页组件,而JdbcTemplate却没有。因此这里以实体类User为例把自己的学习过程记录下来。

二、开发工具及环境

  • 电脑操作系统:Win10

  • Java版本:JDK1.8

  • MySQL数据库版本:mysql-8.0.26-winxx64

  • 编辑器:IntelliJ IDEA 2021.2 企业版

  • SpringBoot版本:2.7.2

  • 工作目录

    在这里插入图片描述

三、SpringBoot基本配置

1、Spring initializr 设置

在这里插入图片描述

2、pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>pagingQuery</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>pagingQuery</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>

3、application.properties

spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/jdbcTemplate?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# thymeleaf配置
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
  • 这里我用的数据库名为jdbcTemplate
  • MySQL 8.xx的版本需要设时区serverTimezone

四、准备工作-数据库

1、创建数据库及User表

# 创建数据库
CREATE DATABASE `jdbcTemplate`CHARACTER SET utf8 COLLATE utf8_general_ci; 
# 创建user表
CREATE TABLE `jdbctemplate`.`user`( `id` INT(4) UNSIGNED NOT NULL, `username` VARCHAR(30) NOT NULL, `password` VARCHAR(30) NOT NULL, `email` VARCHAR(30), `gender` INT(1), PRIMARY KEY (`id`) ) ENGINE=INNODB CHARSET=utf8 COLLATE=utf8_general_ci; 
#向user表中插入数据
INSERT INTO `jdbctemplate`.`user` (`id`, `username`, `password`, `email`, `gender`, `birth`) VALUES ('1', 'AAA', 'pwd01', 'test01@qq.com', '1'); 
INSERT INTO `jdbctemplate`.`user` (`id`, `username`, `password`, `email`, `gender`, `birth`) VALUES ('2', 'BBB', 'pwd02', 'test02@qq.com', '2'); 
INSERT INTO `jdbctemplate`.`user` (`id`, `username`, `password`, `email`, `gender`, `birth`) VALUES ('3', 'CCC', 'pwd03', 'test03@qq.com', '3'); 
INSERT INTO `jdbctemplate`.`user` (`id`, `username`, `password`, `email`, `gender`) VALUES ('4', 'DDD', 'pwd04', 'test04@qq.com', '0'); 
UPDATE `jdbctemplate`.`user` SET `gender` = '0' WHERE `id` = '2';
UPDATE `jdbctemplate`.`user` SET `gender` = '1' WHERE `id` = '3';
UPDATE `jdbctemplate`.`user` SET `birth` = '1994-07-16' WHERE `id` = '4';
INSERT INTO `jdbctemplate`.`user` (`id`, `username`, `password`, `email`, `gender`, `birth`) VALUES ('5', 'EEE', 'pwd05', 'test05@qq.com', '0'); 
INSERT INTO `jdbctemplate`.`user` (`id`, `username`, `password`, `email`, `gender`, `birth`) VALUES ('6', 'FFF', 'pwd06', 'test06@qq.com', '1'); 
INSERT INTO `jdbctemplate`.`user` (`id`, `username`, `password`, `email`, `gender`, `birth`) VALUES ('7', 'GGG', 'pwd07', 'test07@qq.com', '1'); 
INSERT INTO `jdbctemplate`.`user` (`id`, `username`, `password`, `email`, `gender`, `birth`) VALUES ('8', 'HHH', 'pwd08', 'test08@qq.com', '0'); 
INSERT INTO `jdbctemplate`.`user` (`id`, `username`, `password`, `email`, `gender`, `birth`) VALUES ('9', 'III', 'pwd09', 'test09@qq.com', '1'); 
INSERT INTO `jdbctemplate`.`user` (`id`, `username`, `password`, `email`, `gender`, `birth`) VALUES ('10', 'JJJ', 'pwd10', 'test10@qq.com', '0'); 
INSERT INTO `jdbctemplate`.`user` (`id`, `username`, `password`, `email`, `gender`, `birth`) VALUES ('11', 'KKK', 'pwd11', 'test11@qq.com', '0'); 
INSERT INTO `jdbctemplate`.`user` (`id`, `username`, `password`, `email`, `gender`, `birth`) VALUES ('12', 'LLL', 'pwd12', 'test12@qq.com', '1'); 
INSERT INTO `jdbctemplate`.`user` (`id`, `username`, `password`, `email`, `gender`, `birth`) VALUES ('13', 'MMM', 'pwd13', 'test13@qq.com', '0'); 
INSERT INTO `jdbctemplate`.`user` (`id`, `username`, `password`, `email`, `gender`, `birth`) VALUES ('14', 'NNN', 'pwd14', 'test14@qq.com', '1'); 
INSERT INTO `jdbctemplate`.`user` (`id`, `username`, `password`, `gender`, `birth`) VALUES ('15', 'OOO', 'pwd15', '0'); 
INSERT INTO `jdbctemplate`.`user` (`id`, `username`, `password`, `email`) VALUES ('16', 'PPP', 'pwd16', 'test16@qq.com'); 
INSERT INTO `jdbctemplate`.`user` (`id`, `username`, `password`, `email`, `gender`) VALUES ('17', 'QQQ', 'pwd17', 'test17@qq.com', '1'); 
INSERT INTO `jdbctemplate`.`user` (`id`, `username`, `password`, `email`, `gender`, `birth`) VALUES ('18', 'RRR', 'pwd18', 'test18@qq.com', '0'); 
INSERT INTO `jdbctemplate`.`user` (`id`, `username`, `password`, `email`, `birth`) VALUES ('19', 'SSS', 'pwd19', 'test19@qq.com'); 
INSERT INTO `jdbctemplate`.`user` (`id`, `username`, `password`, `email`, `gender`, `birth`) VALUES ('20', 'TTT', 'pwd20', 'test20@qq.com', '1'); 
INSERT INTO `jdbctemplate`.`user` (`id`, `username`, `password`, `email`, `gender`, `birth`) VALUES ('21', 'UUU', 'pwd21', 'test21@qq.com', '0'); 
INSERT INTO `jdbctemplate`.`user` (`id`, `username`, `password`, `email`, `gender`) VALUES ('22', 'VVV', 'pwd22', 'test22@qq.com', '1'); 
INSERT INTO `jdbctemplate`.`user` (`id`, `username`, `password`, `email`) VALUES ('23', 'WWW', 'pwd23', 'test23@qq.com'); 
INSERT INTO `jdbctemplate`.`user` (`id`, `username`, `password`, `gender`) VALUES ('24', 'XXX', 'pwd24', '0'); 
INSERT INTO `jdbctemplate`.`user` (`id`, `username`, `password`, `email`, `gender`, `birth`) VALUES ('25', 'YYY', 'pwd25', 'test25@qq.com', '1');
INSERT INTO `jdbctemplate`.`user` (`id`, `username`, `password`, `email`) VALUES ('26', 'ZZZ', 'pwd26', 'test26@qq.com');
UPDATE `jdbctemplate`.`user` SET `gender` = '0' WHERE `id` = '16';
UPDATE `jdbctemplate`.`user` SET `gender` = '1' WHERE `id` = '19'; 
UPDATE `jdbctemplate`.`user` SET `gender` = '0' WHERE `id` = '23'; 
UPDATE `jdbctemplate`.`user` SET `gender` = '1' WHERE `id` = '26';

创建后User表如下图所示:

在这里插入图片描述

2、测试是否能连接上数据库

package com.example.pagingQuery;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
@SpringBootTest
class PagingQueryApplicationTests { 

//DI注入数据源
@Autowired
DataSource dataSource;
@Test
void contextLoads() throws SQLException { 

//看一下默认数据源
System.out.println(dataSource.getClass());
//获得连接
Connection connection = dataSource.getConnection();
System.out.println(connection);
//关闭连接
connection.close();
}
}

运行测试是否运行成功,如果成功,则说明成功连接数据库。

五、架构准备

1、User类

package com.example.pagingQuery.model;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User { 

private Integer id;
private String username;
private String password;
private String email;
private Integer gender;//0:女性 1:男性
}

2、UserDao

package com.example.pagingQuery.dao;
import com.example.pagingQuery.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.GetMapping;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Repository
public class UserDao { 

@Autowired
JdbcTemplate jdbcTemplate;
//获取所有的user信息
public List<User> getUserList() throws ParseException { 

String sql = "select * from `user`";
List<Map<String, Object>> userList = jdbcTemplate.queryForList(sql);
ArrayList<User> users = new ArrayList<>();
for (int i = 0; i < userList.size(); i++) { 

int id = ((Number) userList.get(i).get("id")).intValue();
String username = (String) userList.get(i).get("username");
String password = (String) userList.get(i).get("password");
String email = (String) userList.get(i).get("email");
int gender = ((Number) userList.get(i).get("gender")).intValue();
User user = new User(id, username, password, email, gender);
users.add(user);
}
return users;
}
}

3、视图控制器MyMvcConfig

package com.example.pagingQuery.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MyMvcConfig implements WebMvcConfigurer { 

//添加视图控制器
@Override
public void addViewControllers(ViewControllerRegistry registry) { 

registry.addViewController("/index").setViewName("userList");
}
}

4、静态资源

静态资源使用Bootstrap v3,获取静态资源链接(永久有效):

链接:https://pan.baidu.com/s/1oq17a4uodSNYWwYNMfjIVw
提取码:Msql

5、userlist.html 静态页面

注意:下面给的是静态页面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!-- 引入Bootstrap v3 静态资源-->
<link rel="stylesheet" href="../static/bootstrap-3.4.1-dist/css/bootstrap.min.css" th:href="@{/bootstrap-3.4.1-dist/css/bootstrap.min.css}">
<link rel="stylesheet" href="../static/bootstrap-3.4.1-dist/css/bootstrap.min.css.map" th:href="@{/bootstrap-3.4.1-dist/css/bootstrap.min.css.map}">
<style> .table-wrapper{ 
 min-height: 300px; } .paging{ 
 width: 100%; height: 150px; position: relative; } .paging nav{ 
 display: block; margin: auto; position: absolute; left: 600px; top: 30px; } </style>
</head>
<body>
<div class="panel panel-default">
<!-- Default panel contents -->
<div class="panel-heading">Panel heading</div>
<div class="panel-body">
<p>《甄嬛传》永远滴神</p>
</div>
<!-- Table -->
<div class="table-wrapper">
<table class="table table-striped">
<thead>
<tr>
<th>序号</th>
<th>用户名</th>
<th>密码</th>
<th>邮箱</th>
<th>性别</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>AAA</td>
<td>123456</td>
<td>test01@qq.com</td>
<td></td>
</tr>
<tr>
<td>2</td>
<td>BBB</td>
<td>123456</td>
<td>test02@qq.com</td>
<td></td>
</tr>
<tr>
<td>3</td>
<td>CCC</td>
<td>123456</td>
<td>test03@qq.com</td>
<td></td>
</tr>
<tr>
<td>4</td>
<td>DDD</td>
<td>123456</td>
<td>test04@qq.com</td>
<td></td>
</tr>
<tr>
<td>5</td>
<td>EEE</td>
<td>123456</td>
<td>test05@qq.com</td>
<td></td>
</tr>
<tr>
<td>6</td>
<td>FFF</td>
<td>123456</td>
<td>test06@qq.com</td>
<td></td>
</tr>
<tr>
<td>7</td>
<td>GGG</td>
<td>123456</td>
<td>test07@qq.com</td>
<td></td>
</tr>
</tbody>
</table>
</div>
<!-- 分页 -->
<div class="paging">
<nav aria-label="Page navigation">
<ul class="pagination pagination-lg">
<li>
<a href="#" aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
</a>
</li>
<li><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">4</a></li>
<li><a href="#">5</a></li>
<li>
<a href="#" aria-label="Next">
<span aria-hidden="true">&raquo;</span>
</a>
</li>
</ul>
</nav>
</div>
</div>
</body>
</html>

静态页面效果如下图所示:

在这里插入图片描述

6、UserController类

package com.example.pagingQuery.controller;
import com.example.pagingQuery.dao.UserDao;
import com.example.pagingQuery.pojo.User;
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 java.text.ParseException;
import java.util.List;
@Controller
public class UserController { 

@Autowired
UserDao userDao;
@GetMapping("/index")
public String showList(Model model){ 

List<User> users = userDao.getUserList();
model.addAttribute("users",users);
return "userList";
}
}

7、userlist.html 动态页面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!-- 引入Bootstrap v3 静态资源-->
<link rel="stylesheet" href="../static/bootstrap-3.4.1-dist/css/bootstrap.min.css" th:href="@{/bootstrap-3.4.1-dist/css/bootstrap.min.css}">
<link rel="stylesheet" href="../static/bootstrap-3.4.1-dist/css/bootstrap.min.css.map" th:href="@{/bootstrap-3.4.1-dist/css/bootstrap.min.css.map}">
<style> .table-wrapper{ 
 min-height: 300px; } .paging{ 
 width: 100%; height: 150px; position: relative; } .paging nav{ 
 display: block; margin: auto; position: absolute; left: 600px; top: 30px; } </style>
</head>
<body>
<div class="panel panel-default">
<!-- Default panel contents -->
<div class="panel-heading">Panel heading</div>
<div class="panel-body">
<p>《甄嬛传》永远滴神</p>
</div>
<!-- Table -->
<div class="table-wrapper">
<table class="table table-striped">
<thead>
<tr>
<th>序号</th>
<th>用户名</th>
<th>密码</th>
<th>邮箱</th>
<th>性别</th>
</tr>
</thead>
<tbody>
<tr th:each="user:${users}">
<td th:text="${user.getId()}"></td>
<td th:text="${user.getUsername()}"></td>
<td th:text="${user.getPassword()}"></td>
<td th:text="${user.getEmail()}"></td>
<td th:text="${user.getGender()==0?'':''}"></td>
</tr>
</tbody>
</table>
</div>
<!-- 分页 -->
<div class="paging">
<nav aria-label="Page navigation">
<ul class="pagination pagination-lg">
<li>
<a href="#" aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
</a>
</li>
<li><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">4</a></li>
<li><a href="#">5</a></li>
<li>
<a href="#" aria-label="Next">
<span aria-hidden="true">&raquo;</span>
</a>
</li>
</ul>
</nav>
</div>
</div>
</body>
</html>

运行程序,得到动态页面效果如下图所示:

在这里插入图片描述

这个时候准备工作算是完成,下面开始进行分页工作

六、分页功能实现研究

1、创建PageList类

首先创建PageList类,代表每一页

package com.example.pagingQuery.model;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.ArrayList;
import java.util.List;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PageList<T> { 

private int pageSize;   //单页最大数据量
private int dataNumber; //Java类T 总的数据量
private int pageNumber; //总的页数 总的页数=(总的数据量%单页最大数据量)==0?(总的数据量/单页最大数据量):((总的数据量/单页最大数据量)+1)
private int currentPage; //当前页
private List<T> dataList = new ArrayList<T>(); //当前页的全部数据
public PageList(int currentPage,int pageSize,int dataNumber){ 

this.currentPage = currentPage;
this.pageSize = pageSize;
pageNumber = (dataNumber%pageSize==0?(dataNumber/pageSize):(dataNumber/pageSize+1));
}
}
  • currentPage:当前页,指的实际上是用户点击某一页时我们要展现出的目标页
  • dataNumber和pageSize主要的用处就是计算出总的页数
  • dataList用于存储我们要展现给用户的数据列表

2、UserDao分页方法

在UserDao里面添加一个分页的方法,根据前端页面传来的单页页面数据大小和当前页返回数据列表

//分页功能实现,获取分页数据
//前端页面点击分页器时调用此函数
public PageList<User> getUserListByPage(int currentPage,int pageSize){ 

//获取总数据量
List<User> userList = getUserList();
int dataNumber = userList.size();
//设置当前页面和每个页面的最大数据量
//这里我设置每个页面的最大数据量为7
PageList<User> userPageList = new PageList<>(currentPage, pageSize,dataNumber);
//获取所有的user数据,易知总的数据量为26
userPageList.setDataNumber(jdbcTemplate.queryForObject("SELECT count(id) FROM `user`",Integer.class));
//根据当前页的情况来确定当前页的展示数据列表
if (userPageList.getCurrentPage()==userPageList.getPageNumber()){ 

//当前页为总页面的最后一页
userPageList.setDataList(jdbcTemplate.query("SELECT * FROM `user` limit ?,?",new BeanPropertyRowMapper<>(User.class),
(currentPage-1)*pageSize,userPageList.getDataNumber()-(currentPage-1)*pageSize));
}else { 

userPageList.setDataList(jdbcTemplate.query("SELECT * FROM `user` limit ?,?",new BeanPropertyRowMapper<>(User.class),
(currentPage-1)*pageSize,pageSize));
}
return userPageList;
}

3、userlist.html页面修改

主要是对分页器那边进行修改,使用JS进行页面处理

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!-- 引入Bootstrap v3 静态资源-->
<link rel="stylesheet" href="../static/bootstrap-3.4.1-dist/css/bootstrap.min.css" th:href="@{/bootstrap-3.4.1-dist/css/bootstrap.min.css}">
<link rel="stylesheet" href="../static/bootstrap-3.4.1-dist/css/bootstrap.min.css.map" th:href="@{/bootstrap-3.4.1-dist/css/bootstrap.min.css.map}">
<style> .table-wrapper{ 
 min-height: 300px; } .paging{ 
 width: 100%; height: 150px; position: relative; } .paging nav{ 
 display: block; margin: auto; position: absolute; left: 600px; top: 30px; } </style>
</head>
<body>
<div class="panel panel-default">
<!-- Default panel contents -->
<div class="panel-heading">Panel heading</div>
<div class="panel-body">
<p>《甄嬛传》永远滴神</p>
</div>
<!-- Table -->
<div class="table-wrapper">
<table class="table table-striped">
<thead>
<tr>
<th>序号</th>
<th>用户名</th>
<th>密码</th>
<th>邮箱</th>
<th>性别</th>
</tr>
</thead>
<tbody>
<tr th:each="userByPage:${usersByPage}">
<td th:text="${userByPage.getId()}"></td>
<td th:text="${userByPage.getUsername()}"></td>
<td th:text="${userByPage.getPassword()}"></td>
<td th:text="${userByPage.getEmail()}"></td>
<td th:text="${userByPage.getGender()==0?'':''}"></td>
</tr>
</tbody>
</table>
</div>
<!-- 分页 -->
<div class="paging">
<!-- 自定义一个容器,用于存放pageNumber数据,并让此容器display:none;-->
<div id="pageNumber" th:text="${pageNumber}" style="display: none"></div>
<nav aria-label="Page navigation">
<ul id="sorter" class="pagination pagination-lg">
</ul>
</nav>
</div>
</div>
<script> window.onload = function (){ 
 //获取pageNumber let pageNumber = document.getElementById("pageNumber"); let number = parseInt(pageNumber.innerText); //获取ul对象 let sorter = document.getElementById("sorter"); sorter.innerHTML += '<li><a href="#" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>'; for (let i = 0; i < number; i++) { 
 sorter.innerHTML += '<li><a href="' + (i+1)+ '">' + (i+1) + '</a></li>'; } sorter.innerHTML += '<li><a href="#" aria-label="Previous"><span aria-hidden="true">&raquo;</span></a></li>'; } </script>
</body>
</html>

4、UserController修改

package com.example.pagingQuery.controller;
import com.example.pagingQuery.dao.UserDao;
import com.example.pagingQuery.pojo.PageList;
import com.example.pagingQuery.pojo.User;
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.PathVariable;
import java.text.ParseException;
import java.util.List;
@Controller
public class UserController { 

@Autowired
UserDao userDao;
@GetMapping("/index")
public String showList(Model model){ 

List<User> users = userDao.getUserList();
model.addAttribute("users",users);
return "userList";
}
@GetMapping("/user/{pageSize}/{currentPage}")
public PageList<User> UserPageList(@PathVariable("pageSize") int pageSize,@PathVariable("currentPage") int currentPage){ 

return userDao.getUserListByPage(currentPage,pageSize);
}
@GetMapping({ 
"/user/listByPage/{currentPage}","localhost:8080/user/listByPage/{currentPage}"})
public String showListByPage(@PathVariable("currentPage") int currentPage,Model model){ 

PageList<User> userPageList = UserPageList(7, currentPage);
//起不同的名,与showList区分一下
List<User> usersByPage = userPageList.getDataList();
model.addAttribute("usersByPage",usersByPage);
//获取总的页数
int pageNumber = userPageList.getPageNumber();
model.addAttribute("pageNumber",pageNumber);
return "userList";
}
}

5、展示效果图

访问链接:localhost:8080/user/listByPage/1

效果图1

在这里插入图片描述

效果图2

在这里插入图片描述

效果图3

在这里插入图片描述

效果图4

在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/183978.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

  • 至强系列cpu天梯图_cpu天梯图2018

    至强系列cpu天梯图_cpu天梯图2018Helio,大家好,距离上一次天梯图更新已经过去一个月时间了,伴随着新的月份到来,新的天梯图该进行更新修正了,下面小编带来CPU天梯图2018年11月最新版,希望对大家有所帮助。CPU天梯图2018年11月最新版:CPU天梯图2018年11月最新版十一月电脑CPU天梯图性能排行新增了几款上个月新上市的几款处理器,尤其是Intel九代酷睿处理器的排行情况,通过前面的评测相信大家基本上对九代酷睿处理器…

  • 关系数据库基础理论[通俗易懂]

    关系数据库基础理论[通俗易懂]mysql系列之一关系数据库基础理论正是数据库管理的需要催生了数据库管理系统DBMS,而关系型数据库管理系统为RDBMS常见的数据模型有三种:-层次模型-网状模型-关系模型一、关系数据库的产生在DBMS出现之前,人们用文件来管理数据,但存在很多缺陷:1.数据冗余和不一致性。数据冗余表示在每个shell脚本中基本上都是/bin/bash,但很多用户使用…

    2022年10月16日
  • sql调用存储过程exec用法_sqlserver存储过程执行日志

    sql调用存储过程exec用法_sqlserver存储过程执行日志一、【存储过程】存储过程的T-SQL语句编译以后可多次执行,由于T-SQL语句不需要重新编译,所以执行存储过程可以 提高性能。存储过程具有以下特点:• 存储过程已在服务器上存储• 存储过程具有安全特性• 存储过程允许模块化程序设计• 存储过程可以减少网络通信流量• 存储过程可以提高运行速度 存储过程分为用户存储过程、系统存储过程和扩展存储过程。存储过程Procedure是一组为了完成…

  • 数据仓库分三层_数据库分层

    数据仓库分三层_数据库分层数据仓库各层说明: 一、数据加载层:ETL(Extract-Transform-Load) 二、数据运营层:ODS(OperationalDataStore) 三、数据仓库层:DW(DataWarehouse) 1.数据明细层:DWD(DataWarehouseDetail) 2.数据中间层:DWM(DataWareHouseMiddle) 3.数据服务层:DWS(DataWareHouseService) 四、数据应用层:A

  • Charles激活成功教程版_charles抓包乱码

    Charles激活成功教程版_charles抓包乱码RegisteredName:https://zhile.ioLicenseKey:48891cf209c6d32bf4

    2022年10月22日
  • Eclipse中代码字体背景变红/变黄/变绿

    Eclipse中代码字体背景变红/变黄/变绿如图所示:运行之后,突然这样。到底是什么原因导致的呢?:经过查找资料可知:因为Eclipse中有覆盖代码功能(绿色表示代码被执行到,红色表示代码没有被执行到,黄色表示代码部分执行到)怎么解决这个问题:Remove All Sessions就是点击下图红色框中的部分。                            或者点击window–&gt;Show view –&gt; Other …

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号