mybatis-plus集成pagehelper进行分页排序和返回查询总数

一、项目搭建

新建一个springboot项目

1.引入依赖

pom.xml

  <dependency>
    <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
 </dependency>
 <!--  Druid 数据库连接池和监控的功能 -->
 <dependency>
     <groupId>com.alibaba</groupId>
     <artifactId>druid-spring-boot-starter</artifactId>
     <version>1.1.23</version>
 </dependency>

 <!-- mysql驱动 -->
 <dependency>
     <groupId>mysql</groupId>
     <artifactId>mysql-connector-java</artifactId>
     <version>8.0.17</version>
 </dependency>

 <!-- pagehelper 分页插件 -->
 <!-- 报错信息:Relying upon circular references is discouraged and they are prohibited by default.
  Update your application to remove the dependency cycle between beans. As a last resort,
  it may be possible to break the cycle automatically by setting spring.main.
  allow-circular-references to true.
 原因:SpringBoot与PageHelper的版本冲突导致的,使用SpringBoot2.6及以上版本,对应的PageHelper版本应该在1.4.1及以上
  -->
 <dependency>
     <groupId>com.github.pagehelper</groupId>
     <artifactId>pagehelper-spring-boot-starter</artifactId>
     <version>1.4.1</version>
 </dependency>

 <!-- Mybatis-plus -->
 <dependency>
     <groupId>com.baomidou</groupId>
     <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
     <version>2.5.3</version>
 </dependency>
 <dependency>
     <groupId>com.baomidou</groupId>
     <artifactId>mybatis-plus-boot-starter</artifactId>
     <version>3.1.0</version>
 </dependency>
 <dependency>
     <groupId>com.baomidou</groupId>
     <artifactId>mybatis-plus-generator</artifactId>
     <version>3.2.0</version>
 </dependency>

 <dependency>
     <groupId>org.projectlombok</groupId>
     <artifactId>lombok</artifactId>
     <optional>true</optional>
 </dependency>

注意:如果在项目启动时报错:

Relying upon circular references is discouraged and they are
prohibited by default. Update your application to remove the
dependency cycle between beans. As a last resort, it may be possible
to break the cycle automatically by setting spring.main.
allow-circular-references to true.

原因:SpringBoot与PageHelper的版本冲突导致的,使用SpringBoot2.6及以上版本,对应的PageHelper版本应该在1.4.1及以上

2.配置文件

application.yml

server:
  port: 8181

spring:
  datasource:
    # 驱动类
    driver-class-name: com.mysql.cj.jdbc.Driver
    # 连接,注意各个配置,尤其是要一次性执行多条 SQL 时,要 allowMultiQueries=true          useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&serverTimezone=UTC&autoReconnect=true&useSSL=false
    url: jdbc:mysql://192.168.0.182:13307/platform?useUnicode=true&useSSL=false&allowMultiQueries=true&characterEncoding=utf-8&useLegacyDatetimeCode=false&useAffectedRows=true&serverTimezone=GMT%2B8
    # 用户名
    username: root
    # 密码
    password: 123456

############ mybatis-plus配置 #############
mybatis-plus:
  type-enums-package: com.zhouquan.entity.enums*
  configuration:
    cache-enabled: true
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    # 关闭sql打印
    # log-impl: org.apache.ibatis.logging.nologging.NoLoggingImpl

############ 分页插件PageHelper配置 #############
pagehelper:
  helperDialect: mysql
  reasonable: true
  supportMethodsArguments: true
  params: count=countSql

项目结构

在这里插入图片描述

二、使用测试

在这里插入图片描述
因此 PageHelper.startPage(pageNum, pageSize, orderField);务必要写在需要执行的查询上面

1.普通的列表查询

在这里插入图片描述
服务的实现类只是简单的列表查询
在这里插入图片描述

postman调用后直接看控制台打印结果

http://192.168.0.146:8181/v1/book_data/list?pageNum=1&pageSize=10&orderField=publish_date asc

在这里插入图片描述
将查询出的list对象放入PageInfo的构造器中,即可获取到总数用于前端分页
在这里插入图片描述
返回结果:

{
    "total": 38,
    "recordList": [
        {
            "id": 25,
            "title": "野草",
            "author": "鲁迅",
            "publishDate": "1973-03-01"
        },
        {
            "id": 14,
            "title": "呐喊",
            "author": "鲁迅",
            "publishDate": "1973-03-01"
        }
    ...
}

2.自定义sql列表查询

controller.java
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
控制台打印
在这里插入图片描述

三、pagehelper的实现

官方文档写的更清晰
https://pagehelper.github.io/docs/interceptor/

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
spring boot整合mybatis-pluspagehelper分页插件是一种常见的开发方式,可以实现数据库的分页查询功能。下面是一个简单示例项目的源码,以供参考: 首先,需要在pom.xml文件中添加相关依赖: ```xml <dependencies> <!-- Spring Boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- MyBatis Plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.0</version> </dependency> <!-- PageHelper --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency> <!-- MySQL Connector --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.27</version> </dependency> </dependencies> ``` 在application.properties(或application.yml)文件中配置数据库连接信息: ```yaml spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver mybatis-plus.configuration.map-underscore-to-camel-case=true ``` 创建一个简单的实体类User: ```java public class User { private Long id; private String username; private String password; // 省略getter和setter方法 } ``` 创建一个Mapper接口UserMapper: ```java @Mapper public interface UserMapper extends BaseMapper<User> { // 省略其他方法 List<User> getUsersByPage(Page<User> page, @Param("username") String username); } ``` 创建一个Service接口UserService以及其实现类UserServiceImpl: ```java @Service public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService { @Autowired private UserMapper userMapper; @Override public IPage<User> getUsersByPage(Page<User> page, String username) { return userMapper.getUsersByPage(page, username); } } ``` 在Controller中注入UserService,并进行分页查询操作: ```java @RestController public class UserController { @Autowired private UserService userService; @GetMapping("/users") public IPage<User> getUsers(@RequestParam(value = "page", defaultValue = "1") Integer pageNum, @RequestParam(value = "size", defaultValue = "10") Integer pageSize, @RequestParam(value = "username", required = false) String username) { Page<User> page = new Page<>(pageNum, pageSize); return userService.getUsersByPage(page, username); } } ``` 至此,就完成了spring boot整合mybatis-pluspagehelper分页插件的配置和使用。 请注意,这是一个简单示例项目,实际使用中可能需要根据需求进行适当调整和修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

徐州蔡徐坤

又要到饭了兄弟们

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值