SpringBoot integrates MybatisPlus 1 - Introduction

MybatisPlus is an upgraded version of Mybatis and a simplification of Mybatis, because their slogan is "born to simplify development".

1. Create a data table

CREATE TABLE ​​User​​ (

​id​​ INT NOT NULL,

​username​​ VARCHAR(50) NULL DEFAULT NULL,

​gendar​​ CHAR(2) NULL DEFAULT NULL,

​remark​​ VARCHAR(50) NULL DEFAULT NULL

);

Insert data into table

INSERT INTO user VALUES(1, 'admin', '男', '管理员账号')

2. Create SpringBoot project

3. Reference MyBatisPlus third-party package

Introduce third-party packages in pom.xml. What needs to be noted here is that in addition to introducing the mybatis-plus package, you also need to introduce the mybatis-plus starter package specially packaged for springboot.

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

4. Create entity class files

Create a pojo package and create a User class under the package.

The class name of the entity class and the names of each member variable can be consistent with the names of the fields in the data table to facilitate mapping.

Manual mapping can also be done in the class:

You can use annotations on class ​@TableName(value = "数据表名")​names to establish associations between entity classes and data tables;

Use annotations on attribute ​@TableField(value = "数据表列名")​names to establish associations between attribute names and data table fields.

package com.test.pojo;

@TableName(value = "user")
public class User {
    @TableField(value = "id")
    Integer id;
    String username;
    String gendar;
    String remark;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getGendar() {
        return gendar;
    }

    public void setGendar(String gendar) {
        this.gendar = gendar;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + ''' +
                ", gendar='" + gendar + ''' +
                ", remark='" + remark + ''' +
                '}';
    }
}

5. Create mapper interface

Create the mapper package and create the UserMapper interface under the package.

This interface inherits the BaseMapper interface and replaces the generic type of the BaseMapper interface with the class name of the entity class.

package com.test.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.test.pojo.User;

public interface UserMapper extends BaseMapper<User> {
}

6. Set the scan directory in the project startup class

Add the MapperScan annotation before starting the class, and specify the directory to be scanned in the annotation:

Because the mapper created in step 5 is in this directory: `com.test.mapper`, you need to specify the full path to the package after annotation.

package com.test;


import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.test.mapper")
public class TestApplication {
    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }
}

7. Configure database information

7.1 Add configuration file

Create the configuration file application.yml under the project and configure the database-related information. Just fill in the database connection parameters according to the actual situation.

During program execution, if we want to see the SQL statements generated by MyBatisPlus, we can add the following content to the configuration file to enable the printing function of the SQL log.

7.2 Add database dependency

Add the driver class of mysql database to pom.xml

<dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
</dependency>

8. Create test class

Please note that the package path of the test class should be the same as the package path of the class under test.

Query records based on ID and output to console

package com.test;

import com.test.mapper.UserMapper;
import com.test.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class TestUser {
    @Autowired
    UserMapper userMapper;

    @Test
    public void getUser(){
        User user = userMapper.selectById(1);
        System.out.println(user);
    }
}

The output after executing the test is as follows:

User{id=1, username='admin', gendar='male', remark='admin account'}

Guess you like

Origin blog.csdn.net/QQ156881887/article/details/131585759