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;
相关推荐
北风toto6 小时前
Spring Boot 常用注解 · 概要笔记
spring boot·笔记·后端
宋冠巡6 小时前
Spring Boot 4 + OpenTelemetry + Spring Cloud Gateway:全链路 TraceId 传播验证
spring boot·可观测性·traceid
下一站春风里6 小时前
Spring IoC 控制反转 完整技术笔记
java·spring boot·笔记·spring
小码哥0687 小时前
Spring Boot在线租房系统源码:UniApp找房、VR展示与预约管理落地解析
spring boot·租房小程序·租房系统·在线租房
芜名氏7 小时前
API 文档生成工具——SpringDoc
java·spring boot·ci/cd·设计规范
乌暮1 天前
Spring AOP 与 Spring 事务:用大白话彻底理解原理与实践
java·数据库·spring boot·后端·sql·学习·spring
小荷才露尖尖角,早有蜻蜓立上头1 天前
SpringBoot+QQ邮箱发送邮件
spring boot
头发还在的女程序员1 天前
Java在线租房系统源码解析:Spring Boot+Vue 3+UniApp房源管理与预约看房方案
java·vue.js·spring boot·租房小程序·租房系统·租房软件
FYKJ_20101 天前
springboot同人周边订单管理系统48955-计算机毕业设计、课程设计
java·javascript·vue.js·spring boot·django·php·课程设计
yuyuxun12 天前
协同过滤的校园二手物品智能推荐系统的设计与实现 毕业设计-附源码40082
javascript·vue.js·spring boot·python·数据分析·django·课程设计