【Mybatis-Plus】单表、多表分页查询

文章介绍了如何使用MyBatis-Plus进行数据库的分页查询,包括通过注解和XML配置的方式,同时展示了带有条件和不带条件的查询方法。此外,还提及了如何进行多表查询,虽然MyBatis-Plus没有内置接口,但可以通过自定义SQL实现分页查询。
摘要由CSDN通过智能技术生成

前期准备

SQL脚本

/*
 Navicat Premium Data Transfer

 Source Server         : localhost
 Source Server Type    : MySQL
 Source Server Version : 50717
 Source Host           : localhost:3306
 Source Schema         : demo

 Target Server Type    : MySQL
 Target Server Version : 50717
 File Encoding         : 65001

 Date: 11/07/2023 17:08:38
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for dept
-- ----------------------------
DROP TABLE IF EXISTS `dept`;
CREATE TABLE `dept` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `dept_name` varchar(32) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4;

-- ----------------------------
-- Records of dept
-- ----------------------------
BEGIN;
INSERT INTO `dept` VALUES (1, '行政部');
INSERT INTO `dept` VALUES (2, '管理部');
COMMIT;

-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` varchar(32) DEFAULT NULL,
  `user_name` varchar(32) DEFAULT NULL,
  `create_time` datetime DEFAULT NULL,
  `dept_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4;

-- ----------------------------
-- Records of user
-- ----------------------------
BEGIN;
INSERT INTO `user` VALUES (5, 'tony', '哈喽啊', '2023-07-11 14:11:20', 1);
INSERT INTO `user` VALUES (6, 'halo1', '哈喽啊2', '2023-07-11 14:35:42', 2);
INSERT INTO `user` VALUES (7, 'halo3', '哈喽啊24', '2023-07-11 14:35:51', 1);
COMMIT;

SET FOREIGN_KEY_CHECKS = 1;


注意看执行的SQL

单表的分页查询

mybatis-plus 自带 selectPage
  • 携带条件查询
@Test
  public void selectPageAndParams() {
      IPage<User> page=new Page<>(1,10);
      userDao.selectPage(page,new QueryWrapper<User>().eq("user_id","tony"));
      System.out.println("当前页码值:"+page.getCurrent());
      System.out.println("每页显示数:"+page.getSize());
      System.out.println("一共多少页:"+page.getPages());
      System.out.println("一共多少条数据:"+page.getTotal());
      System.out.println("数据:"+page.getRecords());
  }
JsqlParserCountOptimize sql=SELECT  id,user_id,user_name,create_time,dept_id  FROM user 
 
 WHERE (user_id = ?)
==>  Preparing: SELECT COUNT(1) FROM user WHERE (user_id = ?) 
==> Parameters: tony(String)
<==    Columns: COUNT(1)
<==        Row: 1
==>  Preparing: SELECT id,user_id,user_name,create_time,dept_id FROM user WHERE (user_id = ?) LIMIT ?,? 
==> Parameters: tony(String), 0(Long), 10(Long)
<==    Columns: id, user_id, user_name, create_time, dept_id
<==        Row: 5, tony, 哈喽啊, 2023-07-11 14:11:20.0, 1
<==      Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@54e2fe]
当前页码值:1
每页显示数:10
一共多少页:1
一共多少条数据:1
数据:[User(id=5, userId=tony, userName=哈喽啊, createTime=Tue Jul 11 14:11:20 CST 2023, deptId=1)]
  • 不携带查询条件
 @Test
public void selectPage() {
        IPage<User> page=new Page<>(1,10);
        userDao.selectPage(page,null);
        System.out.println("当前页码值:"+page.getCurrent());
        System.out.println("每页显示数:"+page.getSize());
        System.out.println("一共多少页:"+page.getPages());
        System.out.println("一共多少条数据:"+page.getTotal());
        System.out.println("数据:"+page.getRecords());
        }
JsqlParserCountOptimize sql=SELECT  id,user_id,user_name,create_time,dept_id  FROM user
==>  Preparing: SELECT COUNT(1) FROM user 
==> Parameters: 
<==    Columns: COUNT(1)
<==        Row: 3
==>  Preparing: SELECT id,user_id,user_name,create_time,dept_id FROM user LIMIT ?,? 
==> Parameters: 0(Long), 10(Long)
<==    Columns: id, user_id, user_name, create_time, dept_id
<==        Row: 5, tony, 哈喽啊, 2023-07-11 14:11:20.0, 1
<==        Row: 6, halo1, 哈喽啊2, 2023-07-11 14:35:42.0, 2
<==        Row: 7, halo3, 哈喽啊24, 2023-07-11 14:35:51.0, 1
<==      Total: 3
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1981d861]
当前页码值:1
每页显示数:10
一共多少页:1
一共多少条数据:3
数据:[User(id=5, userId=tony, userName=哈喽啊, createTime=Tue Jul 11 14:11:20 CST 2023, deptId=1), 
User(id=6, userId=halo1, userName=哈喽啊2, createTime=Tue Jul 11 14:35:42 CST 2023, deptId=2), 
User(id=7, userId=halo3, userName=哈喽啊24, createTime=Tue Jul 11 14:35:51 CST 2023, deptId=1)]
注解配置

注意:${ew.customSqlSegment}是WHERE + sql语句

dao 层

    @Select(" select * from user ")
    IPage<User> queryUserByPageAnnotation(Page page);

    @Select(" select * from user ${ew.customSqlSegment} ")
    IPage<User> queryUserByPageAnnotationAndParams(Page page,@Param(Constants.WRAPPER) QueryWrapper<User> ew);

使用方式:

    @Test
    public void queryUserByPageAnnotation(){
        Page<User> page=new Page<>(1,5);
        userDao.queryUserByPageAnnotation(page);
        System.out.println("当前页码值:"+page.getCurrent());
        System.out.println("每页显示数:"+page.getSize());
        System.out.println("一共多少页:"+page.getPages());
        System.out.println("一共多少条数据:"+page.getTotal());
        System.out.println("数据:"+page.getRecords());
    }


    @Test
    public void queryUserByPageAnnotationAndParams(){
        Page<User> page=new Page<>(1,5);
        userDao.queryUserByPageAnnotationAndParams(page,new QueryWrapper<User>().eq("user_id","tony"));
        System.out.println("当前页码值:"+page.getCurrent());
        System.out.println("每页显示数:"+page.getSize());
        System.out.println("一共多少页:"+page.getPages());
        System.out.println("一共多少条数据:"+page.getTotal());
        System.out.println("数据:"+page.getRecords());
    }

测试的结果跟上面一致的哈

xml 配置

注意:${ew.customSqlSegment}是WHERE + sql语句

userDao.xml

  <select id="queryUserByPage" resultType="xy.mybatis.plus.entity.User">
      select * from user
  </select>

  <select id="queryUserMutlParamsByPage" resultType="xy.mybatis.plus.entity.User">
      select * from user ${ew.customSqlSegment}
  </select>

使用方式:

  @Test
    public void testQueryUserByPage(){
        Page<User> page=new Page<>(1,5);
        userDao.queryUserByPage(page);
        System.out.println("当前页码值:"+page.getCurrent());
        System.out.println("每页显示数:"+page.getSize());
        System.out.println("一共多少页:"+page.getPages());
        System.out.println("一共多少条数据:"+page.getTotal());
        System.out.println("数据:"+page.getRecords());
    }


    @Test
    public void queryUserMutlParamsByPage(){
        Page<User> page=new Page<>(1,5);
        userDao.queryUserMutlParamsByPage(page,new QueryWrapper<User>().eq("user_id","tony"));
        System.out.println("当前页码值:"+page.getCurrent());
        System.out.println("每页显示数:"+page.getSize());
        System.out.println("一共多少页:"+page.getPages());
        System.out.println("一共多少条数据:"+page.getTotal());
        System.out.println("数据:"+page.getRecords());
    }

测试的结果跟上面一致的哈

多表分页查询

多表查询的话,mybatis-plus 没有提供这样的内置接口 。
目前我们只能通过注解和XML的方式去实现 多表查询分页了。

注解

无条件查询 + 带条件查询

    @Select("  select t1.*,t2.dept_name from user t1 left join dept t2 on t1.dept_id = t2.id ")
    IPage<UserDeptVo> queryUserDeptByPageAnnotation(Page page);

    @Select("  select t1.*,t2.dept_name from user t1 left join dept t2 on t1.dept_id = t2.id  ${ew.customSqlSegment} ")
    IPage<UserDeptVo> queryUserDeptByPageAnnotationAndParams(Page page,@Param(Constants.WRAPPER) QueryWrapper<User> ew);

使用方式:

    @Test
    public void queryUserDeptByPageAnnotation(){
        Page<User> page=new Page<>(1,5);
        userDao.queryUserDeptByPageAnnotation(page);
        System.out.println("当前页码值:"+page.getCurrent());
        System.out.println("每页显示数:"+page.getSize());
        System.out.println("一共多少页:"+page.getPages());
        System.out.println("一共多少条数据:"+page.getTotal());
        System.out.println("数据:"+page.getRecords());
    }

    @Test
    public void queryUserDeptByPageAnnotationAndParams(){
        Page<User> page=new Page<>(1,5);
        userDao.queryUserDeptByPageAnnotationAndParams(page,new QueryWrapper<User>().eq("user_id","tony"));
        System.out.println("当前页码值:"+page.getCurrent());
        System.out.println("每页显示数:"+page.getSize());
        System.out.println("一共多少页:"+page.getPages());
        System.out.println("一共多少条数据:"+page.getTotal());
        System.out.println("数据:"+page.getRecords());
    }

测试结果, 就暂时不展示了,基本就是你想到的。

xml

无条件查询 + 带条件查询

<select id="queryUserDeptByPage" resultType="xy.mybatis.plus.vo.UserDeptVo">
    select t1.*,t2.dept_name from user t1 left join dept t2 on t1.dept_id = t2.id
</select>


<select id="queryUserDeptMutlParamsByPage" resultType="xy.mybatis.plus.vo.UserDeptVo">
    select t1.*,t2.dept_name from user t1 left join dept t2 on t1.dept_id = t2.id ${ew.customSqlSegment}
</select>

使用方式也不展示了,跟上面的一致哈。

测试结果, 就暂时不展示了,基本就是你想到的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值