MybatisPlus+Spring Boot3 分页查询实现

目录

导入依赖

[本文的house表 直接复制粘贴运行即可](#本文的house表 直接复制粘贴运行即可)

MybatisConfig配置文件

创建数据库对应的实体类

创建mapper层接口

在service包下创建xxxService接口

controller层创建XXXController类

完成分页查询


导入依赖

XML 复制代码
 <!--注意 SpringBoot3的依赖与Spring Boot2的MybatisPlus并不相同 错用会报错-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-spring-boot3-starter</artifactId>
            <version>3.5.5</version>
        </dependency>

本文的house表 直接复制粘贴运行即可

bash 复制代码
/*
 Navicat Premium Data Transfer

 Source Server         : localDB
 Source Server Type    : MySQL
 Source Server Version : 50743
 Source Host           : localhost:3306
 Source Schema         : property_management_db

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

 Date: 10/10/2024 16:05:51
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for house
-- ----------------------------
DROP TABLE IF EXISTS `house`;
CREATE TABLE `house`  (
  `id` int(6) UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT COMMENT '自增主键',
  `parent_building` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '所属楼宇',
  `parent_unit` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '所属单元',
  `parent_floor` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '所属楼层',
  `house_num` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '房间号',
  `house_size` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '房间大小',
  `house_type` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '房间户型',
  `is_sold` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '0' COMMENT '是否售出:0未售出1已售出',
  `user_id` int(6) UNSIGNED ZEROFILL NULL DEFAULT NULL COMMENT '住户ID(实际住户)',
  `owner_id` int(6) UNSIGNED ZEROFILL NULL DEFAULT NULL COMMENT '业主ID(实际房屋拥有者)',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `fk_house_info_user_user`(`user_id`) USING BTREE,
  INDEX `fk_house_info_owner_user`(`owner_id`) USING BTREE,
  CONSTRAINT `fk_house_info_owner_user` FOREIGN KEY (`owner_id`) REFERENCES `user` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,
  CONSTRAINT `fk_house_info_user_user` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE = InnoDB AUTO_INCREMENT = 127 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '住房详细信息 ' ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of house
-- ----------------------------
INSERT INTO `house` VALUES (000001, '1', '1', '1', '101', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000002, '1', '1', '1', '102', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000003, '1', '1', '2', '201', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000004, '1', '1', '2', '202', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000005, '1', '1', '3', '301', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000006, '1', '1', '3', '302', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000007, '1', '1', '4', '401', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000008, '1', '1', '4', '401', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000009, '1', '1', '5', '501', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000010, '1', '1', '5', '502', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000011, '1', '1', '6', '601', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000012, '1', '1', '6', '602', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000013, '1', '1', '7', '701', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000014, '1', '1', '7', '702', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000015, '1', '2', '1', '101', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000016, '1', '2', '1', '102', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000017, '1', '2', '2', '201', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000018, '1', '2', '2', '202', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000019, '1', '2', '3', '301', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000020, '1', '2', '3', '302', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000021, '1', '2', '4', '401', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000022, '1', '2', '4', '401', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000023, '1', '2', '5', '501', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000024, '1', '2', '5', '502', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000025, '1', '2', '6', '601', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000026, '1', '2', '6', '602', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000027, '1', '2', '7', '701', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000028, '1', '2', '7', '702', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000029, '1', '3', '1', '101', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000030, '1', '3', '1', '102', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000031, '1', '3', '2', '201', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000032, '1', '3', '2', '202', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000033, '1', '3', '3', '301', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000034, '1', '3', '3', '302', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000035, '1', '3', '4', '401', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000036, '1', '3', '4', '401', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000037, '1', '3', '5', '501', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000038, '1', '3', '5', '502', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000039, '1', '3', '6', '601', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000040, '1', '3', '6', '602', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000041, '1', '3', '7', '701', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000042, '1', '3', '7', '702', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000043, '2', '1', '1', '101', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000044, '2', '1', '1', '102', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000045, '2', '1', '2', '201', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000046, '2', '1', '2', '202', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000047, '2', '1', '3', '301', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000048, '2', '1', '3', '302', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000049, '2', '1', '4', '401', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000050, '2', '1', '4', '401', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000051, '2', '1', '5', '501', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000052, '2', '1', '5', '502', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000053, '2', '1', '6', '601', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000054, '2', '1', '6', '602', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000055, '2', '1', '7', '701', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000056, '2', '1', '7', '702', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000057, '2', '2', '1', '101', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000058, '2', '2', '1', '102', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000059, '2', '2', '2', '201', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000060, '2', '2', '2', '202', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000061, '2', '2', '3', '301', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000062, '2', '2', '3', '302', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000063, '2', '2', '4', '401', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000064, '2', '2', '4', '401', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000065, '2', '2', '5', '501', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000066, '2', '2', '5', '502', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000067, '2', '2', '6', '601', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000068, '2', '2', '6', '602', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000069, '2', '2', '7', '701', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000070, '2', '2', '7', '702', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000071, '2', '3', '1', '101', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000072, '2', '3', '1', '102', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000073, '2', '3', '2', '201', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000074, '2', '3', '2', '202', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000075, '2', '3', '3', '301', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000076, '2', '3', '3', '302', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000077, '2', '3', '4', '401', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000078, '2', '3', '4', '401', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000079, '2', '3', '5', '501', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000080, '2', '3', '5', '502', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000081, '2', '3', '6', '601', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000082, '2', '3', '6', '602', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000083, '2', '3', '7', '701', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000084, '2', '3', '7', '702', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000085, '3', '1', '1', '101', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000086, '3', '1', '1', '102', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000087, '3', '1', '2', '201', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000088, '3', '1', '2', '202', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000089, '3', '1', '3', '301', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000090, '3', '1', '3', '302', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000091, '3', '1', '4', '401', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000092, '3', '1', '4', '401', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000093, '3', '1', '5', '501', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000094, '3', '1', '5', '502', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000095, '3', '1', '6', '601', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000096, '3', '1', '6', '602', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000097, '3', '1', '7', '701', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000098, '3', '1', '7', '702', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000099, '3', '2', '1', '101', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000100, '3', '2', '1', '102', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000101, '3', '2', '2', '201', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000102, '3', '2', '2', '202', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000103, '3', '2', '3', '301', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000104, '3', '2', '3', '302', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000105, '3', '2', '4', '401', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000106, '3', '2', '4', '401', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000107, '3', '2', '5', '501', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000108, '3', '2', '5', '502', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000109, '3', '2', '6', '601', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000110, '3', '2', '6', '602', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000111, '3', '2', '7', '701', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000112, '3', '2', '7', '702', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000113, '3', '3', '1', '101', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000114, '3', '3', '1', '102', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000115, '3', '3', '2', '201', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000116, '3', '3', '2', '202', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000117, '3', '3', '3', '301', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000118, '3', '3', '3', '302', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000119, '3', '3', '4', '401', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000120, '3', '3', '4', '401', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000121, '3', '3', '5', '501', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000122, '3', '3', '5', '502', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000123, '3', '3', '6', '601', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000124, '3', '3', '6', '602', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000125, '3', '3', '7', '701', '120', '三室一厅一厨两卫', '0', NULL, NULL);
INSERT INTO `house` VALUES (000126, '3', '3', '7', '702', '120', '三室一厅一厨两卫', '0', NULL, NULL);

SET FOREIGN_KEY_CHECKS = 1;

MybatisConfig配置文件

添加MybatisPlusInterceptor代理对象,并添加分页插件到该代理对象。

java 复制代码
package com.xingdian.config;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MybatisConfig {


     /**
     * 为MyBatis Plus插件创建一个MybatisPlusInterceptor代理对象,
     * 并添加了分页插件(PaginationInnerInterceptor)到该代理对象中。这样,在应用中,
     * 只需要将MybatisPlusInterceptor对象注入到需要的地方,就可以轻松地启用MyBatis Plus插件的分页功能。
     */
    @Bean
    public MybatisPlusInterceptor paginationInterceptor(){
        //新建MybatisPlus拦截器
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        //新建分页拦截器paginationInnerInterceptor
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        //设置分页拦截器的一些属性
        paginationInnerInterceptor.setOverflow(true);
        paginationInnerInterceptor.setMaxLimit(100L);
        //把分页拦截器添加到MybatisPlus拦截器中
        mybatisPlusInterceptor.addInnerInterceptor(paginationInnerInterceptor);
        //添加组件,大功告成!
        return mybatisPlusInterceptor;
    }
}

