<> preface

In the persistence layer framework mybatis More applications , And the proportion is gradually rising . Usually the combination of projects is SSM.mybatis Why fire , Because of his flexibility , Easy to use , Optimization is relatively easy .

mybatis Direct execution of sql sentence , sql The statement is written in xml In the file , use mybatis Multiple required xml
configuration file , To some extent, it is cumbersome . Generally, the operation of database should be involved CURD.

mybatis-plus Yes mybatis Enhancements on , Reduced xml Configuration of , Almost no need to write xml You can make a single table
CURD, It's very convenient , It greatly provides the efficiency of development . We write programs to make life easier .

<> What is? mybatis-plus?

MyBatis-Plus( abbreviation MP) It's a MyBatis Enhanced tools for , stay MyBatis On the basis of, only enhance without change , To simplify development , Born to improve efficiency .

MyBatis-Plus stay MyBatis A coat was put over it , Single table CURD Almost all operations can be performed by MyBatis-Plus
Substitute execution . It also provides various query methods , Paging behavior . As a user, there is no need to write xml, Direct call MyBatis-Plus Provided API That's it .

<> Quick start

You can start in a few minutes MP, The premise is to be familiar with mybatis,spring or spring boot, maven, master lambda Expressions can improve efficiency .

Prepare the environment :

* have Java Development environment and corresponding IDE
* be familiar with Spring Boot
* be familiar with Maven
Prepare data sheet

create database user; use user; CREATE TABLE `user` ( `id` int(11) NOT NULL
AUTO_INCREMENT, `name` varchar(50) DEFAULT NULL, `email` varchar(80) DEFAULT
NULL, `age` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB
AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; insert into user values(null,' Zhang San ',
'[email protected]',18);
IDEA Medium configuration maven

establish spring boot application

use Spring Initializr Quickly initialize a Spring Boot engineering
idea in File – New Project choice Spring Initializr Fill in project information after , Create project .

Project information

Dependent selection SQL Medium MyBatis Framework

Define project name and path

<>pom File add mybatis-plus rely on
<!-- mybatis-plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>
mybatis-plus-boot-starter</artifactId> <version>3.0.5</version> </dependency>
<!-- mysql drive --> <dependency> <groupId>mysql</groupId> <artifactId>
mysql-connector-java</artifactId> <version>8.0.25</version> </dependency>
application.properties or application.yml Add database configuration
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/user
spring.datasource.username=root spring.datasource.password=root
or
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql
://127.0.0.1:3306/user username: root password: root
<> Create entity class

@TableId Set primary key , IdType.AUTO Generate primary keys using automatic growth

<> establish mapper Interface

inherit MyBatis Plus Medium BaseMapper , stay UserMapper Used in MP Methods in , real
present CURD.

<> add to @MapperScan scanning Mapper folder

<> test

view the database

<> to configure mybatis journal
mybatis-plus: configuration: log-impl:
org.apache.ibatis.logging.stdout.StdOutImpl

<>CRUD Basic Usage

CRUD The operation is from BaseMapper Methods in .BaseMapper Communist Party of China 17 Methods ,CRUD Operations have multiple methods with different parameters . inherit
BaseMapper You can use one of these methods .

BaseMapper Method list :

<>insert operation

notes :insert() Return value int, Number of rows successfully inserted , Number of successful records .getId() Get primary key value , After the number of rows is successfully affected, the primary key value will be automatically assigned to the primary key of the entity class id.

<>update operation

Update previous database

Updated database

be careful :null The fields of are not updated

Not set email Field of , So at this time email The fields are null

SQL Statement is not set email

database

<>delete operation

There are multiple ways to delete
deleteById: Delete by primary key

according to Map Delete condition in

notes : Delete condition encapsulated in Map in ,key Is column name ,value Is value , Multiple key between and join .

journal :

Batch delete

Delete the id Put List , Pass to deleteBatchIds()

journal :

<>select operation

notes : No query results , No error will be reported .

journal

Batch query record

notes : according to id Query record , Put multiple items to be queried id Deposit to List, call selectBatchIds(),

journal

use Map Conditional query
The condition field to be queried put reach Map,key Is a field ,value Is the condition value . Multiple conditions are and join . call selectByMap(), afferent Map
As a parameter , The return value is List aggregate .

journal

Technology