MyBatis-Plus integrated SpringBoot entry case


1. MyBatis-Plus

1 Introduction

MyBatis-Plus is a Mybatis enhanced version tool, which expands other functions on MyBatis without changing its basic functions, and exists to simplify the efficiency of development and submission.

Official website document address: https://mp.baomidou.com/guide/

MyBatis-Plus features: https://mp.baomidou.com/guide/#%E7%89%B9%E6%80%A7


2. Use SpringBoot to quickly use MyBatis-Plus

(1) Preparation
    requires Java development environment (JDK) and corresponding development tools (IDE).
    Requires maven (jar packages used to download related dependencies).
    Requires Spring Boot.
    You can use IDEA to install a mybatis-plus plugin.


(2) Create a SpringBoot project.
    Method 1: Go to the official website https://start.spring.io/ to initialize one, and then import the IDE tool.
    Method 2: directly use the IDE tool to create one. Spring Initializer.


 

(3) Add MyBatis-Plus dependency (mybatis-plus-boot-starter)

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

(4) In order to test and develop, mysql 8 is used here, and mysql-related dependencies need to be introduced.
    In order to simplify the code, introduce lombok dependency (reduce getter, setter and other methods).

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.18</version>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.10</version>
</dependency>

(5) Complete dependency file (pom.xml)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.lyh.test</groupId>
    <artifactId>test-mybatis-plus</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>test-mybatis-plus</name>
    <description>测试 -- 测试 MyBatis-Plus 功能</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.3.1.tmp</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.18</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.10</version>
        </dependency>

        <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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

(6) Use a table for testing.

    FYI, fields such as creation time, modification time, etc. can be defined.

DROP DATABASE IF EXISTS testMyBatisPlus;

CREATE DATABASE testMyBatisPlus;

USE testMyBatisPlus;

DROP TABLE IF EXISTS user;

CREATE TABLE user
(
    id BIGINT(20) NOT NULL COMMENT '主键ID',
    name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
    age INT(11) NULL DEFAULT NULL COMMENT '年龄',
    email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
    PRIMARY KEY (id)
);

INSERT INTO user (id, name, age, email) VALUES
(1, 'Jone', 18, '[email protected]'),
(2, 'Jack', 20, '[email protected]'),
(3, 'Tom', 28, '[email protected]'),
(4, 'Sandy', 21, '[email protected]'),
(5, 'Billie', 24, '[email protected]');


(7) Configure the mysql data source information in the application.yml file.

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/testMyBatisPlus?useUnicode=true&characterEncoding=utf8


(8) Write the entity class corresponding to the table.

package entity;

import lombok.Data;

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


(9) Write the Mapper class that operates the entity class.
    Directly inherit BaseMapper, which is a packaged class of mybatis-plus.

package mapper;

import bean.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

public interface UserMapper extends BaseMapper<User> {
}


(10) After the entity class and Mapper class are written, they can be used.
      Step1: First scan the Mapper class in the startup class, that is, add the @MapperScan annotation

package com.lyh.test.testmybatisplus;

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

@MapperScan("mapper")
@SpringBootApplication
public class TestMybatisPlusApplication {

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

}

Step2: Write a test class to test it.

package com.lyh.test.testmybatisplus;

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

import java.util.List;

@SpringBootTest
class TestMybatisPlusApplicationTests {

    @Autowired
    private UserMapper userMapper;

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


(11) Summary: Through the above simple operations, you can perform CRUD operations on the user table without writing an xml file.
           Note: If a variable marked with @Autowired appears underlined in red, it does not affect normal operation.


You can enter Settings, find Inspection, and select Spring Core -> Code -> Autowiring for Bean Class, and change Error to Warning.


To view the executed sql statement, you can add configuration information in the yml file, as follows.

As shown in the figure below: the sql statement will be printed out during execution.

Guess you like

Origin blog.csdn.net/weixin_53443677/article/details/125516810