springBoot中myBatisPlus的使用

MyBatis-Plus 是一个 MyBatis 的增强工具,在 Spring Boot 项目里使用它能极大提升开发效率。下面为你详细介绍在 Spring Boot 中使用 MyBatis-Plus 的步骤以及示例代码。

1. 创建 Spring Boot 项目

你可以借助 Spring Initializr(https://start.spring.io/)来创建一个新的 Spring Boot 项目,添加以下依赖:

  • Spring Web
  • Spring Data JPA(虽然用 MyBatis-Plus,但这个依赖可按需添加)
  • MyBatis Framework
  • MySQL Driver(假设使用 MySQL 数据库)

2. 添加 MyBatis-Plus 依赖

在 pom.xml 里添加 MyBatis-Plus 的依赖:

xml 复制代码
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.5.3.1</version>
</dependency>

3. 配置数据库连接

在 application.properties 或 application.yml 中配置数据库连接信息:

properties 复制代码
spring.datasource.url=jdbc:mysql://localhost:3306/your_database_name
spring.datasource.username=your_username
spring.datasource.password=your_password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

4. 创建实体类

创建一个简单的实体类,例如 User:

java 复制代码
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

@TableName("user")
public class User {
    @TableId
    private Long id;
    private String name;
    private Integer age;

    // Getters and Setters
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

5. 创建 Mapper 接口

创建一个继承自 BaseMapper 的 Mapper 接口:

java 复制代码
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.springframework.stereotype.Repository;

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

6. 创建 Service 层

创建一个 Service 接口及其实现类:

java 复制代码
import java.util.List;

public interface UserService {
    List<User> getAllUsers();
    User getUserById(Long id);
    boolean saveUser(User user);
    boolean updateUser(User user);
    boolean deleteUser(Long id);
}
java 复制代码
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> getAllUsers() {
        return userMapper.selectList(null);
    }

    @Override
    public User getUserById(Long id) {
        return userMapper.selectById(id);
    }

    @Override
    public boolean saveUser(User user) {
        return userMapper.insert(user) > 0;
    }

    @Override
    public boolean updateUser(User user) {
        return userMapper.updateById(user) > 0;
    }

    @Override
    public boolean deleteUser(Long id) {
        return userMapper.deleteById(id) > 0;
    }
}

7. 创建 Controller 层

创建一个 Controller 来处理 HTTP 请求:

java 复制代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

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

    @Autowired
    private UserService userService;

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

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

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

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

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

8. 启动应用

启动 Spring Boot 应用后,你就能通过以下 API 来操作 User 数据:

  • GET /users:获取所有用户信息。
  • GET /users/{id}:根据 ID 获取用户信息。
  • POST /users:新增用户。
  • PUT /users:更新用户信息。
  • DELETE /users/{id}:根据 ID 删除用户。

按照以上步骤,你就能在 Spring Boot 项目中使用 MyBatis-Plus 进行数据库操作了。

相关推荐
AAA修煤气灶刘哥3 小时前
32 GB 内存跑起 1800 亿参数模型,代价是什么?
人工智能·后端·架构
SimonKing3 小时前
零 OCR 依赖!Firecrawl 这个 Rust 库把 Word / PPT / Excel 全变成 Markdown
java·后端·程序员
Rain的Java大神之路3 小时前
别被 ChatGPT 的打字机效果骗了:一字一字蹦,才是 LLM 自回归的命门(附大厂 Java 流式落地)
java·后端·面试
AAA修煤气灶刘哥3 小时前
同一个项目,为什么换个 Agent 又得从头解释?
后端·架构·代码规范
渣渣盟3 小时前
狂命爆肝99天,共52K字的JAVA学习笔记奉上,JAVA从入门到精通一文搞定,一文在手JAVA无忧
java·笔记·学习
BingoGo4 小时前
PHP 8.6 正式发布
后端·php
JaguarJack4 小时前
PHP 8.6 正式发布
后端·php·服务端
星落梦生4 小时前
Maven高级(9)
java·maven
码事漫谈13 小时前
# 2027年,AI终于要开始“干活”了
后端
小满zs14 小时前
Go语言第二十一章(Gin上传文件)
后端·go