- 官方文档
- 配置类
@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), [email protected](String)
*
* # 使用插件后,全表更新则报错
*/
@RequestMapping("/test1")
@ResponseBody
public String test1(){
User user = new User();
user.setId("999");
user.setUsername("custom_name");
user.setPassword("[email protected]");
// 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), [email protected](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("[email protected]");
userService.saveOrUpdate(user, wrapper);
return "success";
}
}