It's too hot! Why is MyBatis Plus so awesome?

  • Features

  • text

  • Component dependency

  • Extension code

Everyone who has used MyBatis-Plus (MP for short) knows that it is an enhancement tool for MyBatis. It is dedicated to enhancing MyBatis without making changes. It is born to simplify development and improve efficiency.

Features

  • No intrusion: only make enhancements without making changes, 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 operation: built-in general Mapper and 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, which can meet various needs and organize a 272-page MybatisPDF document

  • Support Lambda form call: through Lambda expressions, it is convenient to write various query conditions, no need to worry about writing wrong fields

  • Supports automatic generation of primary keys: supports up to 4 primary key strategies (contains a distributed unique ID generator-Sequence), which can be freely configured to perfectly solve primary key problems

  • 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 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

  • Paging plug-in supports multiple databases: supports MySQL, MariaDB, Oracle, DB2, H2, HSQL, SQLite, Postgre, SQLServer and other databases

  • 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 full table delete and update operation intelligent analysis and blocking, and can also customize interception rules to prevent misoperation

text

In actual project development, we often need to save data in batches to the database. Have you more or less realized it with mybatis-plus?

Component dependency

First, we need to introduce the mybatis-plus open source component through Maven, and add the following code to the pom.xml file:

<!--mybatis-->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.0</version>
</dependency>
<!--mybatis plus extension,包含了mybatis plus core-->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-extension</artifactId>
    <version>3.4.0</version>
</dependency>

By viewing the source code, I found the batch insertion interface provided by the API interface:

It's too hot!  Why is MyBatis Plus so awesome?
After starting the service, use Postman to debug, and the background printing is as follows:

It's too hot!  Why is MyBatis Plus so awesome?

It can be seen from the figure that this so-called batch insertion interface is actually a for loop insertion, Oh, My God! is simply a nightmare.

It is difficult to implement manually, so,

INSERT INTO test (a, b, c) VALUES
<foreach collection="list" item="item" separator=",">
    (#{item.a}, #{item.b}, #{item.c})
</foreach>

We read the source code of mybatis-plus. In the com.baomidou.mybatisplus.extension.injector.methods.InsertBatchSomeColumn package, there is actually a batch insertion code implementation. I will not post the source code here, and you will follow it yourself. Let’s extend it manually a bit:

Extension code

Talk is cheap, show me the code. First show the code. Then I will show you slowly why this is done:

It's too hot!  Why is MyBatis Plus so awesome?

Inject the Bean into the MybatisPlusConfig file, the code is as follows:

@Configuration
public class MybatisPlusConfig {

    /**
     * 分页插件
     *
     * @return PaginationInterceptor
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }

    @Bean
    public EasySqlInjector easySqlInjector() {
        return new EasySqlInjector();
    }
}

Also extend the built-in BaseMapper, the code is as follows:

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

import java.util.Collection;

/**
 * 扩展通用 Mapper,支持数据批量插入
 *
 * @author 天开易想
 */
public interface EasyBaseMapper<T> extends BaseMapper<T> {

    /**
     * 批量插入 仅适用于mysql
     *
     * @param entityList 实体列表
     * @return 影响行数
     */
    Integer insertBatchSomeColumn(Collection<T> entityList);
}

We can implement the following references in the business class, the code is as follows:

/**
 * 定义业务mapper接口,继承刚刚扩展的EasyBaseMapper
 *
 * @author 天开易想
 */
@Mapper
public interface TestMapper extends EasyBaseMapper<Test> {
}

/**
 * 业务实现类接口,即可引用
 *
 * @author 天开易想
 */
@Service
public class TestServiceImpl extends ServiceImpl<TestMapper, Test> implements TestService {

    @Override
    public Integer testBatch(Collection<Test> testList) {
        return baseMapper.insertBatchSomeColumn(testList);
    }

Because it can't be quoted directly in BaseMapper, why can't it be quoted directly?It is said that it only supports MySql database, so the author has no built-in reason. I compiled a 272-page MybatisPDF document.

Guess you like

Origin blog.51cto.com/14975073/2590388