通过 Quick start (https://www.yuque.com/jaded/nql9hd/xzygza#ObqBi) 的样例代码可以看到,只要 Mapper接口 继承了 Mybatis-Plus 提供的 BaseMapper ,就具备了增删改查的功能。(这点我不是很理解)

但实现上最好不要这么干。

文件目录结构

image.png

数据库结构

image.png

1、前置步骤

1.1、引入 MyBatis-Plus

  1. <dependency>
  2. <groupId>com.baomidou</groupId>
  3. <artifactId>mybatis-plus-boot-starter</artifactId>
  4. <version>3.4.2</version>
  5. </dependency>

image.png

1.2、配置 @MapperScan 注解

MapperScan 注解的路径必须精准目录的,不能用大范围的包来代替。

  1. package com.example.boot;
  2. import org.mybatis.spring.annotation.MapperScan;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. @MapperScan("com.example.boot.mapper")
  6. @SpringBootApplication
  7. public class SpringBootApiTest01Application {
  8. public static void main(String[] args) {
  9. SpringApplication.run(SpringBootApiTest01Application.class, args);
  10. }
  11. }

如果有多个 Mapper 目录,可以用 @MapperScans 注解来代替:

  1. package com.example.boot;
  2. import org.mybatis.spring.annotation.MapperScan;
  3. import org.mybatis.spring.annotation.MapperScans;
  4. import org.springframework.boot.SpringApplication;
  5. import org.springframework.boot.autoconfigure.SpringBootApplication;
  6. @MapperScans({
  7. @MapperScan("com.example.boot.mapper"),
  8. @MapperScan("com.example.boot.a.mapper"),
  9. @MapperScan("com.example.boot.b.mapper"),
  10. })
  11. //@MapperScan("com.example.boot.mapper")
  12. @SpringBootApplication
  13. public class SpringBootApiTest01Application {
  14. public static void main(String[] args) {
  15. SpringApplication.run(SpringBootApiTest01Application.class, args);
  16. }
  17. }

1.3、编写 Bean

使用 lombok 插件可以简化代码

Role

  1. package com.example.boot.bean;
  2. import com.baomidou.mybatisplus.annotation.TableName;
  3. import lombok.Data;
  4. @Data
  5. @TableName("t_s_role")
  6. public class Role {
  7. private String id;
  8. private String rolecode;
  9. private String rolename;
  10. private String role_type;
  11. }

1.4、编写 Mapper 接口

继承 BaseMapper
RoleMapper

  1. package com.example.boot.mapper;
  2. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  3. import com.example.boot.bean.Role;
  4. public interface RoleMapper extends BaseMapper<Role> {
  5. }

1.5、编写 Service 接口

extends IService

  1. package com.example.boot.service;
  2. import com.baomidou.mybatisplus.extension.service.IService;
  3. import com.example.boot.bean.Role;
  4. public interface RoleService extends IService<Role> {
  5. }

1.6、编写 Service 接口实现类

extends ServiceImpl implements RoleService

  1. package com.example.boot.service.impl;
  2. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  3. import com.example.boot.bean.Role;
  4. import com.example.boot.mapper.RoleMapper;
  5. import com.example.boot.service.RoleService;
  6. import org.springframework.stereotype.Service;
  7. @Service
  8. public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
  9. }

1.7、编写 controller

与 Qucik start (https://www.yuque.com/jaded/nql9hd/xzygza#Ec3kR)中不同的是,注入的组件 Service 实现类替换掉了原来的 Mapper。(第19行)

  1. package com.example.boot.controller;
  2. import com.example.boot.response.Response;
  3. import com.example.boot.service.RoleService;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.web.bind.annotation.GetMapping;
  7. import org.springframework.web.bind.annotation.RequestParam;
  8. import org.springframework.web.bind.annotation.RestController;
  9. @RestController
  10. @Slf4j
  11. public class RoleController extends BaseRestController {
  12. public RoleController() {
  13. log.info("RoleController 构造函数被调用了");
  14. }
  15. @Autowired
  16. private RoleService roleService;
  17. @GetMapping(path = {
  18. "/",
  19. })
  20. public Response role(@RequestParam(name = "id", defaultValue = "") String id) {
  21. if (id == null || id.length() <= 0) {
  22. // 查全部 role
  23. return success("ok", roleService.list());
  24. } else {
  25. // 查单个 role
  26. return success("ok", roleService.getById(id));
  27. }
  28. }
  29. }

1.8、开启分页插件

官网文档:https://baomidou.com/guide/page.html
新建一个 MybatisConfig ,按照官网的提示加入分页插件。

  1. package com.example.boot.config;
  2. import com.baomidou.mybatisplus.annotation.DbType;
  3. import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
  4. import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. @Configuration
  8. public class MybatisConfig {
  9. @Bean
  10. public MybatisPlusInterceptor mybatisPlusInterceptor() {
  11. MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
  12. // 分页拦截器
  13. PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);
  14. // 当查询到最后一页时,继续点下一页,是要循环到首页去,还是继续请求。 true:循环到首页去 false:继续请求 默认为false
  15. paginationInnerInterceptor.setOverflow(false);
  16. // 设置 page size 的最大值
  17. paginationInnerInterceptor.setMaxLimit(1000L);
  18. interceptor.addInnerInterceptor(paginationInnerInterceptor);
  19. return interceptor;
  20. }
  21. }

