简单的分页查询
步骤一:引入依赖
在项目的构建文件(例如Maven的pom.xml)中添加MyBatis-Plus的依赖:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.buba</groupId>
<artifactId>mybatisplus-maven</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.7.6</version>
</parent>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
步骤二:配置类
package com.buba.config;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConditionalOnClass({MybatisPlusInterceptor.class, BaseMapper.class})
public class MybatisConfig {
@Bean
@ConditionalOnMissingBean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);
// 设置最大限制为200
paginationInnerInterceptor.setMaxLimit(200L);
// 添加分页插件到拦截器链中
interceptor.addInnerInterceptor(paginationInnerInterceptor);
return interceptor;
}
}
步骤三:yml文件
server:
port: 8992
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai
username: root
password: 123456
步骤四:实体类
创建对应数据库表的实体类,并使用MyBatis-Plus的注解进行配置。例如,使用@TableName
注解指定表名,使用@TableId
注解指定主键。
package com.buba.damain.po;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import java.io.Serializable;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
/**
* <p>
*
* </p>
*
* @author bilal
* @since 2023-08-29
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("student")
public class Student implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
private String name;
private Integer age;
private Integer classId;
private Integer score;
}
表结构:
CREATE TABLE `student` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`age` int NOT NULL,
`class_id` int NULL DEFAULT NULL,
`score` int NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE,
INDEX `class_id`(`class_id` ASC) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 10 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
步骤五:Mapper接口
创建继承自MyBatis-Plus的BaseMapper
接口的自定义Mapper接口。该接口无需编写任何方法,MyBatis-Plus会根据实体类的注解自动生成基本的增删改查方法。
package com.buba.mapper;
import com.buba.damain.po.Student;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
* <p>
* Mapper 接口
* </p>
*
* @author bilal
* @since 2023-08-29
*/
public interface StudentMapper extends BaseMapper<Student> {
}
步骤六:service层
package com.buba.service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.buba.damain.po.Student;
import com.baomidou.mybatisplus.extension.service.IService;
public interface IStudentService extends IService<Student> {
Page<Student> queryStudent(Integer pageNo, Integer pageSize);
}
步骤七:分页查询
package com.buba.service.impl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.buba.damain.po.Student;
import com.buba.mapper.StudentMapper;
import com.buba.service.IStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {
@Override
public Page<Student> queryStudent(Integer pageNo, Integer pageSize) {
Page<Student> page = new Page<>(pageNo, pageSize);
return lambdaQuery()
.gt(Student::getScore,60)
.page(page);
}
}
步骤八:前端控制器
package com.buba.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.buba.damain.po.Student;
import com.buba.service.IStudentService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/student")
@RequiredArgsConstructor
public class StudentController {
private final IStudentService studentService;
@GetMapping("/page")
public Page<Student> queryStudent(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
return studentService.queryStudent(pageNo, pageSize);
}
}
步骤九:启动类
package com.buba;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.buba.mapper")
public class MybatisApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisApplication.class,args);
}
}
步骤十:测试
仓库地址一键跳转:https://gitee.com/Bilal-0/mybatisplus-maven