项目初始化

  • image-20250521104315650

  • image-20250521104533066

  • pom.xml补全mybatis-plus的依赖(mybatis-plus-boot-starter)

    1
    2
    3
    4
    5
    <dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.5.1</version>
    </dependency>
  • 默认插件spring-boot-maven-plugin的skip属性改为false,否则打包的jar包无法运行

  • 修改resources下的application.yaml (当前mysql版本5.7)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    server:
    port: 8080

    spring:
    datasource:
    url: jdbc:mysql://localhost:3306/demo
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver

mybatis-plus集成

  • 项目结构

    image-20250521105545154

    • 实体类

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      package com.example.demo1.entity;


      import com.baomidou.mybatisplus.annotation.IdType;
      import com.baomidou.mybatisplus.annotation.TableId;
      import com.baomidou.mybatisplus.annotation.TableName;
      import lombok.Data;

      @Data
      @TableName("user")
      public class User {

      @TableId(type = IdType.AUTO)
      private Long id;

      private String username;
      private String password;
      }

    • mapper接口

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      package com.example.demo1.mapper;


      import com.baomidou.mybatisplus.core.mapper.BaseMapper;
      import com.example.demo1.entity.User;
      import org.springframework.stereotype.Repository;

      @Repository
      public interface UserMapper extends BaseMapper<User> {
      }

    • 数据库(id字段自增)

      image-20250521112601400

    • service接口

      1
      2
      3
      4
      5
      6
      7
      8
      package com.example.demo1.service;

      import com.baomidou.mybatisplus.extension.service.IService;
      import com.example.demo1.entity.User;

      public interface UserService extends IService<User> {
      }

    • service实现类

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      package com.example.demo1.service.impl;

      import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
      import com.example.demo1.entity.User;
      import com.example.demo1.mapper.UserMapper;
      import com.example.demo1.service.UserService;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Service;


      @Service
      public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

      @Autowired
      private UserMapper userMapper;
      }

    • 控制器

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      package com.example.demo1.controller;


      import com.example.demo1.entity.User;
      import com.example.demo1.service.UserService;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.web.bind.annotation.*;

      import java.util.List;

      @RestController
      @RequestMapping("/user")
      public class UserController {

      @Autowired
      private UserService userService;

      @GetMapping
      public List<User> findAll() {
      return userService.list();
      }

      @GetMapping("/{id}")
      public User findById(@PathVariable Long id) {
      return userService.getById(id);
      }

      @PostMapping
      public boolean save(@RequestBody User user) {
      return userService.save(user);
      }

      @PutMapping
      public boolean update(@RequestBody User user) {
      return userService.updateById(user);
      }

      @DeleteMapping("/{id}")
      public boolean delete(@PathVariable Long id) {
      return userService.removeById(id);
      }
      }

    • 启动类Application下添加MapperScan

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      package com.example.demo1;

      import org.mybatis.spring.annotation.MapperScan;
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;

      @SpringBootApplication
      @MapperScan("com.example.demo1.mapper")
      public class Demo1Application {

      public static void main(String[] args) {
      SpringApplication.run(Demo1Application.class, args);
      }
      }
    • 运行测试

      1
      2
      3
      http://127.0.0.1:8080/user/
      http://127.0.0.1:8080/user/1
      ...