2、查询

2.1、简单查询

如果没有传递 id 参数,则返回所有角色数据。

控制器方法:

  1. @GetMapping(path = {
  2. "/role",
  3. })
  4. public Response role(@RequestParam(name = "id", defaultValue = "") String id) {
  5. if (id == null || id.length() <= 0) {
  6. // 查全部 role
  7. return success("ok", roleService.list());
  8. } else {
  9. // 查单个 role
  10. return success("ok", roleService.getById(id));
  11. }
  12. }

单个
image.png

列表
image.png

2.2、分页查询

改造一下 role 方法,没有传递 id 时,进行分页查询,默认第1页,每页2条记录,并返回页码信息。

控制器方法:

  1. @GetMapping(path = {
  2. "/role",
  3. })
  4. public Response role(
  5. @RequestParam(name = "id", defaultValue = "") String id,
  6. @RequestParam(name = "page", defaultValue = "1") Integer page,
  7. @RequestParam(name = "pageSize", defaultValue = "2") Integer pageSize
  8. ) {
  9. if (id == null || id.length() <= 0) {
  10. // 查全部 role
  11. // 分页对象
  12. Page<Role> rolePage = new Page<Role>(page, pageSize);
  13. // 分页查询的结果
  14. Page<Role> roles = roleService.page(rolePage, null);
  15. long current = roles.getCurrent();
  16. long pages = roles.getPages();
  17. long total = roles.getTotal();
  18. Map<String, Object> map = new HashMap<>();
  19. map.put("current", current);
  20. map.put("pages", pages);
  21. map.put("total", total);
  22. map.put("data", roles.getRecords());
  23. return success("ok", map);
  24. } else {
  25. // 查单个 role
  26. return success("ok", roleService.getById(id));
  27. }
  28. }

image.png

3、新增

3.1、单条新增

这里新增要传递 id 参数是因为 tole 表的 id 字段不是数据库自增 id 。

控制器方法:

  1. @GetMapping(path = {
  2. "/role/add",
  3. })
  4. public Response addRole(
  5. @RequestParam(name = "id", required = true) String id,
  6. @RequestParam(name = "rolecode", defaultValue = "000567") String rolecode,
  7. @RequestParam(name = "rolename", defaultValue = "测试角色") String rolename,
  8. @RequestParam(name = "role_type", defaultValue = "0") String role_type
  9. ) {
  10. Role role = new Role();
  11. role.setId(id);
  12. role.setRolecode(rolecode);
  13. role.setRolename(rolename);
  14. role.setRole_type(role_type);
  15. boolean success = roleService.save(role);
  16. if (success) {
  17. return success("新增角色成功");
  18. } else {
  19. return error("新增角色失败");
  20. }
  21. }

image.png
image.png

3.2、批量新增

4、删除

4.1、单条删除

输出删除结果。

  1. @GetMapping(path = {
  2. "/role/delete",
  3. })
  4. public Response deleteRole(
  5. @RequestParam(name = "id", defaultValue = "") String id
  6. ) {
  7. boolean success = roleService.removeById(id);
  8. if (success) {
  9. return success("删除成功");
  10. } else {
  11. return error("删除失败");
  12. }
  13. }

image.png

4.2、批量删除

5、编辑

5.1、单条编辑

控制器:

  1. @GetMapping(path = {
  2. "/role/edit",
  3. })
  4. public Response editRole(
  5. @RequestParam(name = "id", defaultValue = "") String id,
  6. @RequestParam(name = "rolecode", defaultValue = "000567") String rolecode,
  7. @RequestParam(name = "rolename", defaultValue = "测试角色") String rolename,
  8. @RequestParam(name = "role_type", defaultValue = "0") String role_type
  9. ) {
  10. Role role = new Role();
  11. role.setId(id);
  12. role.setRolecode(rolecode);
  13. role.setRolename(rolename);
  14. role.setRole_type(role_type);
  15. QueryWrapper<Role> wrapper = new QueryWrapper<>();
  16. wrapper.eq("id", id);
  17. boolean success = roleService.update(role, wrapper);
  18. if (success) {
  19. return success("编辑角色成功");
  20. } else {
  21. return error("便捷角色失败");
  22. }
  23. }

image.png
image.png

5.2、批量编辑