Search in sources :

Example 1 with MyBatisSystemException

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("数据还在");
    }
}
Also used : User(com.maxqiu.demo.entity.User) MyBatisSystemException(org.mybatis.spring.MyBatisSystemException) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with MyBatisSystemException

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();
    }
}
Also used : SqlSessionTemplate(org.mybatis.spring.SqlSessionTemplate) MyBatisSystemException(org.mybatis.spring.MyBatisSystemException) SqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory) SqlSessionFactoryBean(org.mybatis.spring.SqlSessionFactoryBean) AbstractMyBatisSpringTest(org.mybatis.spring.AbstractMyBatisSpringTest) Test(org.junit.jupiter.api.Test)

Example 3 with MyBatisSystemException

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<>())), "数据都被删掉了.(┬_┬)");
}
Also used : MyBatisSystemException(org.mybatis.spring.MyBatisSystemException) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) Student(com.baomidou.samples.execution.entity.Student) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with MyBatisSystemException

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<>()), "数据都被删掉了.(┬_┬)");
}
Also used : MyBatisSystemException(org.mybatis.spring.MyBatisSystemException) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) Student(com.baomidou.samples.execution.entity.Student) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

MyBatisSystemException (org.mybatis.spring.MyBatisSystemException)4 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)3 Test (org.junit.jupiter.api.Test)3 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)3 Student (com.baomidou.samples.execution.entity.Student)2 User (com.maxqiu.demo.entity.User)1 SqlSessionFactory (org.apache.ibatis.session.SqlSessionFactory)1 Test (org.junit.Test)1 AbstractMyBatisSpringTest (org.mybatis.spring.AbstractMyBatisSpringTest)1 SqlSessionFactoryBean (org.mybatis.spring.SqlSessionFactoryBean)1 SqlSessionTemplate (org.mybatis.spring.SqlSessionTemplate)1