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;
相关推荐
kfepiza12 分钟前
Spring 如何解决循环依赖 笔记251008
java·spring boot·spring
Arva .2 小时前
Spring Boot 配置文件
java·spring boot·后端
IT_Octopus2 小时前
https私人证书 PKIX path building failed 报错解决
java·spring boot·网络协议·https
风象南2 小时前
从RBAC到ABAC的进阶之路:基于jCasbin实现无侵入的SpringBoot权限校验
spring boot·后端
小蒜学长2 小时前
jsp基于JavaWeb的原色蛋糕商城的设计与实现(代码+数据库+LW)
java·开发语言·数据库·spring boot·后端
摇滚侠2 小时前
Spring Boot中使用线程池来优化程序执行的效率!笔记01
java·spring boot·多线程
尘觉11 小时前
中秋节与 Spring Boot 的思考:一场开箱即用的团圆盛宴
java·spring boot·后端
半旧夜夏13 小时前
【设计模式】核心设计模式实战
java·spring boot·设计模式
皮皮林55114 小时前
SpringBoot 控制台秒变炫彩特效,秀翻同事指南!
spring boot