Categories: MyBatis-Plus 教程

MyBatis-Plus 插件-防全表更新与删除插件

BlockAttackInnerInterceptor

针对 ​update和 ​delete语句,作用: 阻止恶意的全表更新删除

注入​MybatisPlusInterceptor​类,并配置​BlockAttackInnerInterceptor​拦截器

@Configuration
public class MybatisPlusConfig {
  @Bean
  public MybatisPlusInterceptor mybatisPlusInterceptor() {
    MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
    interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
    return interceptor;
  }
}

测试示例(全表更新)

@SpringBootTest
public class QueryWrapperTest {

  @Autowired
  private UserService userService;

  /**
  + SQL:UPDATE user  SET name=?,email=?;
  */  @Test
  public void test() {
    User user = new User();
    user.setId(999L);
    user.setName("custom_name");
    user.setEmail("xxx@mail.com");
    // com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: Prohibition of table update operation
    userService.saveOrUpdate(user, null);
  }
}

测试示例(部分更新)

@SpringBootTest
public class QueryWrapperTest {

  @Autowired
  private UserService userService;

  /**
  + SQL:UPDATE user  SET name=?, email=? WHERE id = ?;
  */  @Test
  public void test() {
    LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
    wrapper.eq(User::getId, 1);
    User user = new User();
    user.setId(10L);
    user.setName("custom_name");
    user.setEmail("xxx@mail.com");
    userService.saveOrUpdate(user, wrapper);
  }}

冒牌SEO

前端开发者,欢迎大家一起沟通和交流。

Share
Published by
冒牌SEO

Recent Posts

在 Chrome 中删除、允许和管理 Cookie

您可以选择删除现有 Cooki…

2 天 ago

自定义指令:聊聊vue中的自定义指令应用法则

今天我们来聊聊vue中的自定义…

1 周 ago

聊聊Vue中@click.stop和@click.prevent

一起来学下聊聊Vue中@cli…

2 周 ago

Nginx 基本操作:启动、停止、重启命令。

我们来学习Nginx基础操作:…

3 周 ago

Vue3:手动清理keep-alive组件缓存的方法

Vue3中手动清理keep-a…

3 周 ago

聊聊React和Vue组件更新的实现及区别

React 和 Vue 都是当…

4 周 ago