🍁 作者:知识浅谈,CSDN签约讲师,CSDN博客专家,华为云云享专家,阿里云专家博主 📌 擅长领域:全栈工程师、爬虫、ACM算法

(🤞Spring Boot 整合 MyBatis-Plus使用教程🤞)

🎈环境准备

  • JDK: 确保你已经安装了 JDK 1.8 或更高版本。
  • IDE: 推荐使用 IntelliJ IDEA 或者 Eclipse。
  • Maven: 本教程使用 Maven 作为构建工具。

🎈创建 Spring Boot 项目

可以使用 Spring Initializr(https://start.spring.io)创建一个 Spring Boot 项目。选择的依赖项包括:

  • Spring Web
  • MyBatis-Plus Boot Starter
  • MySQL Driver(或者你使用的其他数据库驱动)

🎈配置 pom.xml

在你的 pom.xml 中添加以下依赖项:

<dependencies>
    <!-- Spring Boot Starter -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    
    <!-- Spring Boot Starter Web -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    <!-- MyBatis-Plus Starter -->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
    </dependency>
    
    <!-- MySQL Driver (根据实际情况选择) -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    
    <!-- Spring Boot Starter Test -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

🎈配置 application.yml

在 src/main/resources/application.yml 中添加数据库配置:

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/your_database
    username: your_username
    password: your_password
    driver-class-name: com.mysql.cj.jdbc.Driver
  mybatis-plus:
    # 配置 MyBatis-Plus 的全局配置
    global-config:
      db-config:
        id-type: auto  # 主键策略

如果使用 application.properties,配置如下:

spring.datasource.url=jdbc:mysql://localhost:3306/your_database
spring.datasource.username=your_username
spring.datasource.password=your_password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

mybatis-plus.global-config.db-config.id-type=auto

🎈创建实体类

在 src/main/java/com/example/demo/entity 目录下创建一个实体类,例如 User:

package com.example.demo.entity;

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

@Data
@TableName("user")
public class User {
    @TableId(type = IdType.AUTO)
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

🎈创建 Mapper 接口

在 src/main/java/com/example/demo/mapper 目录下创建一个 Mapper 接口,例如 UserMapper:

package com.example.demo.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.entity.User;

public interface UserMapper extends BaseMapper<User> {
}

🎈创建 Service 和 Controller

创建 UserService 和 UserController: UserService:

package com.example.demo.service;

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

public interface UserService extends IService<User> {
}

UserServiceImpl:

package com.example.demo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import org.springframework.stereotype.Service;

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

UserController:

package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/users")
    public User createUser(@RequestBody User user) {
        userService.save(user);
        return user;
    }

    @GetMapping("/users")
    public List<User> getAllUsers() {
        return userService.list();
    }
}

🎈启动应用

创建主类并启动 Spring Boot 应用:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

启动 Spring Boot 应用并测试 API 端点,例如使用 Postman 或 cURL 测试 POST /users 和 GET /users。

这样,你就完成了 Spring Boot 和 MyBatis-Plus 的整合。通过 MyBatis-Plus,你可以享受到更简洁的 CRUD 操作和更强大的功能扩展

🍚总结

大功告成,撒花致谢🎆🎇🌟,关注我不迷路,带你起飞带你富。 作者:知识浅谈