如何使用mybatis-plus实现分页查询功能

今天就跟大家聊聊有关使用mybatis-plus如何实现分页查询功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

引入依赖:

<!-- 引入mybatisPlus -->
    <dependency>
   <groupId>com.baomidou</groupId>
   <artifactId>mybatis-plus-boot-starter</artifactId>
   <version>3.2.0</version>
  </dependency>
  <!-- 引入mysql驱动包 -->
  <dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>5.1.27</version>
  </dependency>
  <!-- 引入Druid依赖,阿里巴巴所提供的数据源 -->
  <dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>druid</artifactId>
   <version>1.0.29</version>
    </dependency>

在application.yml配置

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

在启动类上面添加@MapperScan注解,扫描mapper包

@SpringBootApplication
@MapperScan("com.qiao.demo02.mapper")
public class SpringbootDemo02Application {

 public static void main(String[] args) {
  SpringApplication.run(SpringbootDemo02Application.class, args);
 }

}

新建User和UserMapper

user类

@Data
public class User {
 @TableId
 private Integer userId;
 private String userName;
 private Integer userAge;
 private String userEmail;
}
UserMapper接口
 public interface UserMapper extends BaseMapper<User> {

 }

最重要的是继承BaseMapper接口:里面声明了很强大的CRUD方法

public interface BaseMapper<T> extends Mapper<T> {
 int insert(T entity);
 int deleteById(Serializable id);
 int deleteByMap(@Param("cm") Map<String, Object> columnMap);
 int delete(@Param("ew") Wrapper<T> wrapper);
 int deleteBatchIds(@Param("coll") Collection<? extends Serializable> idList);
 int updateById(@Param("et") T entity);
 int update(@Param("et") T entity, @Param("ew") Wrapper<T> updateWrapper);
 T selectById(Serializable id);
 List<T> selectBatchIds(@Param("coll") Collection<? extends Serializable> idList);
 List<T> selectByMap(@Param("cm") Map<String, Object> columnMap);
 T selectOne(@Param("ew") Wrapper<T> queryWrapper);
 Integer selectCount(@Param("ew") Wrapper<T> queryWrapper);
 List<T> selectList(@Param("ew") Wrapper<T> queryWrapper);
 List<Map<String, Object>> selectMaps(@Param("ew") Wrapper<T> queryWrapper);
 List<Object> selectObjs(@Param("ew") Wrapper<T> queryWrapper);
 IPage<T> selectPage(IPage<T> page, @Param("ew") Wrapper<T> queryWrapper);
 IPage<Map<String, Object>> selectMapsPage(IPage<T> page, @Param("ew") Wrapper<T> queryWrapper);
}

分页查询

这点官方文档讲的也很详细:https://mp.baomidou.com/guide/page.html

新建一个config包,在里面建一个MybatisPlus配置类 返回一个分页拦截器

package com.qiao.demo02.config;

@Configuration
@ConditionalOnClass(value = {PaginationInterceptor.class})
public class MybatisPlusConfig {
 @Bean
 public PaginationInterceptor paginationInterceptor() {
  PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
  return paginationInterceptor;
 }
}

这样就能使用mybatis的分页功能了

Junit测试

@Resource
 private UserMapper userMapper;
 @Test
 public void queryUserForPage(){
  IPage<User> userPage = new Page<>(2, 2);//参数一是当前页,参数二是每页个数
  userPage = userMapper.selectPage(userPage, null);
  List<User> list = userPage.getRecords();
  for(User user : list){
   System.out.println(user);
  }
 }

Controller返回json串

先定义一个包装类UserVo,用来保存分页所需要的数据

package com.qiao.demo02.vo;

@Data
public class UserVo {
 private Integer current;
 private Integer size;
 private Long total;
 private List<User> userList;
}

然后在控制器编写代码,这里省略了service层,实际开发业务代码写在service层,Controller只负责:接受参数、调用service层方法处理业务逻辑,返回结果

Controller类贴上了@RestController注解

 @GetMapping("queryUser")
 public UserVo queryList(Integer current, Integer size) {
  /**
   * 这些代码应该写在service层
   */
  UserVo userVo = new UserVo();
  IPage<User> page = new Page<>(current, size);
  userMapper.selectPage(page, null);
  userVo.setCurrent(current);
  userVo.setSize(size);
  userVo.setTotal(page.getTotal());
  userVo.setUserList(page.getRecords());
  return userVo;
 }

附上结果,前端直接处理json数据即可

看完上述内容,你们对使用mybatis-plus如何实现分页查询功能有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注小编,感谢大家的支持。

总结

