一、搭建mybatisplus项目
参考博客地址 https://blog.csdn.net/only_foryou/article/details/156913310?spm=1001.2014.3001.5501
mybatis-plus 版本 3.5.8只需要增加配置类即可
java
@Configuration
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// 添加分页插件
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
page有多种方式这里给一种service方法
如:
java
@Override
public Page<Student> getPageByName(int pageNum, int pageSize, String name) {
return page(new Page<>(pageNum, pageSize), new LambdaQueryWrapper<Student>()
.eq(Student::getName, name));
}
二、entity
java
package com.qiu.entity;
import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import java.time.LocalDateTime;
/**
* 描述:学生实体类
*
* @author: qxd
* @date: 2025/11/29 22:09
* @version: 1.0.0
*/
@Data
// 指定表名
@TableName("student")
public class Student {
/**
* 学生ID
*/
@TableId(type = IdType.AUTO)
private Integer id;
/**
* 学生姓名
*/
private String name;
/**
* 年龄
*/
private Integer age;
/**
* 性别
*/
private String gender;
/**
* 邮箱
*/
private String email;
/**
* 电话
*/
private String phone;
/**
* 地址
*/
private String address;
/**
* 创建时间
*/
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;
/**
* 更新时间
*/
@TableField(fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updateTime;
}
**三、**dao
java
package com.qiu.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.qiu.entity.Student;
import org.apache.ibatis.annotations.Mapper;
/**
* 描述:学生数据访问接口
*
* @author: qxd
* @date: 2025/11/12 22:11
* @version: 1.0.0
*/
@Mapper
public interface StudentDao extends BaseMapper<Student> {
}
**四、**Service
java
package com.qiu.service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.qiu.entity.Student;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
/**
* 描述:
*
* @author: qxd
* @date: 2025/11/29 22:04
* @version: 1.0.0
*/
public interface StudentService extends IService<Student> {
public List<Student> getListByName(String name);
public Page<Student> getPageByName(@RequestParam(defaultValue = "1") int pageNum,
@RequestParam(defaultValue = "10") int pageSize,
@RequestParam("name") String name);
}
**五、**ServiceImpl
java
package com.qiu.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiu.dao.StudentDao;
import com.qiu.entity.Student;
import com.qiu.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* 描述:
*
* @author: qxd
* @date: 2025/11/19 22:04
* @version: 1.0.0
*/
@Slf4j
@Service
public class StudentServiceImpl extends ServiceImpl<StudentDao, Student> implements StudentService {
@Override
public List<Student> getListByName(String name) {
return list(new LambdaQueryWrapper<Student>().eq(Student::getName, name));
}
@Override
public Page<Student> getPageByName(int pageNum, int pageSize, String name) {
return page(new Page<>(pageNum, pageSize), new LambdaQueryWrapper<Student>()
.eq(Student::getName, name));
}
}
六、Controller
java
package com.qiu.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qiu.entity.Student;
import com.qiu.service.StudentService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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;
import java.util.List;
/**
* 描述:demo
*
* @author: qxd
* @date: 2025/11/29 21:54
* @version: 1.0.0
*/
@Slf4j
@RequestMapping("/student")
@RestController
public class StudentController {
@Autowired
private StudentService studentService;
@Operation(summary = "hello", description = "hello")
@GetMapping("/hello")
public String hello() {
log.info("hello world");
return "hello world";
}
@Operation(summary = "根据姓名查询学生列表", description = "根据姓名查询学生列表")
@GetMapping("/getListByName")
public List<Student> getListByName(@RequestParam("name") String name) {
return studentService.getListByName(name);
}
@Operation(summary = "根据名字分页查询", description = "根据姓名分页查询学生信息")
@GetMapping("/getPageByName")
public Page<Student> getPageByName(@RequestParam(defaultValue = "1") int pageNum,
@RequestParam(defaultValue = "10") int pageSize,
@RequestParam("name") String name) {
return studentService.getPageByName(pageNum, pageSize, name);
}
}
七、config
java
package com.qiu.config;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// 添加分页插件
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
八、项目地址
https://gitee.com/qiuxiaodong/demo
项目名称:demo-springboot-mybatisplus