SoFunction
Updated on 2025-04-13

Steps and example code for using myBatisPlus in springBoot

MyBatis-Plus is an enhancement tool for MyBatis. Using it in Spring Boot projects can greatly improve development efficiency. The following is a detailed introduction to the steps and sample code for using MyBatis-Plus in Spring Boot.

1. Create a Spring Boot project

You can create a new Spring Boot project with Spring Initializr(/) and add the following dependencies:

  • Spring Web
  • Spring Data JPA (although using MyBatis-Plus, this dependency can be added as needed)
  • MyBatis Framework
  • MySQL Driver (assuming a MySQL database)

2. Add MyBatis-Plus dependency

existAdd MyBatis-Plus dependencies:

<dependency>
    <groupId></groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.5.3.1</version>
</dependency>

3. Configure database connections

existorConfigure database connection information:

=jdbc:mysql://localhost:3306/your_database_name
=your_username
=your_password
-class-name=

4. Create entity class

Create a simple entity class, e.g.User

import ;
import ;
@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) {
         = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
         = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
         = age;
    }
}

5. Create a Mapper interface

Create an inherited fromBaseMapperMapper interface:

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

6. Create a Service Layer

Create a Service interface and its implementation class:

import ;
public interface UserService {
    List<User> getAllUsers();
    User getUserById(Long id);
    boolean saveUser(User user);
    boolean updateUser(User user);
    boolean deleteUser(Long id);
}
import ;
import ;
import ;
import ;
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public List<User> getAllUsers() {
        return (null);
    }
    @Override
    public User getUserById(Long id) {
        return (id);
    }
    @Override
    public boolean saveUser(User user) {
        return (user) > 0;
    }
    @Override
    public boolean updateUser(User user) {
        return (user) > 0;
    }
    @Override
    public boolean deleteUser(Long id) {
        return (id) > 0;
    }
}

7. Create Controller Layer

Create a Controller to handle HTTP requests:

import ;
import .*;
import ;
@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping
    public List<User> getAllUsers() {
        return ();
    }
    @GetMapping("/{id}")
    public User getUserById(@PathVariable Long id) {
        return (id);
    }
    @PostMapping
    public boolean saveUser(@RequestBody User user) {
        return (user);
    }
    @PutMapping
    public boolean updateUser(@RequestBody User user) {
        return (user);
    }
    @DeleteMapping("/{id}")
    public boolean deleteUser(@PathVariable Long id) {
        return (id);
    }
}

8. Launch the application

After starting the Spring Boot application, you can operate through the following APIUserdata:

  • GET /users: Obtain all user information.
  • GET /users/{id}: Obtain user information based on ID.
  • POST /users: Added new users.
  • PUT /users: Update user information.
  • DELETE /users/{id}: Delete the user according to the ID.

Follow the above steps and you can use MyBatis-Plus to perform database operations in your Spring Boot project.

This is the article about the use steps and sample code of myBatisPlus in springBoot. For more related content of using springBoot myBatisPlus, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!