# Quick Start

Let's have a simple Demo to show how to use MP. But before that, we suppose you:

  • Have environment and IDE for Java development
  • Are familiar with Spring Boot
  • Are familiar with Maven

Here is a table named User

id name age email
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

Schema script as below:

DROP TABLE IF EXISTS user;

CREATE TABLE user
(
	id BIGINT(20) NOT NULL COMMENT 'Primary key',
	name VARCHAR(30) NULL DEFAULT NULL COMMENT 'name',
	age INT(11) NULL DEFAULT NULL COMMENT 'age',
	email VARCHAR(50) NULL DEFAULT NULL COMMENT 'email',
	PRIMARY KEY (id)
);

Data script as below:

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

Question

What should we do for CRUD by MP?

# Initialization

Create a project of Spring Boot(will use H2 Database for showcase by default)

TIP

You can use Spring Initializer for Spring Boot Quick Start

# Dependency

Import Spring Boot Starter as parent:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>spring-latest-version</version>
    <relativePath/>
</parent>

Import spring-boot-starterspring-boot-starter-testmybatis-plus-boot-starterlombokh2


















 









<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>starter-latest-version</version>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>
</dependencies>

# Configuration

Define H2 Database configuration in application.yml

# DataSource Config
spring:
  datasource:
    driver-class-name: org.h2.Driver
    schema: classpath:db/schema-h2.sql
    data: classpath:db/data-h2.sql
    url: jdbc:h2:mem:test
    username: root
    password: test

Add @MapperScan Annotation in Spring boot application class, configure Mapper's package:


 








@SpringBootApplication
@MapperScan("com.baomidou.mybatisplus.samples.quickstart.mapper")
public class QuickStartApplication {

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

}

# Code

Write User.java as an entity(Here using Lombok to simplify getter/setter methods)

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

Write a Mapper UserMapper.java

public interface UserMapper extends BaseMapper<User> {

}

# Let's begin

Add a test case:

@RunWith(SpringRunner.class)
@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.assertEquals(5, userList.size());
        userList.forEach(System.out::println);
    }

}

TIP

The parameter of selectList() is a condition constructor built-in MP, null means no condition(fetch all records)

Console log:

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)

TIP

For Complete codes, please refer to: Spring Boot Quick Start Sample

# Summary

We have achieved CURD for table User by the few steps above, even no XML file!

It's very simple to integrate Mybatis-Plus, just import starter and configure mapper's package.

But the powerful functions are far more than these. Keep going!

Sponsors