关键词:
七、SpringBoot整合pageHelper分页组件以及前端js分页插件
1、PageHelper简介
我们知道mysql实现分页查询,要使用limit分页公式:curPage是当前第几页;pageSize是一页多少条记录,limit (curPage-1)*pageSize,pageSize;
select * from dept order by deptno desc limit m,n;
Oracle实现分页查询,内层不排序的情况下至少需要两层嵌套子查询:
--查询第6-9条数据(嵌套子查询-)
select * from (select e.*,rownum r from emp2 e where rownum<=9) where r>=5;
由上述可知,无论是mysql还是Oracle,要么是使用内置函数,要么是限制SQL查询语句,当然可以实现分页查询,但是在效率上存在一定的低能耗时,而且不易于与前端页面的整合,所以后者就引入了PageHelper分页组件的使用。
PageHelper是一款基于MyBatis开源的分页插件,使用非常方便,支持各种复杂的单表、多表分页查询,让你在写sql时无需考虑分页问题,只需查询出所有数据即可,剩下的PageHelper帮你搞定。
我们将简单介绍PageHelper的基本使用和配置参数的含义,重点分析PageHelper作为Mybatis分页插件的实现原理。PageHelper官网
2、PageHelper支持物理分页的数据库
- Oracle
- Mysql
- MariaDB
- SQLite
- Hsqldb
- PostgreSQL
- DB2
- SqlServer(2005,2008)
- Informix
- H2
- SqlServer2012
- Derby
- Phoenix
3、SpringBoot集成PageHelper
1、先引入pageHelper相关依赖:
<!--PageHelper分页 注意是以starter结尾(启动器)-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
2、导入oracle数据库依赖驱动和mybatis依赖驱动
<!--Oracle驱动类-->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.2.0</version>
</dependency>
<!--SpringBoot 整合mybatis 启动器-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
4、配置datasource和thymeleaf
#配置Thymeleaf视图配置
spring:
thymeleaf:
prefix: classpath:/templates/ #默认视图模板路径
suffix: .html #默认解析视图后缀
mode: HTML5 #使用非严格 HTML(LEGACYHTML5)
encoding: UTF-8 #编码格式
cache: false #是否开启缓存
check-template-location: true
datasource:
url: jdbc:oracle:thin:localhost:1521:orcl
driver-class-name: oracle.jdbc.OracleDriver
username: ebuy
password: 123456
tomcat:
max-active: 20
max-wait: 60000
min-idle: 2
initial-size: 1
5、选用SystemUserinfo和SystemRole数据库表
--system_userinfo表
create table SYSTEM_USERINFO
(
userinfo_uid VARCHAR2(11) not null,
userinfo_loginid VARCHAR2(20) not null,
userinfo_name VARCHAR2(50) not null,
userinfo_password VARCHAR2(64) not null,
userinfo_sex CHAR(3),
userinfo_email VARCHAR2(100),
userinfo_mobile VARCHAR2(20),
userinfo_status NUMBER(1),
userinfo_roleid VARCHAR2(20)
)
--system_role表
create table SYSTEM_ROLE
(
role_id VARCHAR2(20) not null,
role_name VARCHAR2(50) not null,
role_code VARCHAR2(40),
role_description VARCHAR2(100)
)
存入一些相关数据:
6、mybatis逆向工程
使用mybatis插件和驱动依赖为我们自动创建pojo层实体类、dao层接口、mapper层映射文件,以及自动帮我们创建好常用的增删改查等数据库操作方法,还为我们自动做好实体类属性与数据库表字段的映射关系,用起来贼six!
SystemUserinfoMapper接口(dao层):
package com.kdcrm.mapper;
import com.kdcrm.pojo.SystemUserinfo;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface SystemUserinfoMapper
SystemUserinfo login(SystemUserinfo systemUserinfo);
int deleteByPrimaryKey(String userinfoUid);
int insert(SystemUserinfo record);
SystemUserinfo selectByPrimaryKey(String userinfoUid);
List<SystemUserinfo> selectAll(SystemUserinfo systemUserinfo);
int updateByPrimaryKey(SystemUserinfo record);
SystemRoleMapper接口(dao层):
package com.kdcrm.mapper;
import com.kdcrm.pojo.SystemRole;
import java.util.List;
public interface SystemRoleMapper
int deleteByPrimaryKey(String roleId);
int insert(SystemRole record);
SystemRole selectByPrimaryKey(String roleId);
List<SystemRole> selectAll();
int updateByPrimaryKey(SystemRole record);
7、编写service业务逻辑层接口及实现类(关键)
我们主要是为了演示分页,为了观看体验,我们只写selectAll方法:
SystemUserinfoService接口:
package com.kdcrm.service;
import com.github.pagehelper.PageInfo;
import com.kdcrm.pojo.SystemUserinfo;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface SystemUserinfoService
/**
* pageHelper分页
* @param pageNum 当前页
* @param pageSize 每页显示条数
* @return
*/
PageInfo<SystemUserinfo> selectAll(SystemUserinfo systemUserinfo,int pageNum,int pageSize);
SystemUserinfoServiceImpl实现类:
package com.kdcrm.service.impl;
/**
* @author 一宿君(CSDN : qq_52596258)
* @date 2021-08-03 10:37:38
*/
@Service
@SuppressWarnings("all")
public class SystemUserinfoServiceImpl implements SystemUserinfoService
@Autowired
SystemUserinfoMapper systemUserinfoMapper;
@Autowired
RedisTemplate redisTemplate;
@Override
public PageInfo<SystemUserinfo> selectAll(SystemUserinfo systemUserinfo,int pageNum,int pageSize)
//告诉分页组件(当前页数和每页显示数量)
PageHelper.startPage(pageNum,pageSize);
//查询出所有数据
List<SystemUserinfo> list = systemUserinfoMapper.selectAll(systemUserinfo);
//将数据放入分页信息管理对象中
PageInfo<SystemUserinfo> pageInfo = new PageInfo<>(list);
return pageInfo;
我们进入PageHelper源码中查看封装属性:
public static final int DEFAULT_NAVIGATE_PAGES = 8;
private int pageNum; //当前页码
private int pageSize; //每页数量
private int size; //当前页数量
private long startRow; //当前页面第一行数据在数据表中的行号
private long endRow; //当前页面最后一行数据在数据表中的行号
private int pages; //首页
private int prePage; //上一页
private int nextPage; //下一页
private boolean isFirstPage; //是否是第一页
private boolean isLastPage; //是否是最后一页
private boolean hasPreviousPage; //是否有上一页
private boolean hasNextPage; //是否有下一页
private int navigatePages;//导航页码数
private int[] navigatepageNums; //所有导航页号
private int navigateFirstPage; //首页
private int navigateLastPage; //尾页
8、编写Controller控制层(关键)
我们可以在控制层将PageHelper组件的内置封装属性全部打印出来:
package com.kdcrm.controller;
/**
* @author 一宿君(CSDN : qq_52596258)
* @date 2021-08-03 10:45:07
*/
@Controller
public class SystemUserInfoController
@Autowired
SystemUserinfoService systemUserinfoService;
@Autowired
SystemRoleService systemRoleService;
/**
*
* @param systemUserinfo 用于接收前端表单数据
* @param request
* @param pageNum 当前页,如果没有传入该值,则默认为1
* @param pageSize 每页显示数量 默认为5
* @return
*/
@RequestMapping("/selectAll")
public String selectAll(SystemUserinfo systemUserinfo,HttpServletRequest request, @RequestParam(name = "pageNum",defaultValue = "1")int pageNum,@RequestParam(name = "pageSize",defaultValue = "5")int pageSize)
//分页组件
PageInfo<SystemUserinfo> pageinfo = systemUserinfoService.selectAll(systemUserinfo,pageNum,pageSize);
//所有角色集合
List<SystemRole> roleList = systemRoleService.selectAll();
System.out.println("当前页:" + pageinfo.getPageNum());
System.out.println("每页数量:" +pageinfo.getPageSize());
System.out.println("当前页的数量:" +pageinfo.getSize());
System.out.println("总记录数:" + pageinfo.getTotal());
System.out.println("总页数:" + pageinfo.getPages());
System.out.println("当前页面第一行数据在数据表中的行号 :" + pageinfo.getStartRow());
System.out.println("当前页面最后一行数据在数据表中的行号 :" + pageinfo.getEndRow());
System.out.println("首页:" + pageinfo.getNavigateFirstPage());
System.out.println("上一页:" + pageinfo.getPrePage());
System.out.println("下一页:" + pageinfo.getNextPage());
System.out.println("末页:" + pageinfo.getNavigateLastPage());
System.out.println("导航页码数:" + pageinfo.getNavigatePages());
System.out.println("是否是第一页:" + pageinfo.isIsFirstPage());
System.out.println("是否是最后一页:" + pageinfo.isIsLastPage());
System.out.println("是否有前一页:" + pageinfo.isHasPreviousPage());
System.out.println("是否有后一页:" + pageinfo.isHasNextPage());
System.out.println("所有导航页号:" + Arrays.toString(pageinfo.getNavigatepageNums()));
System.out.println("结果集:" + pageinfo.getList());
if(pageinfo != null && roleList != null)
request.setAttribute("roleList",roleList);
request.setAttribute("systemUserinfo", systemUserinfo);
request.setAttribute("pageInfo", pageinfo);
return "list";
9、编写前端视图页面list.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en" >
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form method="post" action="/selectAll" id="userForm" th:align="center" style="margin-top: 50px">
<input type="hidden" id="pageNum" name="pageNum"/>
登录名:<input type="text" id="userinfoLoginid" name="userinfoLoginid" th:value="$systemUserinfo.userinfoLoginid" /><br/>
性别:<input type="radio" name="userinfoSex" th:checked="$systemUserinfo.userinfoSex == '男'" value="男"/>男
<input type="radio" name="userinfoSex" th:checked="$systemUserinfo.userinfoSex == '女'" value="女"/>女
<br/>
邮箱:<input type="text" id="userinfoEmail" name="userinfoEmail" th:value="$systemUserinfo.userinfoEmail" /><br/>
电话:<input type="text" id="userinfoMobile" name="userinfoMobile" th:value="$systemUserinfo.userinfoMobile"/><br/>
角色:<select id="sel" name="userinfoRoleid">
<option value="">--请选择--</option>
<option th:selected="$systemUserinfo.userinfoRoleid == role.roleId" th:each="role:$roleList" th:text="$role.roleName" th:value="$role.roleId" ></option>
</select>
<input type="submit" value="查询"/>
springboot系列springboot整合持久层(代码片段)
这里写目录标题四、SpringBoot整合持久层4.1SpringBoot项目工程搭建4.2MyBatis逆向工程生成pojo和mapper文件1先建立包和文件夹,用于存储配置文件和逆向生成pojo和mapper文件2先引入pom依赖文件4.3应用实现增删改查四、SpringBoot整合持... 查看详情
springboot系列十springboot整合redis
...a里面提供的官方工具包:jedis,所以即便你现在使用的是SpringBoot,那么也继续使用此开发包。二、redidTemplate操作在Spring支持的Redis操作之中提供有一个RedisTemplate处理程序类,利用这个类可以非常方便的实现Redis的各种基本数据... 查看详情
springboot2系列教程|springboot整合mybatis
前言如题,今天介绍SpringBoot与Mybatis的整合以及Mybatis的使用,本文通过注解的形式实现。什么是MybatisMyBatis是支持定制化SQL、存储过程以及高级映射的优秀的持久层框架。MyBatis避免了几乎所有的JDBC代码和手动设置参数以及获取... 查看详情
springboot系列之springboot与mybatis整合
...也不是太方便,眼过千遍不如手过一遍的操作一遍,即使Springboot已经很好的整合了各项的技术框架,但实际操作的时候也会发现一些问题。我会将可能出现的问题记录一下,博文时刻更新。预备知识:Springboot2.0.6Mybatis3.4.6Maven3.5... 查看详情
消息队列mq——springboot整合rabbitmq(代码片段)
...bitMQ的介绍、安装以及管理页面的使用消息队列MQ(二)——SpringBoot整合RabbitMQ文章目录系列文章目录前言一、简介二、搭建工程1.创建P系统(生产者)1.1yml配置RabbitMQ属性2.创建C系统(消费者)2.1yml配置RabbitMQ属性三、实操RabbitMQ五种工... 查看详情
springboot应用系列6--springboot2整合quartz
...bsp;Scheduler(调度器):负责Job的执行。有两种方式可以实现SpringBoot与Quartz的整合:一、使用Spring 查看详情
springboot系列-springboot整合rabbitmq
一RabbitMQ的介绍 RabbitMQ是消息中间件的一种,消息中间件即分布式系统中完成消息的发送和接收的基础软件.这些软件有很多,包括ActiveMQ(apache公司的),RocketMQ(阿里巴巴公司的,现已经转让给apache). 消息中间件的工作过程可... 查看详情
springboot2系列教程|整合thymeleaf
...eaf,并整合Thymeleaf开发一个简陋版的学生信息管理系统。SpringBoot提供了大量模板引擎,包含Freemarker、Groovy、Thymeleaf、Velocity以及Mustache,SpringBoot中推荐使用Thymeleaf作为模板引擎,因为Thymeleaf提供了完美的SpringMVC支持。Thymeleaf是... 查看详情
springboot应用系列5--springboot2整合logback
上一篇我们梳理了SpringBoot2整合log4j2的配置过程,其中讲到了SpringBoot2原装适配logback,并且在非异步环境下logback和log4j2的性能差别不大,所以对于那些日志量不算太高的项目来说,选择logback更简单方便。1.pom.xmlpom.xml不需要添加... 查看详情
如何整合springboot+mybatis-plus(系列一)(代码片段)
如何整合springboot+mybatis-plus(系列一)背景整合springboot+mybatis-plus,其实就是整合springboot+mybatis+mybatis-plus,本来mybatis-plus就能自动包含mybatis步骤非常简单,用IDEA新建项目,选择Sprin 查看详情
activemq学习系列activemq与springboot整合
activemq与springboot整合1、添加依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-activemq</artifactId><exclusions><exclusion> 查看详情
spring系列——springboot整合quarter定时任务
简介本文主要介绍Spring系列Springboot整合quarter定时任务,大致分为三个部分:添加jar包、创建一个定时任务的配置类、创建具体执行的任务,希望看完本文后对大家有所帮助。一、添加jar包<!--quartz定时任务--><dependency><... 查看详情
springboot系列springboot整合pagehelper分页组件以及前端js分页插件(代码片段)
七、SpringBoot整合pageHelper分页组件以及前端js分页插件1、PageHelper简介2、PageHelper支持物理分页的数据库3、SpringBoot集成PageHelper4、配置datasource和thymeleaf5、选用SystemUserinfo和SystemRole数据库表6、[mybatis逆向工程](https://blog.csdn.net/qq_5259 查看详情
redis系列:springboot整合redisson
参考技术ARedissongithub:wikiRedisson作为一个客户端工具,能够使开发者减少对Redis的关注,从而将精力集中到业务上。Redisson除了提供基础的Redis服务外,还提供了较为可靠的分布式锁,布隆过滤器等功能。pom.xml,引入Redisson3.13.4版... 查看详情
spring系列——springboot整合quarter定时任务
参考技术A本文主要介绍Spring系列Springboot整合quarter定时任务,大致分为三个部分:添加jar包、创建一个定时任务的配置类、创建具体执行的任务,希望看完本文后对大家有所帮助。可以理解为这个类要去执行什么任务,什么时候... 查看详情
springboot2系列教程(十八)|整合mongodb
...,请后台留言,反正我也不会听。前言如题,今天介绍下SpringBoot是如何整合MongoDB的。MongoDB简介MongoDB是由C++编写的非关系型数据库,是一个基于分布式文件存储的开源数据库系统,它将数据存储为一个文档,数据结构由键值(key=... 查看详情
java之springboot入门到精通idea版springboot整合其他框架junit,redis,mybatis(一篇文章精通系列)中(代码片段)
SpringBoot整合其他框架【Junit,Redis,MyBatis】一、SpringBoot整合Junit①搭建SpringBoot工程②引入starter-test起步依赖③编写测试类(1)在启动类傍边其他类④添加测试相关注解⑤编写测试方法二、SpringBoot整合Redis1、搭建SpringBoot工... 查看详情
springboot2系列教程(十六)|整合websocket实现广播
前言如题,今天介绍的是SpringBoot整合WebSocket实现广播消息。什么是WebSocket?WebSocket为浏览器和服务器提供了双工异步通信的功能,即浏览器可以向服务器发送信息,反之也成立。WebSocket是通过一个socket来实现双工异步通信能力... 查看详情