Mybatis-plus的初次使用

faith team

Mybatis-plus

MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

特性

  • 无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑

  • 损耗小:启动即会自动注入基本 CURD,性能基本无损耗,直接面向对象操作

  • 强大的 CRUD 操作:内置通用 Mapper、通用 Service,仅仅通过少量配置即可实现单表大部分 CRUD 操作,更有强大的条件构造器,满足各类使用需求

  • 支持 Lambda 形式调用:通过 Lambda 表达式,方便的编写各类查询条件,无需再担心字段写错
    支持主键自动生成:支持多达 4 种主键策略(内含分布式唯一 ID 生成器 - Sequence),可自由配置,完美解决主键问题

  • 支持 ActiveRecord 模式:支持 ActiveRecord 形式调用,实体类只需继承 Model 类即可进行强大的 CRUD 操作

  • 支持自定义全局通用操作:支持全局通用方法注入( Write once, use anywhere )

  • 内置代码生成器:采用代码或者 Maven 插件可快速生成 Mapper 、 Model 、 Service 、 Controller 层代码,支持模板引擎,更有超多自定义配置等您来使用

  • 内置分页插件:基于 MyBatis 物理分页,开发者无需关心具体操作,配置好插件之后,写分页等同于普通 List 查询

  • 分页插件支持多种数据库:支持 MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、Postgre、SQLServer 等多种数据库

  • 内置性能分析插件:可输出 Sql 语句以及其执行时间,建议开发测试时启用该功能,能快速揪出慢查询

  • 内置全局拦截插件:提供全表 delete 、 update 操作智能分析阻断,也可自定义拦截规则,预防误操作

支持数据库

  • mysql 、 mariadb 、 oracle 、 db2 、 h2 、 hsql 、 sqlite 、 postgresql 、 sqlserver
  • 达梦数据库 、 虚谷数据库 、 人大金仓数据库

官方文档地址

https://baomidou.com/pages/24112f/

1.1 导入依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>

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

1.2 编写实体类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.faith.domain;/**
* @Author: faith
* @Description: TODO
* @DateTime: 2023/5/31 16:12
**/

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
*@description:
*@author:faith
*@time:2023/5/3116:12
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("stu") //绑定表名
public class Student implements Serializable {
@TableId(value = "id")//绑定id
private Long id;
@TableField(value = "stuName")//绑定字段
private String stuName;
@TableField(value = "teacherId")
private Long teacherId;
}

1.3 编写Mapper

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.faith.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.faith.domain.Student;
import org.springframework.stereotype.Repository;

/**
*@description:
*@author:faith
*@time:2023/5/3116:14
*/
//在对应的mapper上实现泛型接口
@Repository
public interface UserMapper extends BaseMapper<Student> {

}

1.4 编写接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.faith.controller;
import com.faith.domain.Student;
import com.faith.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
*@description:
*@author:faith
*@time:2023/5/3116:23
*/
@RestController
@RequestMapping("api/hello")
public class HelloController {
@Autowired
private UserMapper mapper;
@RequestMapping("/select")
public void select(){
List<Student> list = mapper.selectList(null);
for (Student s:
list) {
System.out.println(s);
}
}
@RequestMapping("/insert")
public void insert(){
Student student = new Student();
student.setStuName("qweqwe");
mapper.insert(student);
System.out.println("1111");
}
}

注意事项

表中如果是BigInt的话 insert的时候会自动生成雪花算法

  • Title: Mybatis-plus的初次使用
  • Author: faith team
  • Created at: 2023-06-01 19:07:05
  • Updated at: 2025-11-29 09:01:08
  • Link: https://redefine.ohevan.com/2023/06/01/20230601/
  • License: This work is licensed under CC BY-NC-SA 4.0.
 Comments