MyBatisPlus
MyBatisPlus overview
brief introduction
MyBatis is meant to simplify JDBC operations! Official website: https://mp.baomidou.com/
MyBatis Plus simplifies MyBatis!
characteristic
-
No invasion: it is only enhanced without change, and its introduction will not affect the existing project, which is as smooth as silk
-
Low loss: basic CURD will be injected automatically upon startup, and there is basically no loss in performance. Direct object-oriented operation, BaseMapper
-
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 a more powerful condition constructor to meet various use needs. In the future, simple CRUD operations do not need to be written by itself!
-
Support Lambda form call: through Lambda expression, it is convenient to write various query conditions without worrying about wrong fields. It supports automatic generation of primary key: it supports up to four primary key strategies (including distributed unique ID generator - Sequence), which can be configured freely to perfectly solve the problem of primary key. It supports ActiveRecord mode:
-
It supports ActiveRecord formal calls, and the entity class only needs to inherit the 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: 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 (automatically generate code for you)
-
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
quick get start
Address: https://mp.baomidou.com/guide/quick-start.html# Initialization project
Using third-party components:
1. Import corresponding dependencies
2. Study how dependencies are configured
3. How to write the code
4. Improve and expand technical capability!
step
1. Create database mybatis_plus
2. Create user table
The corresponding database Schema script is as follows:
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) );
The corresponding database Data script is as follows:
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');
3. Write project, initialize project! Initialize with SpringBoot!
4. Import dependency
<!--Database driven--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <!-- mybatis-plus--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.2.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
Note: we can save a lot of code by using mybatis plus. Try not to import mybatis and mybatis plus at the same time! Version difference!
5. Connect to database! This step is the same as mybatis!
# mysql 5 drives different com mysql. jdbc. Driver # mysql 8 drives different com mysql. cj. jdbc. Driver #Time zone configuration needs to be added#serverTimezone=GMT%2B8 spring.datasource.username=root spring.datasource.password=123456 spring.datasource.url=jdbc:mysql://localhost:3306/mybatis-plus?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
6. The traditional method is POJO Dao (connect to mybatis and configure the mapper.xml file) - service controller
7. After using mybatis plus
Package structure:
- pojo
package com.liuyuan.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.format.annotation.DateTimeFormat; /** * @Author:LY * @Description:Entity class corresponding to database * @DateTime:Create on 2021/7/18 16:54 */ @Data @AllArgsConstructor @NoArgsConstructor public class User { private int id; private String name; private int age; private String email; }
-mapper interface
package com.liuyuan.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.liuyuan.pojo.User; import org.springframework.stereotype.Repository; /** * @Author:LY * @Description:Interface class of user table * @DateTime:Create on 2021/7/18 16:57 */ //Inherit the BaseMapper class on the corresponding Mapper @Repository//Represents the persistence layer public interface UserMapper extends BaseMapper<User> { //All CRUD operations have been written //You don't need to configure a lot of files like before }
Note that we need to scan all interfaces under our mapper package on the main startup class
@MapperScan("com.liuyuan.mapper")
- Test in test class
package com.liuyuan; import com.liuyuan.mapper.UserMapper; import com.liuyuan.pojo.User; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.util.List; @SpringBootTest class MybatispulsStudyApplicationTests { @Autowired private UserMapper userMapper; @Test void contextLoads() { List<User> users=userMapper.selectList(null); users.forEach(System.out::println); } }
- Operation results