Cynicism Cynicism
首页
  • 前端学习笔记

    • 《Vue》笔记
    • 《TypeScript 从零实现 axios》
    • TypeScript
    • JS设计模式总结
    • 小程序笔记
  • 后端学习笔记

    • 《JavaWeb》
    • 《SSM》
    • 《瑞吉外卖》
    • 《Git》
    • 《SpringCloud》
    • 《黑马点评》
    • 《Spring原理》
    • 《JVM》
    • 《Java并发编程》
    • 《学成在线》
  • HTML
  • CSS
  • 技术文档
  • GitHub技巧
  • Nodejs
  • 博客搭建
  • 面试
  • 常见问题
  • 实用技巧
  • 友情链接
实习
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Cynicism

Java后端学习中的IKUN
首页
  • 前端学习笔记

    • 《Vue》笔记
    • 《TypeScript 从零实现 axios》
    • TypeScript
    • JS设计模式总结
    • 小程序笔记
  • 后端学习笔记

    • 《JavaWeb》
    • 《SSM》
    • 《瑞吉外卖》
    • 《Git》
    • 《SpringCloud》
    • 《黑马点评》
    • 《Spring原理》
    • 《JVM》
    • 《Java并发编程》
    • 《学成在线》
  • HTML
  • CSS
  • 技术文档
  • GitHub技巧
  • Nodejs
  • 博客搭建
  • 面试
  • 常见问题
  • 实用技巧
  • 友情链接
实习
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • Spring简介
  • Bean的管理开发
  • SpringMVC简介
  • MyBatisPlus基础
  • MyBatisPlus进阶
    • 1. ActiveRecord
      • 1.1 开启AR之旅
      • 1.2 根据主键查询
      • 1.3 新增数据
      • 1.4 更新操作
      • 1.5 删除操作
      • 1.6 根据条件查询
    • 2. 插件
      • 2.1 mybatis插件机制
      • 2.2 执行分析插件
      • 2.3 性能分析插件
    • 3. 自动填充
      • 3.1 添加@TableField注解
      • 3.2 编写MyMetaObjectHandler
    • 4. MybatisX快速开发插件
  • 《SSM》笔记
cynicism
2023-05-10
目录

MyBatisPlus进阶

# 1. ActiveRecord

概述

ActiveRecord也属于ORM(对象关系映射)层,由Rails最早提出,遵循标准的ORM模型:表映射到记录,记 录映射到对象,字段映射到对象属性。配合遵循的命名和配置惯例,能够很大程度的快速实现模型的操作,而 且简洁易懂。

ActiveRecord的主要思想是:

  • 每一个数据库表对应创建一个类,类的每一个对象实例对应于数据库中表的一行记录;通常表的每个字段 在类中都有相应的Field;
  • ActiveRecord同时负责把自己持久化,在ActiveRecord中封装了对数据库的访问,即CURD;
  • ActiveRecord是一种领域模型(Domain Model),封装了部分业务逻辑;

# 1.1 开启AR之旅

在MP中,开启AR非常简单,只需要将实体对象继承 Model 即可。

public class User extends Model<User> {
    private Long id;
    private String userName;
    private String password;
    private String name;
    private Integer age;
    private String email;
}
1
2
3
4
5
6
7
8

# 1.2 根据主键查询

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperTest {
    @Test
    public void testAR() {
        User user = new User();
        user.setId(2L);
        User user2 = user.selectById();
        System.out.println(user2);
    }
}
1
2
3
4
5
6
7
8
9
10
11

