SpringBoot | MyBatisPlus quick start

Source code of this article: Gitee point here

Introduction (from Official website)

MyBatis-Plus (MP) is a MyBatis On the basis of MyBatis, the enhancement tool is only enhanced without change, and is born to simplify development and improve efficiency.


  • No invasion: it is only enhanced without change, and its introduction will not affect the existing project, which is as smooth as silk
  • Low loss: the basic CURD will be injected automatically upon startup, with basically no loss of performance and direct object-oriented operation
  • Powerful crud operation: built-in general Mapper and general Service. Most CRUD operations of a single table can be realized only through a small number of configurations. There is also a powerful condition constructor to meet various use needs
  • Support Lambda form call: through Lambda expression, it is convenient to write various query conditions without worrying about wrong fields
  • Support automatic generation of primary key: support up to 4 primary key strategies (including distributed unique ID generator - Sequence), which can be configured freely to perfectly solve the problem of primary key
  • Support ActiveRecord mode: support ActiveRecord formal calls. Entity classes only need to inherit Model classes to perform powerful CRUD operations
  • Support custom global general operations: support global general method injection (Write once, use anywhere)
  • Built in code generator: code or Maven plug-in can be used to quickly generate Mapper, Model, Service and Controller layer code, support template engine, and have more custom configurations for you to use
  • Built in paging plug-in: Based on MyBatis physical paging, developers do not need to care about specific operations. After configuring the plug-in, writing paging is equivalent to ordinary List query
  • The paging plug-in supports multiple databases: MySQL, MariaDB, Oracle, DB2, H2, HSQL, SQLite, Postgre, SQLServer and other databases
  • Built in performance analysis plug-in: it can output Sql statements and their execution time. It is recommended to enable this function during development and testing to quickly find out slow queries
  • Built in global interception plug-in: it provides intelligent analysis and blocking of full table delete and update operations, and can also customize interception rules to prevent misoperation

Support database

  • mysql ,mariadb ,oracle ,db2 ,h2 ,hsql ,sqlite ,postgresql ,sqlserver ,presto ,Gauss ,Firebird
  • Phoenix, clickhouse, Sybase ASE, OceanBase, Dameng database, Xugu database, NPC Jincang database and NTU general database

Frame structure

Quick start

We will illustrate the power of mybatis plus with a simple Demo.

Create a table:

	id BIGINT(20) NOT NULL COMMENT 'Primary key ID',

Insert data script:

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');

Add dependency

Introduce the dependency of mybatis plus boot starter, spring boot starter test and MySQL connector Java:


to configure

In application Add the relevant configuration of MySQL database in the YML configuration file. In order to facilitate the later demonstration, the sql printing function is started:

    url: jdbc:mysql://localhost:3306/springboot-mybatis-plus?charset=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: root

    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl  # Enable sql printing

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

public class MybatisPlusApplication {

    public static void main(String[] args) {
        SpringApplication.run(MybatisPlusApplication.class, args);

Entity class

Add User entity class

public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;

    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;

    public String getEmail() {
        return email;

    public void setEmail(String email) {
        this.email = email;

    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", email='" + email + '\'' +

Mapper interface

Add UserMapper interface, inherit basemapper < T >, and the generic class is User

public interface UserMapper extends BaseMapper<User> {

Start using

Add test class to test

public class UserMapperTest {

    private UserMapper userMapper;

    public void testSelect() {
        System.out.println(("----- selectAll method test ------"));
        List<User> userList = userMapper.selectList(null);


Output result:

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)


Through the above simple steps, we have realized the CRUD function of the User table, and we don't even need to write XML files!

From the above steps, we can see that integrating mybatis plus is very simple. We only need to introduce the starter project and configure the mapper scanning path.

But mybatis plus is far more powerful than these functions. Would you like to learn more about the powerful functions of mybatis plus? Then keep looking down!

Tags: Mybatis Spring Boot mybatis-plus

Posted by pieai on Sun, 15 May 2022 12:45:43 +0300