[Capability Improvement-MyBatis-Plus] Initial MyBatis-Plus

Insert picture description here

Mybatis-Plus introduction

MyBatis-Plus (opens new window) (MP for short) is an enhancement tool for MyBatis (opens new window). On the basis of MyBatis, it only enhances and does not change. It is born to simplify development and improve efficiency.

Government network 1: https://mp.baomidou.com/

Official website 2: https://mybatis.plus/

Document: https://baomidou.com/guide/

Source code: https://github.com/baomidou/awesome-mybatis-plus

Features of MyBatis-Plus:

  • No intrusion: only enhance and do not change, the introduction of it will not affect the existing project, it is as smooth as silk
  • Low loss: basic CURD will be automatically injected at startup, performance is basically no loss, direct object-oriented operation
  • Powerful CRUD operations: built-in general Mapper, general service, only a small amount of configuration can realize most of the CRUD operations of a single table, and a more powerful condition builder to meet various usage requirements
  • Support Lambda form invocation: Through Lambda expressions, it is convenient to write various query conditions, no need to worry about writing wrong fields.
    Support primary key automatic generation: support up to 4 primary key strategies (including distributed unique ID generator-Sequence), Free configuration, perfect solution to the primary key problem
  • Support ActiveRecord mode: Support ActiveRecord form call, entity classes only need to inherit Model class to perform powerful CRUD operations
  • Support custom global general operations: Support global general method injection (Write once, use anywhere)
    Built-in code generator: Use code or Maven plug-in to quickly generate Mapper, Model, Service, Controller layer code, support template engine, and more Custom configuration is waiting 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
    paging plug-ins. Support multiple databases: support MySQL, MariaDB, Oracle, DB2, H2, HSQL, SQLite, Various databases such as Postgre, SQLServer, etc.
  • Built-in performance analysis plug-in: Sql statement and its execution time can be output. It is recommended to enable this function during development and testing to quickly detect slow queries
  • Built-in global interception plug-in: Provides intelligent analysis and blocking of delete and update operations of the entire table, and can also customize interception rules to prevent misoperations

Supported databases

Any database that can use mybatis for crud and supports standard sql

Architecture diagram

Insert picture description here


Quick start

Create an empty springboot project

Add dependency

Introduce the Spring Boot Starter parent project:

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

Introduce dependencies such as spring-boot-starter, spring-boot-starter-test, mybatis-plus-boot-starter, lombok, mysql, slf4j, etc.

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </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>
    <!--mybatis-plus的springboot支持-->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.1.1</version>
    </dependency>
    <!--mysql驱动-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.47</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
    </dependency>

</dependencies>

Supplemental configuration

Add the relevant configuration of the mysql database in the application.properties configuration file:

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mp?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=root

Create MybatisPlusConfig configuration class

package cn.javaClimber.mp;

import cn.javaClimber.handler.MyMetaObjectHandler;
import cn.javaClimber.injectors.MySqlInjector;
import cn.javaClimber.mp.plugin.MyInterceptor;
import com.baomidou.mybatisplus.core.parser.ISqlParser;
import com.baomidou.mybatisplus.extension.parsers.BlockAttackSqlParser;
import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
import com.baomidou.mybatisplus.extension.plugins.SqlExplainInterceptor;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

@Configuration
@MapperScan("cn.javaClimber.mp.mapper") //设置mapper接口的扫描包
public class MybatisPlusConfig {

}

Through the above few simple steps, we have realized the CRUD function of the table, even without writing XML files! From the above steps, we can see that integrating MyBatis-Plus is very simple, just import the starter project and configure the mapper scan path.


Common notes

  • @TableName
    • Description: Table name annotation
  • @TableId
    • Description: Primary key annotation --> configurable id type
  • @TableField
    • Description: Field annotation (non-primary key) --> Configurable whether to query, auto-fill strategy, database field name, etc.
  • @Version
    • Description: optimistic lock annotation, mark @Verison on the field
  • @EnumValue
    • Description: through enumeration class annotation (annotation is on the enumeration field)
  • @TableLogic
    • Description: Annotation for logical processing of table fields (logical deletion)

CRUD case

Too simple... I won’t record it

Guess you like

Origin blog.csdn.net/qq_37126480/article/details/115021564