# 1.3 新增数据

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperTest {
    @Test
    public void testAR() {
        User user = new User();
        user.setName("刘备");
        user.setAge(30);
        user.setPassword("123456");
        user.setUserName("liubei");
        user.setEmail("liubei@itcast.cn");
        boolean insert = user.insert();
        System.out.println(insert);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# 1.4 更新操作

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperTest {
    @Test
    public void testAR() {
        User user = new User();
        user.setId(8L);
        user.setAge(35);
        boolean update = user.updateById();
        System.out.println(update);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12

# 1.5 删除操作

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperTest {
@Test
    public void testAR() {
        User user = new User();
        user.setId(7L);
        boolean delete = user.deleteById();
        System.out.println(delete);
    }
}
1
2
3
4
5
6
7
8
9
10
11

# 1.6 根据条件查询

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperTest {
@Test
    public void testAR() {
        User user = new User();
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.le("age","20");
        List<User> users = user.selectList(userQueryWrapper);
        for (User user1 : users) {
        System.out.println(user1);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13

# 2. 插件

# 2.1 mybatis插件机制

MyBatis 允许你在已映射语句执行过程中的某一点进行拦截调用。默认情况下,MyBatis 允许使用插件来拦截的方法 调用包括:

  1. Executor (update, query, flushStatements, commit, rollback, getTransaction, close, isClosed)
  2. ParameterHandler (getParameterObject, setParameters)
  3. ResultSetHandler (handleResultSets, handleOutputParameters)
  4. StatementHandler (prepare, parameterize, batch, update, query)

我们看到了可以拦截Executor接口的部分方法,比如update,query,commit,rollback等方法,还有其他接口的 一些方法等。

总体概括为:

  1. 拦截执行器的方法
  2. 拦截参数的处理
  3. 拦截结果集的处理
  4. 拦截Sql语法构建的处理

# 2.2 执行分析插件

在MP中提供了对SQL执行的分析的插件,可用作阻断全表更新、删除的操作,注意:该插件仅适用于开发环境,不 适用于生产环境

SpringBoot配置:

@Bean
public SqlExplainInterceptor sqlExplainInterceptor(){
    SqlExplainInterceptor sqlExplainInterceptor = new SqlExplainInterceptor();
    List<ISqlParser> sqlParserList = new ArrayList<>();
    // 攻击 SQL 阻断解析器、加入解析链
    sqlParserList.add(new BlockAttackSqlParser());
    sqlExplainInterceptor.setSqlParserList(sqlParserList);
    return sqlExplainInterceptor;
}
1
2
3
4
5
6
7
8
9

当执行全表更新时,会抛出异常,这样有效防止了一些误操作

# 2.3 性能分析插件

性能分析拦截器,用于输出每条 SQL 语句及其执行时间,可以设置最大执行时间,超过时间会抛出异常。注意:该插件仅适用于开发环境,不适用于生产环境

配置:

<configuration>
    <plugins>
<!-- SQL 执行性能分析,开发环境使用,线上不推荐。 maxTime 指的是 sql 最大执行时长 -->
    <plugin
        interceptor="com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor">
        <property name="maxTime" value="100" />
        <!--SQL是否格式化 默认false-->
        <property name="format" value="true" />
    </plugin>
    </plugins>
</configuration>
1
2
3
4
5
6
7
8
9
10
11

# 3. 自动填充

有些时候我们可能会有这样的需求,插入或者更新数据时,希望有些字段可以自动填充数据,比如密码、version等。在MP中提供了这样的功能,可以实现自动填充。

# 3.1 添加@TableField注解

@TableField(fill = FieldFill.INSERT) //插入数据时进行填充
private String password;
1
2

为password添加自动填充功能,在新增数据时有效。

FieldFill提供了多种模式选择

public enum FieldFill {
/**
* 默认不处理
*/
    DEFAULT,
/**
* 插入时填充字段
*/
    INSERT,
/**
* 更新时填充字段
*/
    UPDATE,
/**
* 插入和更新时填充字段
*/
    INSERT_UPDATE
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# 3.2 编写MyMetaObjectHandler

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        Object password = getFieldValByName("password", metaObject);
        if(null == password){
        //字段为空,可以进行填充
        setFieldValByName("password", "123456", metaObject);
        }
    }
    @Override
    public void updateFill(MetaObject metaObject) {}
}
1
2
3
4
5
6
7
8
9
10
11
12
13

# 4. MybatisX快速开发插件

MybatisX 是一款基于 IDEA 的快速开发插件,为效率而生。

  • 安装方法:打开 IDEA,进入 File -> Settings -> Plugins -> Browse Repositories,输入 mybatisx 搜索并安装。
  • 功能:
  • Java 与 XML 调回跳转
  • Mapper 方法自动生成 XML
编辑 (opens new window)
#mybatis
上次更新: 2025/05/12, 04:51:03
MyBatisPlus基础

← MyBatisPlus基础

最近更新
01
JVM调优
06-03
02
Linux篇
03-30
03
Kafka篇
03-30
更多文章>
Theme by Vdoing | Copyright © 2023-2025 Cynicism | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式