Skip to content

Quick Start

We’ll demonstrate the powerful features of MyBatis-Plus through a simple Demo. Before we begin, we assume you already have:

  • A Java development environment and corresponding IDE
  • Familiarity with Spring Boot
  • Familiarity with Maven or Gradle

Let’s consider a User table with the following structure:

idnameageemail
1Jone18test1@baomidou.com
2Jack20test2@baomidou.com
3Tom28test3@baomidou.com
4Sandy21test4@baomidou.com
5Billie24test5@baomidou.com

The corresponding database Schema script is as follows:

schema-h2.sql
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user`
(
id BIGINT NOT NULL COMMENT 'Primary Key ID',
name VARCHAR(30) NULL DEFAULT NULL COMMENT 'Name',
age INT NULL DEFAULT NULL COMMENT 'Age',
email VARCHAR(50) NULL DEFAULT NULL COMMENT 'Email',
PRIMARY KEY (id)
);

The corresponding database Data script is as follows:

data-h2.sql
DELETE FROM `user`;
INSERT INTO `user` (id, name, age, email) VALUES
(1, 'Jone', 18, 'test1@baomidou.com'),
(2, 'Jack', 20, 'test2@baomidou.com'),
(3, 'Tom', 28, 'test3@baomidou.com'),
(4, 'Sandy', 21, 'test4@baomidou.com'),
(5, 'Billie', 24, 'test5@baomidou.com');

Initialize Project

Create an empty Spring Boot project and add the H2 database for integration testing.

Add Dependencies

Add the MyBatis-Plus Starter dependency.

Spring Boot2

<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.14</version>
</dependency>

Spring Boot3

<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
<version>3.5.14</version>
</dependency>

Spring Boot4 (Since 3.5.13)

<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-spring-boot4-starter</artifactId>
<version>3.5.14</version>
</dependency>

Configuration

Add H2 database configuration in the application.yml configuration file:

application.yml
# DataSource Config
spring:
datasource:
driver-class-name: org.h2.Driver
username: root
password: test
sql:
init:
schema-locations: classpath:db/schema-h2.sql
data-locations: classpath:db/data-h2.sql

The configuration above contains the standard database connection settings for any Spring Boot project. If you’re using a different database like MySQL, modify the configuration accordingly.

Add the @MapperScan annotation to the Spring Boot application class to scan the Mapper package:

Application.java
@SpringBootApplication
@MapperScan("com.baomidou.mybatisplus.samples.quickstart.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

Coding

Create the entity class User.java:

User.java
@Data
@TableName("`user`")
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}

Create the Mapper interface UserMapper.java:

public interface UserMapper extends BaseMapper<User> {
}

Start Using

Add a test class for functionality testing:

@SpringBootTest
public class SampleTest {
@Autowired
private UserMapper userMapper;
@Test
public void testSelect() {
System.out.println(("----- selectAll method test ------"));
List<User> userList = userMapper.selectList(null);
Assert.isTrue(5 == userList.size(), "");
userList.forEach(System.out::println);
}
}

Console output:

User(id=1, name=Jone, age=18, email=test1@baomidou.com)
User(id=2, name=Jack, age=20, email=test2@baomidou.com)
User(id=3, name=Tom, age=28, email=test3@baomidou.com)
User(id=4, name=Sandy, age=21, email=test4@baomidou.com)
User(id=5, name=Billie, age=24, email=test5@baomidou.com)

Summary

With just these simple steps, we’ve implemented CRUD functionality for the User table without even writing any XML files!

From the steps above, you can see that integrating MyBatis-Plus is very straightforward—just add the starter dependency and do some basic configuration to start using it.

But MyBatis-Plus is much more powerful than what we’ve shown here. Want to learn more about its powerful features? Continue reading!

Baomidou

© 2016-2025 Baomidou™. All Rights Reserved.

Power by Astro Starlight | Sponsored by JetBrains

渝ICP备2021000141号-1 | 渝公网安备50011302222097