MyBatis-Plus Overview and Getting Started
1. Introduction
What is it? MyBatis is meant to simplify JDBC operations! Simplify MyBatis!
2. Features
- No intrusion : only enhance and do not change, the introduction of it will not affect the existing project, it is silky smooth
- Low loss : basic CURD will be automatically injected at startup, performance is basically no loss, direct object-oriented operation
- Powerful CRUD operations : 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 to meet various usage needs - Support Lambda form call : through Lambda expressions, you can easily write all kinds of query conditions, no need to worry about writing wrong fields
- Supports automatic primary key generation : supports up to 4 primary key strategies (including a distributed unique ID generator-Sequence), which can be configured freely, which perfectly solves the primary key problem
- 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 plugin 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 intelligent analysis and blocking of delete and update operations of the entire table, and can also customize interception rules to prevent misoperations
Three, quick start
Address: https://mp.baomidou.com/guide/quick-start.html Initialization project
Steps:
1. Create the database mybatis_plus
2. Create the user table
DROP TABLE IF EXISTS user;
CREATE TABLE user
(
id BIGINT(20) NOT NULL COMMENT '主键ID',
name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
age INT(11) NULL DEFAULT NULL COMMENT '年龄',
email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (id)
);
DELETE FROM user;
INSERT INTO user (id, name, age, email) VALUES
(1, 'Jone', 18, '[email protected]'),
(2, 'Jack', 20, '[email protected]'),
(3, 'Tom', 28, '[email protected]'),
(4, 'Sandy', 21, '[email protected]'),
(5, 'Billie', 24, '[email protected]');
-- 真实开发中,version(乐观锁)、deleted(逻辑删除)、gmt_create、gmt_modified
3. Write the project and initialize the project! Use SpringBoot to initialize!
4. Import dependencies
SpringBoot:
<!-- 数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- mybatis-plus --> <!-- mybatis-plus 是自己开发,并非官方的! -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
<scope>provided</scope>
</dependency>
SpringMVC
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>3.4.2</version>
</dependency>
注意
: We use mybatis-plus to save us a lot of code. After introducing MyBatis-Plus, please do not introduce MyBatis and MyBatis-Spring again to avoid problems caused by version differences.
5. Connect to the database! This step is the same as mybatis!
# mysql 5 驱动不同 com.mysql.jdbc.Driver
# mysql 8 驱动不同com.mysql.cj.jdbc.Driver、需要增加时区的配置 serverTimezone=GMT%2B8
spring:
datasource:
url: jdbc:mysql://localhost:3306/mybatisplus?useSSl=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
username: root
password: "081020"
driver-class-name: com.mysql.cj.jdbc.Driver
Traditional way domain-dao (connect to mybatis, configure mapper.xml file)-service-controller
6. After using mybatis-plus
- domain
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
- mapper interface
// 在对应的Mapper上面继承基本的类 BaseMapper
@Repository // 代表持久层
public interface Usermapper extends BaseMapper<User> {
// 所有的CRUD操作都已经编写完成了
// 你不需要像以前的配置一大堆文件了
}
注意
: We need to scan all interfaces under our mapper package on the main startup class @MapperScan("com.latte.mapper")
- Test in the test class
@Autowired
// 继承了BaseMapper,所有的方法都来自己父类
// 我们也可以编写自己的扩展方法!
private Usermapper usermapper;
@Test
void contextLoads() {
// 参数是一个 Wrapper ,条件构造器,这里我们先不用 null
// 查询全部用户
List<User> users = usermapper.selectList(null);
for (User user : users) {
System.out.println(user);
}
}
- result
1. Who helped us write SQL? MyBatis-Plus has been written
2. Where did the method come from? MyBatis-Plus has been written