SpringBoot2项目集成mybatisplus
一、新建SpringBoot2项目并引入mybatis-plus依赖包
版本使用3.4.0
com.baomidou
mybatis-plus-boot-starter
3.4.0
完整pom依赖
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.7.10
com.qiu
demo-springboot-mybatisplus02
1.0-SNAPSHOT
SpringBoot集成MybatisPlus
8
8
8
3.4.0
2.8.14
org.springframework.boot
spring-boot-starter-web
com.mysql
mysql-connector-j
runtime
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
org.springdoc
springdoc-openapi-ui
1.8.0
com.baomidou
mybatis-plus-boot-starter
${mybatisplus.version}
org.apache.maven.plugins
maven-clean-plugin
二、yml配置
# mybatis-plus
mybatis-plus:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.qiu.entity
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
完整配置
server:
port: 8089
spring:
application:
name: demo-springboot-mybatisplus02
# 数据库连接池
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.2:3306/demo?useSSL=false&serverTimezone=UTC
username: root
password: 123456
# mybatis-plus
mybatis-plus:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.qiu.entity
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
# 日志
logging:
level:
com.qiu.dao: debug
org.mybatis: debug
三、启动类
package com.qiu;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 描述:启动类
*
* @author: qxd
* @date: 2026/01/05 22:09
* @version: 1.0.0
*/
@SpringBootApplication
@MapperScan("com.qiu.dao")
public class DemoSpringBootMybatisPlus02Application {
public static void main(String[] args) {
SpringApplication.run(DemoSpringBootMybatisPlus02Application.class, args);
}
}
四、entity
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
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 {
}
六、Service
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 java.util.List;
/**
* 描述:学生服务接口
*
* @author: qxd
* @date: 2025/11/29 22:04
* @version: 1.0.0
*/
public interface StudentService extends IService {
/**
* 根据姓名获取学生列表
*/
List getListByName(String name);
/**
* 根据姓名分页查询学生
*/
Page getPageByName(int pageNum, int pageSize, String name);
/**
* 新增学生
*/
boolean addStudent(Student student);
/**
* 批量新增学生
*/
boolean addStudents(List students);
/**
* 根据ID删除学生
*/
boolean deleteById(Long id);
/**
* 根据ID批量删除学生
*/
boolean deleteBatchIds(List ids);
/**
* 根据条件删除学生
*/
boolean deleteByCondition(String name);
/**
* 更新学生信息
*/
boolean updateStudent(Student student);
/**
* 批量更新学生信息
*/
boolean updateStudents(List students);
/**
* 条件更新学生信息
*/
boolean updateByCondition(Student student, String oldName);
/**
* 根据ID查询学生
*/
Student getStudentById(Long id);
/**
* 根据条件查询单个学生
*/
Student getOneStudent(String name);
/**
* 查询所有学生
*/
List getAllStudents();
/**
* 分页查询所有学生
*/
Page getPageList(int pageNum, int pageSize);
/**
* 统计学生数量
*/
long countAll();
/**
* 根据姓名统计学生数量
*/
long countByName(String name);
/**
* 判断学生是否存在
*/
boolean existsById(Long id);
}
七、ServiceImpl
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 implements StudentService {
@Override
public List getListByName(String name) {
return list(new LambdaQueryWrapper().eq(Student::getName, name));
}
@Override
public Page getPageByName(int pageNum, int pageSize, String name) {
return page(new Page<>(pageNum, pageSize), new LambdaQueryWrapper()
.eq(Student::getName, name));
}
/**
* 新增学生
*/
@Override
public boolean addStudent(Student student) {
return save(student);
}
/**
* 批量新增学生
*/
@Override
public boolean addStudents(List students) {
return saveBatch(students);
}
/**
* 根据ID删除学生
*/
@Override
public boolean deleteById(Long id) {
return removeById(id);
}
/**
* 根据ID批量删除学生
*/
@Override
public boolean deleteBatchIds(List ids) {
return removeByIds(ids);
}
/**
* 根据条件删除学生
*/
@Override
public boolean deleteByCondition(String name) {
return remove(new LambdaQueryWrapper().eq(Student::getName, name));
}
/**
* 更新学生信息
*/
@Override
public boolean updateStudent(Student student) {
return updateById(student);
}
/**
* 批量更新学生信息
*/
@Override
public boolean updateStudents(List students) {
return updateBatchById(students);
}
/**
* 条件更新学生信息
*/
@Override
public boolean updateByCondition(Student student, String oldName) {
return update(student, new LambdaQueryWrapper().eq(Student::getName, oldName));
}
/**
* 根据ID查询学生
*/
@Override
public Student getStudentById(Long id) {
return getById(id);
}
/**
* 根据条件查询单个学生
*/
@Override
public Student getOneStudent(String name) {
return getOne(new LambdaQueryWrapper().eq(Student::getName, name));
}
/**
* 查询所有学生
*/
@Override
public List getAllStudents() {
return list();
}
/**
* 分页查询所有学生
*/
@Override
public Page getPageList(int pageNum, int pageSize) {
return page(new Page<>(pageNum, pageSize));
}
/**
* 统计学生数量
*/
@Override
public long countAll() {
return count();
}
/**
* 根据条件统计学生数量
*/
@Override
public long countByName(String name) {
return count(new LambdaQueryWrapper().eq(Student::getName, name));
}
/**
* 判断学生是否存在
*/
@Override
public boolean existsById(Long id) {
return getById(id) != null;
}
}
八、Controller
package com.qiu.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qiu.common.Result;
import com.qiu.entity.Student;
import com.qiu.service.StudentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
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 getListByName(@RequestParam("name") String name) {
return studentService.getListByName(name);
}
@Operation(summary = "根据名字分页查询", description = "根据姓名分页查询学生信息")
@GetMapping("/getPageByName")
public Page getPageByName(@RequestParam(defaultValue = "1") int pageNum,
@RequestParam(defaultValue = "10") int pageSize,
@RequestParam("name") String name) {
return studentService.getPageByName(pageNum, pageSize, name);
}
@Operation(summary = "新增学生", description = "添加新的学生信息")
@PostMapping("/add")
public Result addStudent(@RequestBody Student student) {
boolean result = studentService.addStudent(student);
return Result.success(result);
}
@Operation(summary = "批量新增学生", description = "批量添加学生信息")
@PostMapping("/batchAdd")
public Result addStudents(@RequestBody List students) {
boolean result = studentService.addStudents(students);
return Result.success(result);
}
@Operation(summary = "根据ID删除学生", description = "根据学生ID删除学生信息")
@DeleteMapping("/delete/{id}")
public Result deleteById(@PathVariable Long id) {
boolean result = studentService.deleteById(id);
return Result.success(result);
}
@Operation(summary = "根据ID批量删除学生", description = "根据多个学生ID批量删除学生信息")
@Parameter(name = "ids", description = "学生ID列表,用逗号分隔,如:1,2,3",
required = true, schema = @Schema(type = "string"))
@DeleteMapping("/batchDelete")
public Result deleteBatchIds(@RequestParam List ids) {
boolean result = studentService.deleteBatchIds(ids);
return Result.success(result);
}
@Operation(summary = "根据条件删除学生", description = "根据姓名删除学生信息")
@DeleteMapping("/deleteByCondition")
public Result deleteByCondition(@RequestParam String name) {
boolean result = studentService.deleteByCondition(name);
return Result.success(result);
}
@Operation(summary = "更新学生信息", description = "更新学生的基本信息")
@PutMapping("/update")
public Result updateStudent(@RequestBody Student student) {
boolean result = studentService.updateStudent(student);
return Result.success(result);
}
@Operation(summary = "批量更新学生信息", description = "批量更新学生信息")
@PutMapping("/batchUpdate")
public Result updateStudents(@RequestBody List students) {
boolean result = studentService.updateStudents(students);
return Result.success(result);
}
@Operation(summary = "条件更新学生信息", description = "根据旧姓名更新学生信息")
@PutMapping("/updateByCondition")
public Result updateByCondition(@RequestBody Student student,
@RequestParam String oldName) {
boolean result = studentService.updateByCondition(student, oldName);
return Result.success(result);
}
@Operation(summary = "根据ID查询学生", description = "根据学生ID获取学生详细信息")
@GetMapping("/getById/{id}")
public Result getStudentById(@PathVariable Long id) {
Student student = studentService.getStudentById(id);
return Result.success(student);
}
@Operation(summary = "根据条件查询单个学生", description = "根据姓名查询单个学生信息")
@GetMapping("/getOne")
public Result getOneStudent(@RequestParam String name) {
Student student = studentService.getOneStudent(name);
return Result.success(student);
}
@Operation(summary = "查询所有学生", description = "获取所有学生信息")
@GetMapping("/getAll")
public Result> getAllStudents() {
List students = studentService.getAllStudents();
return Result.success(students);
}
@Operation(summary = "分页查询所有学生", description = "分页获取所有学生信息")
@GetMapping("/getPageList")
public Result> getPageList(@RequestParam(defaultValue = "1") int pageNum,
@RequestParam(defaultValue = "10") int pageSize) {
Page pageResult = studentService.getPageList(pageNum, pageSize);
return Result.success(pageResult);
}
@Operation(summary = "统计学生总数", description = "获取学生总数量")
@GetMapping("/countAll")
public Result countAll() {
Long count = studentService.countAll();
return Result.success(count);
}
@Operation(summary = "根据姓名统计学生数量", description = "根据姓名统计学生数量")
@GetMapping("/countByName")
public Result countByName(@RequestParam String name) {
Long count = studentService.countByName(name);
return Result.success(count);
}
@Operation(summary = "判断学生是否存在", description = "根据ID判断学生是否存在")
@GetMapping("/exists/{id}")
public Result existsById(@PathVariable Long id) {
boolean exists = studentService.existsById(id);
return Result.success(exists);
}
}
九、访问链接
http://127.0.0.1:8089/swagger-ui/index.html

十、项目地址
https://gitee.com/qiuxiaodong/demo
项目名称:demo-springboot-mybatisplus02






