展开
拓展 关闭
订阅号推广码
GitHub
视频
公告栏 关闭

防全表更新与删除插件

@Configuration
public class MybatisPlusConfig {

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

}
  • 测试
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private UserService userService;

    /**
     * 全表更新
     * @return
     *
     * # 未使用插件时,可全表更新
     * UPDATE user SET username=?, password=?
     * custom_name(String), xxx@mail.com(String)
     *
     * # 使用插件后,全表更新则报错
     */
    @RequestMapping("/test1")
    @ResponseBody
    public String test1(){
        User user = new User();
        user.setId("999");
        user.setUsername("custom_name");
        user.setPassword("xxx@mail.com");
        // com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: Prohibition of table update operation
        userService.saveOrUpdate(user, null);
        return "success";
    }

    /**
     * 部分更新
     *
     * # 使用插件后,也可部分表更新
     * UPDATE user SET username=?, password=? WHERE (id = ?)
     * custom_name(String), xxx@mail.com(String), 1(Integer)
     */
    @RequestMapping("/test2")
    @ResponseBody
    public String test2(){
        LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(User::getId, 1);
        User user = new User();
        user.setId("10");
        user.setUsername("custom_name");
        user.setPassword("xxx@mail.com");
        userService.saveOrUpdate(user, wrapper);
        return "success";
    }

}
posted @ 2022-07-20 16:32  DogLeftover  阅读(148)  评论(0编辑  收藏  举报