创建数据库对应的实体类

java 复制代码
package com.xingdian.pojo;

import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

@Data
@TableName("house")
public class House {
    /**
     * 自增主键
     */
    private Integer id;

    /**
     * 所属楼宇
     */
    private String parentBuilding;

    /**
     * 所属单元
     */
    private String parentUnit;

    /**
     * 所属楼层
     */
    private String parentFloor;

    /**
     * 房间号
     */
    private String houseNum;

    /**
     * 房间大小
     */
    private String houseSize;

    /**
     * 房间户型
     */
    private String houseType;

    /**
     * 是否售出:0未售出1已售出
     */
    private String isSold;



    /**
     * 绑定的住户ID(已绑定也代表已售出)
     */
    private Integer userId;
    /**
     * 业主ID(如果售出则不可为空)
     */
    private String ownerId;

}

创建mapper层接口

在mapper包下创建操作user表的数据库接口

java 复制代码
package com.xingdian.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xingdian.pojo.House;
import com.xingdian.pojo.User;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface HouseMapper extends BaseMapper<House> {
}

在service包下创建xxxService接口

java 复制代码
package com.xingdian.service.impl;



import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xingdian.mapper.HouseMapper;
import com.xingdian.pojo.House;
import com.xingdian.service.IHouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HouseService extends ServiceImpl<HouseMapper, House> implements IHouseService {

    @Autowired
    private HouseMapper houseMapper;

    public IPage<House> getDataByPage(int current,int size, House house){
        IPage<House> page = new Page<>(current,size);
        QueryWrapper<House> queryWrapper = new QueryWrapper<>();
        // 动态构建查询条件
        queryWrapper.lambda().eq(house.getParentBuilding()!=null, House::getParentBuilding,house.getParentBuilding())
                .eq(house.getParentUnit()!=null,House::getParentUnit,house.getParentUnit())
                .eq(house.getParentFloor()!=null,House::getParentFloor,house.getParentFloor())
                .eq(house.getHouseNum()!=null,House::getHouseNum,house.getHouseNum())
                .eq(house.getHouseSize()!=null,House::getHouseSize,house.getHouseSize())
                .eq(house.getHouseType()!=null,House::getHouseType,house.getHouseType())
                .eq(house.getIsSold()!=null,House::getIsSold,house.getIsSold());
        return  houseMapper.selectPage(page,queryWrapper);

    }
}

