use of org.mybatis.spring.MyBatisSystemException in project demo-SpringBoot by Max-Qiu.
the class TestOther method testOperationAllTable.
/**
* 防止操作全表
*
* 插件内添加 interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
*
* 可在遇到全表操作时,抛出异常
*/
@Test
void testOperationAllTable() {
userMapper.selectList(new QueryWrapper<>());
userMapper.deleteById(1L);
userMapper.insert(new User().setAge(18).setEmail("13"));
User user = new User();
user.setUsername("test_update");
userMapper.update(user, new QueryWrapper<User>().eq("id", 1L));
try {
userMapper.update(new User().setAge(18), new QueryWrapper<>());
} catch (MyBatisSystemException e) {
System.out.println("发现全表更新");
}
try {
userMapper.delete(new QueryWrapper<>());
} catch (MyBatisSystemException e) {
System.out.println("发现全表删除");
}
List<User> list = userMapper.selectList(new QueryWrapper<>());
if (list.size() == 0) {
System.out.println("数据都被删掉了");
} else {
System.out.println("数据还在");
}
}
use of org.mybatis.spring.MyBatisSystemException in project spring by mybatis.
the class MapperFactoryBeanTest method testAddToConfigFalse.
// will fail because TestDao's mapper config is never loaded
@Test
void testAddToConfigFalse() throws Throwable {
try {
// the default SqlSessionFactory in AbstractMyBatisSpringTest is created with an explicitly
// set MapperLocations list, so create a new factory here that tests auto-loading the
// config
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
// mapperLocations properties defaults to null
factoryBean.setDataSource(dataSource);
SqlSessionFactory sqlSessionFactory = factoryBean.getObject();
assertThrows(org.apache.ibatis.binding.BindingException.class, () -> find(new SqlSessionTemplate(sqlSessionFactory), false));
// fail("TestDao's mapper xml should not be loaded");
} catch (MyBatisSystemException mbse) {
// unwrap exception so the exact MyBatis exception can be tested
throw mbse.getCause();
} finally {
// connection not used; force close to avoid failing in validateConnectionClosed()
connection.close();
}
}
use of org.mybatis.spring.MyBatisSystemException in project mybatis-plus-samples by baomidou.
the class ExecutionTest method test.
@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<>())), "数据都被删掉了.(┬_┬)");
}
use of org.mybatis.spring.MyBatisSystemException in project tutorials-java by Artister.
the class ExecutionTest method test.
@Test
public 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) {
}
Assert.notEmpty(studentMapper.selectList(new QueryWrapper<>()), "数据都被删掉了.(┬_┬)");
}
Aggregations