- pom.xml
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
- application.yml
mybatis-plus:
global-config:
db-config:
id-type: ASSIGN_ID
capital-mode: true
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
- 配置类
@Configuration
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
return interceptor;
}
}
- 测试
@Slf4j
@SpringBootTest
class ExecutionTest {
@Autowired
private StudentMapper studentMapper;
@Test
void test() {
studentMapper.selectList(new QueryWrapper<>());
studentMapper.deleteById(1L);
Student student = new Student();
student.setName("test_update");
studentMapper.insert(new Student(1L, "test", 12));
studentMapper.update(student, new QueryWrapper<Student>().eq("id", 1L));
try {
studentMapper.update(new Student(), new QueryWrapper<>());
} catch (MyBatisSystemException e) {
}
try {
studentMapper.delete(new QueryWrapper<>());
} catch (MyBatisSystemException e) {
System.err.println("执行了全表删除拦截,删除无效!异常:" + e.getMessage());
}
Assertions.assertTrue(CollectionUtils.isNotEmpty(studentMapper.selectList(new QueryWrapper<>())), "数据都被删掉了.(┬_┬)");
}
}
- 控制台
SELECT ID,NAME,AGE FROM student
==> Parameters:
<== Total: 0
DELETE FROM student WHERE ID=?
==> Parameters: 1(Long)
<== Updates: 0
INSERT INTO student ( ID, NAME, AGE ) VALUES ( ?, ?, ? )
==> Parameters: 1(Long), test(String), 12(Integer)
<== Updates: 1
UPDATE student SET NAME=? WHERE (id = ?)
==> Parameters: test_update(String), 1(Long)
<== Updates: 1
执行了全表删除拦截,删除无效!异常:nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error updating database. Cause: com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: Prohibition of full table deletion
### The error may exist in com/baomidou/samples/execution/mapper/StudentMapper.java (best guess)
### The error may involve com.baomidou.samples.execution.mapper.StudentMapper.delete
### The error occurred while executing an update
### Cause: com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: Prohibition of full table deletion
SELECT ID,NAME,AGE FROM student
==> Parameters:
<== Columns: ID, NAME, AGE
<== Row: 1, test_update, 12
<== Total: 1