Skip to content

Quick Start

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

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

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

Introduce the MyBatis-Plus Starter dependency

Spring Boot 2

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

Spring Boot 3

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

Configuration

Add the relevant H2 database configuration to 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 is the database connection information that any Spring Boot project would configure. If you are using a different database, such as MySQL, you need to modify the corresponding configuration information.

Add the @MapperScan annotation to the Spring Boot startup class to scan the Mapper folder:

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

Write 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;
}

Write the Mapper interface class UserMapper.java:

public interface UserMapper extends BaseMapper<User> {
}

Start Using

Add a test class for functional 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

Through these simple steps, we have implemented the CRUD functions for the User table, without even needing to write an XML file!

From the steps above, we can see that integrating MyBatis-Plus is very simple. Just introduce the starter dependency and configure it briefly to use.

But the power of MyBatis-Plus goes far beyond these features. Want to learn more about the powerful features of MyBatis-Plus? Then keep reading!

Baomidou

© 2016-2025 Baomidou™. All Rights Reserved.

Power by Astro Starlight | Sponsored by JetBrains

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