依赖配置可参考:MyBatis-Plus学习笔记(1):环境搭建以及基本的CRUD操作

分页配置

@Configuration
public class PlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
// 开启 count 的 join 优化,只针对部分 left join
paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
return paginationInterceptor;
}
}

通用Mapper查询分页

	@Test
public void testSelectPage(){
//分页对象,构造函数传入当前页数和每页条数
Page selectPage = new Page<User>(1, 10);
//是否查询总条数,默认为true,也可以通过构造函数传入
//selectPage.setSearchCount(false); //封装查询条件
Wrapper wrapper = new QueryWrapper<User>().gt("id", 0).orderByDesc("id"); //调用通用Mapper
Page<User> resultPage = userMapper.selectPage(selectPage, wrapper); System.out.println("总条数:" + resultPage.getTotal());
System.out.println("列表:" + resultPage.getRecords()); //true,返回的对象和查询时传入的对象是同一个对象
System.out.println(selectPage.equals(resultPage));
}

自定义Mapper查询分页

UserMapper.java:

package com.cf.plusdm.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cf.plusdm.entity.User; public interface UserMapper extends BaseMapper<User> {
IPage<User> getUserList(Page<User> page);
}

UserMapper.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cf.plusdm.mapper.UserMapper"> <!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="com.cf.plusdm.entity.User">
<id column="id" property="id" />
<result column="real_name" property="realName" />
<result column="email" property="email" />
<result column="phone" property="phone" />
</resultMap> <!-- 通用查询结果列 -->
<sql id="Base_Column_List">
id, real_name, email, phone
</sql> <select id="getUserList" resultMap="BaseResultMap">
SELECT
<include refid="Base_Column_List"></include>
FROM tb_user
</select> </mapper>

使用:

	@Test
public void testSelectPageXml(){
Page resultPage = new Page<User>(1, 10);
userMapper.getUserList(resultPage); System.out.println("总条数:" + resultPage.getTotal());
System.out.println("列表:" + resultPage.getRecords());
}

参考:分页插件

MyBatis-Plus学习笔记(3):分页查询的更多相关文章

  1. MyBatis:学习笔记(3)——关联查询

    MyBatis:学习笔记(3)--关联查询 关联查询 理解联结 SQL最强大的功能之一在于我们可以在数据查询的执行中可以使用联结,来将多个表中的数据作为整体进行筛选. 模拟一个简单的在线商品购物系统, ...

  2. mybatis学习笔记(10)-一对一查询

    mybatis学习笔记(10)-一对一查询 标签: mybatis mybatis学习笔记10-一对一查询 resultType实现 resultMap实现 resultType和resultMap实 ...

  3. MyBatis:学习笔记(4)——动态SQL

    MyBatis:学习笔记(4)——动态SQL 如果使用JDBC或者其他框架,很多时候需要你根据需求手动拼装SQL语句,这是一件非常麻烦的事情.MyBatis提供了对SQL语句动态的组装能力,而且他只有 ...

  4. MyBatis 3学习笔记

    MyBatis 3 一.MyBatis简介 优秀的持久层框架,支持支持自定义 SQL.存储过程以及高级映射,专注于SQL的编写. ​ 为什么不使用工具类进行数据库操作: ​ 功能简单,sql语句编写在 ...

  5. SQLServer学习笔记<>相关子查询及复杂查询

    二.查询缺少值的查询 在这里我们加入要查询2008年每一天的订单有多少?首先我们可以查询下订单表的订单日期在2008年的所有订单信息. 1 select distinct orderdate,coun ...

  6. Hibernate学习笔记-Hibernate HQL查询

    Session是持久层操作的基础,相当于JDBC中的Connection,通过Session会话来保存.更新.查找数据.session是Hibernate运作的中心,对象的生命周期.事务的管理.数据库 ...

  7. mybatis的学习笔记

    前几天学习了mybatis,今天来复习一下它的内容. mybatis是一个基于Java的持久层框架,那就涉及到数据库的操作.首先来提出第一个问题:java有jdbc连接数据库,我们为什么还要使用框架呢 ...

  8. springboot结合mybatis使用pageHelper插件进行分页查询

    1.pom相关依赖引入 <dependencies> <dependency> <groupId>org.springframework.boot</grou ...

  9. HIbernate学习笔记5 之 查询

    一.HQL查询 * 按条件查询,条件中写的是属性名,之后在query对象为添加赋值,如: String hql = " from User where uid=?"; Sessio ...

  10. Mybatis的ResultMap与limit分页查询

    ResultMap主要解决的是:属性名和字段不一致 如果在pojo中设置的是一个名字,在数据库上又是另一个名字,那么查询出来的结果或者其他操作的结果就为null. //在pojo中 private S ...

随机推荐

  1. mybatis - @MapperScan

    一. 测试代码 //实体类 public class User { private Integer id; private String name; private Integer age; priv ...

  2. CSS遮罩层简易写法

      现在很多站点弹出框,都需要一个遮罩层.写法很多,以下是我比较喜欢的一种: .box{ position:absolute; top:0; bottom:0; left:0; right:0; ba ...

  3. 初识eclipse-java

    开始时会有工程的地址需要设置,最好将程序放在一个单独的文件夹中 有时候会用到外部的驱动程序,如excel等,就需要导入jar包 具体的请看下篇博客.

  4. C++11 auto的用法

    参考博客:https://www.cnblogs.com/KunLunSu/p/7861330.html

  5. Cut Ribbon

    Polycarpus has a ribbon, its length is n. He wants to cut the ribbon in a way that fulfils the follo ...

  6. (c#)奇数值单元格的数量

    题目 解

  7. 【转载】Java的JVM原理

    转自:http://blog.csdn.net/witsmakemen/article/details/28600127/ 一.Java虚拟机的生命周期: Java虚拟机的生命周期 一个运行中的Jav ...

  8. Mongodb学习笔记(三)性能篇

    一.索引管理 MongoDB提供了多样性的索引支持,索引信息被保存在system.indexes中MongoDB中_id字段在创建的时候,默认已经建立了索引,这个索引比较特殊,并且不可以删除,不过Ca ...

  9. DVWA全级别之Brute Force(暴力破解)

    Brute Force Brute Force,即暴力(破解),是指黑客利用密码字典,使用穷举法猜解出用户口令. 首先我们登录DVWA(admin,password),之后我们看网络是否为无代理,: ...

  10. leetcode 25. K 个一组翻转链表

    # coding:utf-8 __author__ = "sn" """ 25. K 个一组翻转链表 给你一个链表,每 k 个节点一组进行翻转,请你返 ...