springboot3.x+mybatisplus3.4.x+分页测试

复制代码
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.3.9</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<dependency>

<groupId>com.alibaba</groupId>

<artifactId>druid-spring-boot-starter</artifactId>

<version>1.2.6</version>

</dependency>

<!-- mybatis plus -->

<dependency>

<groupId>com.baomidou</groupId>

<artifactId>mybatis-plus-boot-starter</artifactId>

<version>3.5.5</version>

</dependency>

<dependency>

<groupId>com.baomidou</groupId>

<artifactId>mybatis-plus-extension</artifactId>

<version>3.5.5</version>

</dependency>

<dependency>

<groupId>com.baomidou</groupId>

<artifactId>mybatis-plus-boot-starter</artifactId>

<version>3.5.5</version>

</dependency>
分页拦截器

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

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

/**
 * mybatis-plus配置
 */
@Configuration
@EnableTransactionManagement
public class MybatisPlusConfig {
    /**
     * 添加分页插件
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        //构建拦截器
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 添加分页插件, 如果配置多个插件, 切记分页最后添加
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        // 如果有多数据源可以不配具体类型, 否则都建议配上具体的 DbType

        //2 添加乐观锁拦截器
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());

        return interceptor;
    }
}

测试类

java 复制代码
@Test
public void testPage() {
    //IPage<Emp> page = empService.page(null);
    IPage<Student> stu = new Page<Student>(1, 5);
    QueryWrapper<Student> queryWrapper = new QueryWrapper<Student>();
    IPage<Student> page = studentMapper.selectPage(stu, queryWrapper);
    //long total = page.getTotal();
    System.out.println("总条数:" + page.getTotal());
    System.out.println("总页数:" + page.getPages());
    System.out.println("每页显示多少条:" + page.getSize());
}

实体类

java 复制代码
package com.java199.entity;
import com.baomidou.mybatisplus.annotation.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author 汤老师 认认真真敲代码
 * @version 1.0
 * @description: TODO
 * @date 2025/3/19 9:04
 */
@Data
@TableName(value = "emp") //实体和表的映射,mybatisplus
@NoArgsConstructor
@AllArgsConstructor
public class Emp {
    //主键生成策略
    @TableId(type = IdType.AUTO)
    @TableField(value = "empno")
    private Integer empno;//员工编号
    private String ename;//员工名字
    private String job;//工作
    private Integer mgr;//经理编号
    private String hiredate;//入职时间
    private Double sal;//薪水
    private Double comm;//提成
    @TableField(value = "dept_no")
    private Integer deptno;//部门编号
    //transient短暂的 非持久的
    private transient String whoami;
    @TableField(exist = false)
    private String other;
    //标记此属性为version列对应的属性 和数据库中列要一致
    @Version
    private Integer version;
    //表,逻辑删除列对应的属性
    @TableLogic
    private Integer deleted;

}

表

sql 复制代码
DROP TABLE IF EXISTS `emp`;
CREATE TABLE `emp`  (
  `empno` int NOT NULL AUTO_INCREMENT COMMENT '员工编号',
  `ename` varchar(10) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '员工名字',
  `job` varchar(9) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '工作',
  `mgr` decimal(4, 0) NULL DEFAULT NULL COMMENT '经理编号',
  `hiredate` datetime NULL DEFAULT NULL COMMENT '入职时间',
  `sal` decimal(7, 2) NULL DEFAULT NULL COMMENT '薪水',
  `comm` decimal(7, 2) NULL DEFAULT NULL COMMENT '提成',
  `dept_no` int NULL DEFAULT NULL COMMENT '部门编号',
  `version` int NULL DEFAULT NULL COMMENT '乐观锁',
  `deleted` tinyint NULL DEFAULT NULL COMMENT ' 逻辑删除',
  PRIMARY KEY (`empno`) USING BTREE,
  INDEX `FK_DEPTNO`(`dept_no` ASC) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 7941 CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = DYNAMIC;

SET FOREIGN_KEY_CHECKS = 1;
相关推荐
paopaokaka_luck42 分钟前
基于springboot+vue的智慧家政管理系统(AI问答、 WebSocket实时沟通、ECharts运营分析、订单规则与服务追踪)
java·javascript·spring boot·数据分析·echarts·mybatis
阿森_dev1 小时前
2027 届毕设选题怎么选:30 个 Java 方向选题的实现难度排序
java·开发语言·spring boot·毕业设计·课程设计
计算机毕业设计杰瑞1 小时前
【计算机毕设精品】基于SpringBoot的一体化教务与学业质量管理系统的设计与实现,附源码_增删改查_java项目_可完美运行部署_网站项目
java·spring boot·课程设计
大侠归来3 小时前
Spring Boot 2.1 → 3.5 迁移推演:从实战出发的完整路线图
java·spring boot·后端
Sweet锦4 小时前
jDCS 开源项目:面向工业现场的 Modbus RTU 数据采集基础框架
java·spring boot·物联网·开源
海马4 小时前
Spring Boot 开发知识整理
java·spring boot·后端
谢亮_vipxieliang4 小时前
Spring Boot 3.x 从零开始——环境搭建与第一个 REST 项目
java·spring boot·后端
FYKJ_20104 小时前
django个性化新闻推荐35173-计算机课程设计、毕业设计
java·spring boot·后端·python·架构·django·课程设计
今年下半年4 小时前
【消息队列】Spring Boot 集成 Kafka 消息队列:确保消费完成(手动提交偏移量+幂等判断+失败重试机制)
spring boot·kafka·幂等·失败重试
计算机毕设定制辅导-无忧学长4 小时前
《校园食堂在线订餐小程序的设计与实现》
java·vue.js·spring boot·微信小程序·uniapp·校园食堂在线订餐小程序