在impl包下创建具体的业务层逻辑处理实现类

java 复制代码
package com.xingdian.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xingdian.entity.User;
import com.xingdian.mapper.UserMapper;
import com.xingdian.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService extends ServiceImpl<UserMapper,User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> getAllUsers() {
        return userMapper.selectList(null);
    }

    @Override
    public User getUserById(int id) {
        return userMapper.selectById(id);
    }


}

controller层创建XXXController类

java 复制代码
package com.xingdian.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xingdian.enums.ResponseStatusEnum;
import com.xingdian.pojo.House;
import com.xingdian.pojo.User;
import com.xingdian.pojo.dto.ResponseDTO;
import com.xingdian.service.impl.HouseService;
import com.xingdian.service.impl.UserService;
import lombok.extern.log4j.Log4j2;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

@Log4j2
@RestController
@RequestMapping("user")
public class HouseController {

    private final HouseService houseService;

    public HouseController(HouseService houseService) {
        this.houseService = houseService;
    }


    /**
     * @param house 分页查询条件
     * @param page 当前页
     * @param rows 当前页的数据条数
     * @return
     */
    @GetMapping("getDataByPage")
    public ResponseDTO<IPage> getDataByPage(House house, int page, int rows) {
        if (house == null || page<=0 || rows<=0)
            return ResponseDTO.failure(ResponseStatusEnum.PARAM_IS_INVALID);
        //支支持其他属性的条件分页查询
        house.setId(null);
        house.setOwnerId(null);
        house.setUserId(null);

        return ResponseDTO.success(houseService.getDataByPage(page,rows,house));
    }

}

完成分页查询

相关推荐
荆州克莱2 分钟前
mysql中局部变量_MySQL中变量的总结
spring boot·spring·spring cloud·css3·技术
zquwei15 分钟前
SpringCloudGateway+Nacos注册与转发Netty+WebSocket
java·网络·分布式·后端·websocket·网络协议·spring
dessler36 分钟前
Docker-run命令详细讲解
linux·运维·后端·docker
武昌库里写JAVA1 小时前
Java成长之路(一)--SpringBoot基础学习--SpringBoot代码测试
java·开发语言·spring boot·学习·课程设计
Q_19284999061 小时前
基于Spring Boot的九州美食城商户一体化系统
java·spring boot·后端
Q_19284999061 小时前
基于Spring Boot的营销项目系统
spring boot
ZSYP-S2 小时前
Day 15:Spring 框架基础
java·开发语言·数据结构·后端·spring
Yuan_o_2 小时前
Linux 基本使用和程序部署
java·linux·运维·服务器·数据库·后端
程序员一诺3 小时前
【Python使用】嘿马python高级进阶全体系教程第10篇:静态Web服务器-返回固定页面数据,1. 开发自己的静态Web服务器【附代码文档】
后端·python