MyBatisPlus study notes < 1 >

Posted by jodie on Sat, 05 Mar 2022 07:42:11 +0100

Opening summary:

When I learned mybatis plus, I did find that the development efficiency is much more convenient, but I still have to learn on the basis of mybatis. It doesn't mean that learning mybatis plus discards mybatis. The foundation is very important.

Performance:

  • No invasion: it is only enhanced without change, and its introduction will not affect the existing project, which is as smooth as silk
  • CURD: low object-oriented operation, i.e. no loss, basic automatic operation, i.e. no loss
  • Powerful crud operation: built-in general Mapper and general Service. Most CRUD operations of a single table can be realized only through a small number of configurations. There is also a powerful condition constructor to meet various use needs
  • Support Lambda form call: through Lambda expression, it is convenient to write various query conditions without worrying about wrong fields
  • Support automatic generation of primary key: support up to 4 primary key strategies (including distributed unique ID generator - Sequence), which can be configured freely to perfectly solve the problem of primary key
  • Support ActiveRecord mode: support ActiveRecord formal calls. Entity classes only need to inherit Model classes to perform powerful CRUD operations
  • Support custom global general operations: support global general method injection (Write once, use anywhere)
  • Built in code generator: code or Maven plug-in can be used to quickly generate Mapper, Model, Service and Controller layer code, support template engine, and have 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
  • The paging plug-in supports multiple databases: MySQL, MariaDB, Oracle, DB2, H2, HSQL, SQLite, Postgre, SQLServer and other databases
  • Built in performance analysis plug-in: it can output 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: it provides intelligent analysis and blocking of full table delete and update operations, and can also customize interception rules to prevent misoperation

This is the performance profile given in the official document. In fact, for beginners like me, the only advantage is to improve my development speed.

 
Preliminary work:

Let's create a data table as a demo to get familiar with the operation of mybatis plus. I quoted the official sql script

DROP TABLE IF EXISTS user;

CREATE TABLE user
(
    id BIGINT(20) NOT NULL COMMENT 'Primary key ID',
    name VARCHAR(30) NULL DEFAULT NULL COMMENT 'full name',
    age INT(11) NULL DEFAULT NULL COMMENT 'Age',
    email VARCHAR(50) NULL DEFAULT NULL COMMENT 'mailbox',
    PRIMARY KEY (id)
);

Corresponding script data

DELETE FROM user;

INSERT INTO user (id, name, age, email) VALUES
(1, 'Jone', 18, 'test1@baomidou.com'),
(2, 'Jack', 20, 'test2@baomidou.com'),
(3, 'Tom', 28, 'test3@baomidou.com'),
(4, 'Sandy', 21, 'test4@baomidou.com'),
(5, 'Billie', 24, 'test5@baomidou.com');

ok, so our watch will be built

Dependency and configuration:

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

At present, the latest version given by the official is 3.5.1. The database in the official document is h2 dependent (the embedded database of java, which we do not use here, we use mysql)

datasource:
    username: root
    password: 123456
    url: url
    driver-class-name: com.mysql.cj.jdbc.Driver

According to the document, you need to scan Mapper class in the startup class, or you can add (@ Mapper annotation) in Mapper class, so you don't need to scan.

We write an entity class code and its Mapper class (inheriting the parent class BaseMapper given by mybatis plus)

code.

@Data
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
}
public interface UserMapper extends BaseMapper<User> {

}

In this way, we have configured it. Let's talk about the conditional constructor instead of adding, deleting, changing and checking.

Conditional constructor:

Some complex sql statements can be replaced by wrappers (where conditions or Oderby and groupby conditions for generating sql)

Here we give a small example

 QueryWrapper<User> wrapper = new QueryWrapper<>();
 //wrapper. EQ (the first parameter is the field name, and the second is the parameter you pass in for comparison)
 //The sql statement implemented here is select * from user where name = (the parameter you pass in for comparison)
 wrapper.eq("name",name);
 //Results of sql execution
 List<User> list=UserMapper.selectList(wrapper);

There are also many query criteria, such as:

Original link: (33 messages) collection of QueryWrapper methods of mybatis plus condition constructor_ Try and try again - CSDN blog

Topics: Java