注意:本教程使用的数据库脚本、数据模型和环境信息请参考 “MyBatis Plus环境准备” 章节,点击下载示例源码。
在实际项目开发中,可能由于不小心或者有人恶意将整个表格的数据修改、或删除。这对项目/产品的影响是非常大的,可能会导致项目/产品失败。
MyBatis Plus 提供了 BlockAttackInnerInterceptor 插件,该插件可以阻止全表更新和删除操作。在一定程度上,保证了数据库数据的安全。下面将通过示例介绍怎样使用该插件:
使用 @Configuration 和 @Bean 注解配置 BlockAttackInnerInterceptor 插件,代码如下:
package com.hxstrive.mybatis_plus;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor paginationInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// 防止全表更新与删除
// 针对 update 和 delete 语句
interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
return interceptor;
}
}定义一个 SimpleMapper 接口,该接口继承 BaseMapper 接口。代码如下:
package com.hxstrive.mybatis_plus.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hxstrive.mybatis_plus.model.UserBean;
import java.util.List;
public interface SimpleMapper extends BaseMapper<UserBean> {
}下面将使用 @SpringBootTest 和 @RunWith 注解,实现一个简单的单元测试。验证 BlockAttackInnerInterceptor 插件功能,代码如下:
package com.hxstrive.mybatis_plus.plugins;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hxstrive.mybatis_plus.mapper.SimpleMapper;
import com.hxstrive.mybatis_plus.model.UserBean;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
class PluginDemo {
@Autowired
private SimpleMapper mapper;
@Test
void contextLoads() {
// 更新全表数据
UpdateWrapper<UserBean> wrapper = new UpdateWrapper<>();
UserBean userBean = new UserBean();
userBean.setFace(new byte[]{});
int result = mapper.update(userBean, wrapper);
System.out.println("result=" + result);
}
}运行上面代码,将抛出如下错误信息:
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: ### Error updating database. Cause: com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: Prohibition of table update operation ### The error may exist in com/hxstrive/mybatis_plus/mapper/SimpleMapper.java (best guess) ### The error may involve com.hxstrive.mybatis_plus.mapper.SimpleMapper.update ### The error occurred while executing an update ### Cause: com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: Prohibition of table update operation at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:92) at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:440) at com.sun.proxy.$Proxy63.update(Unknown Source) ... at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) Caused by: org.apache.ibatis.exceptions.PersistenceException:
上面的错误信息中,“Prohibition of table update operation” 禁止全表更新操作,这样就能阻止全表更新和删除操作。