<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;