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;
相关推荐
cainiao0806052 小时前
《Spring Boot 4.0新特性深度解析》
java·spring boot·后端
呆萌很2 小时前
基于 Spring Boot 瑞吉外卖系统开发(十二)
spring boot
计算机学姐3 小时前
基于SpringBoot的小区停车位管理系统
java·vue.js·spring boot·后端·mysql·spring·maven
小鸡脚来咯3 小时前
请求参数:Header 参数,Body 参数,Path 参数,Query 参数分别是什么意思,什么样的,分别通过哪个注解获取其中的信息
java·spring boot·后端
添砖Java中4 小时前
深入剖析缓存与数据库一致性:Java技术视角下的解决方案与实践
java·数据库·spring boot·spring·缓存·双写一致性
幽络源小助理6 小时前
懒人美食帮SpringBoot订餐系统开发实现
java·spring boot·后端·美食
源码云商8 小时前
基于Spring Boot + Vue的母婴商城系统( 前后端分离)
java·spring boot·后端
还听珊瑚海吗12 小时前
基于SpringBoot的抽奖系统测试报告
java·spring boot·后端
*.✧屠苏隐遥(ノ◕ヮ◕)ノ*.✧15 小时前
MyBatis快速入门——实操
java·spring boot·spring·intellij-idea·mybatis·intellij idea
曼岛_17 小时前
[Java实战]Spring Boot 静态资源配置(十三)
java·开发语言·spring boot