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.propertiesapplication.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 进行数据库操作了。

相关推荐
飞翔中文网25 分钟前
Java设计模式之装饰器模式
java·设计模式
Suwg2091 小时前
【Java导出word】使用poi-tl轻松实现Java导出数据到Word文档
java·开发语言·word·poi-tl
坚持拒绝熬夜1 小时前
JVM的一些知识
java·jvm·笔记·java-ee
修炼成精3 小时前
C#实现的一个简单的软件保护方案
java·开发语言·c#
网安-轩逸3 小时前
网络安全——SpringBoot配置文件明文加密
spring boot·安全·web安全
乌云暮年3 小时前
算法刷题整理合集(四)
java·开发语言·算法·dfs·bfs
代码代码快快显灵4 小时前
SpringSecurity——如何获取当前登录用户的信息
java·开发语言·springsecurity
Luo_LA4 小时前
【排序算法对比】快速排序、归并排序、堆排序
java·算法·排序算法
果冻kk4 小时前
【Java集合夜话】第1篇:拨开迷雾,探寻集合框架的精妙设计
java·开发语言