到此这篇关于如何使用mybatis-plus实现分页查询功能的文章就介绍到这了,更多相关mybatis-plus分页查询功能内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • MyBatis-Plus 分页查询的实现示例

    方法: 使用selectPage()方法,第一个参数是传入分页方法(传入当前页和当前显示多少条数据),第二个参数是传入查询条件(如果查询全部的话,可以传null). 前提: 表中的数据为: 第一种方式: //分页查询 Page<Employee> employees = employeeMapper.selectPage(new Page<>(3, 2), null); System.out.println("数据为:"+employees.getRecords

  • MyBatis-Plus多表联合查询并且分页(3表联合)

    这3张表的关系是模型表Model  ===> 训练表Training ===>应用表Application(大概的逻辑是:选择应用,然后训练,然后成为模型) 首先我们先建立实体Model(我使用的data注解不需要get set  @TableField(exist = false) 注解下的属性 是相关联表的属性) package cn.com.befery.dataai.po; import java.util.Date; import org.springframework.boot.j

  • mybatis-plus分页查询的实现示例

    按照官方文档进行的配置:快速开始|mybatis-plus 引入依赖: <!-- 引入mybatisPlus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.2.0</version> </dependency> <!--

  • springboot整合mybatis-plus实现多表分页查询的示例代码

    1.新建一个springboot工程 2.需要导入mybatis和mybatis-plus的依赖文件 <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.1.1</version> </dependency> <dependency> &l

  • MyBatis-Plus 分页查询以及自定义sql分页的实现

    一.引言 分页查询每个人程序猿几乎都使用过,但是有部分同学不懂什么是物理分页和逻辑分页. 物理分页:相当于执行了limit分页语句,返回部分数据.物理分页只返回部分数据占用内存小,能够获取数据库最新的状态,实施性比较强,一般适用于数据量比较大,数据更新比较频繁的场景. 逻辑分页:一次性把全部的数据取出来,通过程序进行筛选数据.如果数据量大的情况下会消耗大量的内存,由于逻辑分页只需要读取数据库一次,不能获取数据库最新状态,实施性比较差,适用于数据量小,数据稳定的场合. 那么MP中的物理分页怎么实现

  • Oracle使用MyBatis中RowBounds实现分页查询功能

    Oracle中分页查询因为存在伪列rownum,sql语句写起来较为复杂,现在介绍一种通过使用MyBatis中的RowBounds进行分页查询,非常方便. 使用MyBatis中的RowBounds进行分页查询时,不需要在 sql 语句中写 offset,limit,mybatis 会自动拼接 分页sql ,添加 offset,limit,实现自动分页. 需要前台传递参数currentPage和pageSize两个参数,分别是当前页和每页数量,controller层把参数传递给service层即可

  • MyBatis Plus 实现多表分页查询功能的示例代码

    在Mybatis Plus 中,虽然IService 接口帮我们定义了很多常用的方法,但这些都是 T 对象有用,如果涉及到 多表的查询,还是需要自定义Vo 对象和自己编写sql 语句,Mybatis Plus提供了一个Page 对象,查询是需要设置其中的 size 字段 和 current 字段的值 一.分页配置 可以直接使用selectPage这样的分页,但返回的数据确实是分页后的数据,但在控制台打印的SQL语句其实并没有真正的物理分页,而是通过缓存来获得全部数据中再进行的分页,这样对于大数据

  • ssm框架+PageHelper插件实现分页查询功能

    通过搭建ssm框架,然后通过mybatis的分页插件pagehelp进行分页查询. 源码:https://gitee.com/smfx1314/pagehelper 看一下项目结构: 首先创建一个maven工程,pom中引入相关jar包 <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId&

  • SpringBoot整合PageHelper实现分页查询功能详解

    前言 本文介绍的是MyBatis 分页插件 PageHelper,如果你也在用 MyBatis,建议尝试该分页插件,这一定是最方便使用的分页插件.分页插件支持任何复杂的单表.多表分页. 官方文档:https://pagehelper.github.io/ 项目地址:https://github.com/pagehelper/Mybatis-PageHelper 使用方法 导入依赖 在中央仓库sonatype中搜索 pageHelper,找到 pagehelper-spring-boot-star

  • 如何使用mybatis-plus实现分页查询功能

    今天就跟大家聊聊有关使用mybatis-plus如何实现分页查询功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获. 引入依赖: <!-- 引入mybatisPlus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <ver

  • kkpager 实现ajax分页查询功能

    前台分页数据,适合数据少量的时候,因为分页的数据是从后台获取的,大数据的话不建议使用 先看下前台代码: @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <script src="~/kkpager/lib/jquery-1.10.2.min.js

  • ajax实现分页查询功能

    ajax分页查询功能的具体代码,供大家参考,具体内容如下 显示的效果如下: 实现效果的代码如下: 1.fenye.php <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <link type="text/css" rel=

  • thinkPHP5框架实现分页查询功能的方法示例

    本文实例讲述了thinkPHP5框架实现分页查询功能的方法.分享给大家供大家参考,具体如下: controller文件内Admin.php <?php namespace app\admin\controller; use think\Controller; use app\admin\model\Admin as AdminModel; //使用分页类 取别名解决类名冲突 class Admin extends Controller{ public function lst(){ /* 分页开

  • JPA多条件复杂SQL动态分页查询功能

    概述 ORM映射为我们带来便利的同时,也失去了较大灵活性,如果SQL较复杂,要进行动态查询,那必定是一件头疼的事情(也可能是lz还没发现好的方法),记录下自己用的三种复杂查询方式. 环境 springBoot IDEA2017.3.4 JDK8 pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0&q

  • springboot+mongodb 实现按日期分组分页查询功能

    具体代码如下所示: WalletDetailsResp walletDetailsResp = new WalletDetailsResp(); List<WalletDetailsResp.WalletDetail> list = new ArrayList<>(); WalletDetailsResp.PageInfoBean pageInfoBean = new WalletDetailsResp.PageInfoBean(); List<Integer> typ

随机推荐