preface

Everyone who has used MyBatis-Plus (referred to as MP) knows that it is an enhancement tool of MyBatis. It aims to enhance MyBatis without changing it on the basis of MyBatis. It is born to simplify development and improve efficiency.

The characteristics of

  • No intrusion: only enhancements are made, no changes are made, and its introduction will not affect the existing project, as smooth as silk
  • Low loss: Basic CURD will be injected automatically upon startup, with basically no loss in performance and direct object-oriented operation
  • Powerful CRUD operations: built-in universal Mapper, universal Service, only through a small amount of configuration can achieve a single table most CRUD operations, more powerful condition constructor, to meet all types of use requirements
  • Support Lambda form call: through Lambda expressions, it is convenient to write all kinds of query conditions, without worrying about field write errors
  • Support automatic generation of primary keys: support up to four primary key policies (including distributed unique ID generator – Sequence), can be freely configured, perfect solution to the primary key problem
  • Support for ActiveRecord mode: Support for ActiveRecord form calls, entity classes only need to inherit from Model classes to perform powerful CRUD operations
  • Support custom global universal operations: support Write once (use anywhere)
  • Built-in code generator: using code or Maven plug-in can quickly generate Mapper, Model, Service, Controller layer code, support template engine, more than a lot of custom configuration 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, write paging is equal to ordinary List query
  • The paging plug-in supports a variety of databases: MySQL, MariaDB, Oracle, DB2, H2, HSQL, SQLite, Postgre, SQLServer, etc
  • Built-in performance analysis plug-in: outputs Sql statements and their execution time. It is recommended to enable this function during development and testing to quickly find out slow queries
  • Built-in global interception plug-in: provides intelligent analysis and blocking of delete and UPDATE operations on all tables, and can customize interception rules to prevent misoperations

The body of the

In the actual project development, we often have the need to save the data in batches to the database, we more or less use Mybatis – Plus to achieve it?

The component dependence

Maven: Mybatis — plus: mybatis — Plus: Mybatis — Plus: MyBatis — Plus: MyBatis — Plus: MyBatis — Plus: MyBatis — Plus: MyBatis — Plus: MyBatis — Plus: MyBatis — Plus:

<! --mybatis--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> The < version > 3.4.0 < / version > < / dependency > <! <dependency> <groupId>com.baomidou</groupId> < artifactId > mybatis - plus - the extension < / artifactId > < version > 3.4.0 < / version > < / dependency >Copy the code

Find the bulk insert interface provided by the API interface by looking at the source code:

After starting the service, use Postman to debug, and the background print is as follows:

The bulk insert interface is actually a for loop. Oh,My God! It was a nightmare.

Do I have to do it by hand? So,

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

Mybatis – plus source code, we read in com. Baomidou. Mybatisplus. The extension. The injector. The methods. The InsertBatchSomeColumn package there are bulk insert code implementation, here I will not post the source code, Keep track of it yourself. Let’s extend it manually:

Extension code

Talk is cheap, show me the code. I’ll show you the code and explain why it’s done this way:

Inject the Bean into the MybatisPlusConfig file as follows:

@configuration Public class MybatisPlusConfig {@return PaginationInterceptor */ @bean public PaginationInterceptor paginationInterceptor() { return new PaginationInterceptor(); } @Bean public EasySqlInjector easySqlInjector() { return new EasySqlInjector(); }}Copy the code

Also extend BaseMapper, code as follows:

import com.baomidou.mybatisplus.core.mapper.BaseMapper; import java.util.Collection; /** * Extension to Mapper, Public interface EasyBaseMapper<T> extends BaseMapper<T> {/** ** EasyBaseMapper<T> extends BaseMapper<T> {/** ** EasyBaseMapper<T> {/** ** EasyBaseMapper<T EntityList entityList * @return number of affected rows */ Integer insertBatchSomeColumn(Collection<T> entityList); }Copy the code

We can implement the following reference in the business class as follows:

/** * Defines a business mapper interface that extends EasyBaseMapper ** @author */ @mapper public Interface TestMapper extends EasyBaseMapper<Test> {}/** * Service public class TestServiceImpl extends ServiceImpl<TestMapper, Test> implements TestService { @Override public Integer testBatch(Collection<Test> testList) { return baseMapper.insertBatchSomeColumn(testList); }Copy the code

Because BaseMapper is not used to directly reference, why can not directly reference, it is said that only support MySql database, so the author does not have built-in reasons!