SpringBoot 整合Mybatis + PageHelper 实现分页

前言:

  • 现在公司大多数都实现了前后端分离,前端使用Vue、React、AngularJS 等框架,不用完全依赖后端。但是如果对于比较小型的项目,没必要前后端分离,而SpringBoot也基本抛弃了Jsp,使用Thymeleaf模板搭配SpringBoot是个不错的选择。
  • 在展示数据时必然需要大量的分页操作,在使用JPA、Mybatis-Plus等持久层框架时,已经自带分页查询操作,无需手动编写,然而在使用Mybatis作为持久层时,需要手动编写分页操作十分麻烦。PageHelper就是用来在Mybatis之上帮助我们实现分页操作。

开发环境:

  • IDEA IntelliJ IDEA 2019.3.3 x64
  • JDK: 1.8
  • SpringBoot: 2.25
  • PageHelper: 1.2.13
  • Mybatis: 2.1.3 (使用Mybatis作为持久层)
  • 数据源:Druid 1.1.23

一、SpringBoot框架搭建

【1】点击:File —> New —> Project

image-20200722002449351

【2】这个页面选项是选择SpringBoot需要的启动依赖,在这里可以有很多选项,这里选择 Web 和 SQL中的相应配置,然后点击下一步。(这里的SpringBoot版本有些过高,可以选择使用默认的最新版本)

image-20200722002852164

【3】保存路径,点击FINSH完成。

image-20200722003036710

二、详细配置

1、在pom文件中引入Pagehelper分页插件,Driud数据源(非必需)

<!-- Mybatis 分页插件 -->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.2.13</version>
</dependency>

<!-- Druid 数据源-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.23</version>
</dependency>

2、创建数据库

数据库名:pagehelperdemo 编码字符集:utf8

CREATE DATABASE `pagehelperdemo`;

USE `pagehelperdemo`;

DROP TABLE IF EXISTS `users`;

CREATE TABLE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id主键',
  `username` varchar(20) NOT NULL COMMENT '用户名',
  `PASSWORD` varchar(20) NOT NULL COMMENT '用户密码',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;

insert  into `users`(`id`,`username`,`PASSWORD`) values 
(1,'onetest','123'),
(2,'twotest','456'),
(3,'onetest','789'),
(4,'twotest','987'),
(5,'onetest','654'),
(6,'twotest','321'),
(7,'onetest','147'),
(8,'twotest','258'),
(9,'onetest','369'),
(10,'twotest','963');

插入多条数据方便分页查看。

image-20200722005028707

3、配置分页插件

将resource文件夹下的application.properties配置文件改成yml后缀,即application.yml,进行如下配置

server:
  port: 8089

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/pagehelperdemo?useUnicode=true&characterEncoding=UTF-8
    username: root
    password: root

  thymeleaf:
    encoding: UTF-8
    prefix: classpath:/templates/
    suffix: .html
    cache: false
    
    
#mybatis:
#  mapper-locations: classpath:/mapper/*.xml  #项目中使用注解进行开发


pagehelper:
  helper-dialect: mysql # 指定数据库类型
  reasonable: true
  params: count=countSql
  support-methods-arguments: true

三、代码编写

1、创建用户实体类

package com.jia.pojo;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: ButterflyStars
 * @DateTime: Created in 2020/7/22 1:28
 * @QQ: 1498575492
 * Description: 用户信息实体类
 */
public class User {
   
    private Integer id;
    private String username;
    private String password;

    public Integer getId
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值