SpringBoot整合MybatisPlus3.X之SQL执行分析插件(十四)

时间:2022-02-09 04:21:16

  • pom.xml

    <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>3.2.0</version>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
            </dependency>
            <dependency>
                <groupId>com.h2database</groupId>
                <artifactId>h2</artifactId>
                <scope>runtime</scope>
            </dependency>
    ?
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.49</version>
                <scope>test</scope>
            </dependency>
            <!-- for testing -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
  • 配置文件

    @Configuration
    public class MybatisPlusConfig {
    ?
    ?
        @Bean
        public SqlExplainInterceptor sqlExplainInterceptor(){
            SqlExplainInterceptor sqlExplainInterceptor = new SqlExplainInterceptor();
            List<ISqlParser> sqlParserList = new ArrayList<>();
            sqlParserList.add(new BlockAttackSqlParser());
            sqlExplainInterceptor.setSqlParserList(sqlParserList);
            return sqlExplainInterceptor;
        }
    ?
    ?
    }

     

  • 实体类

    @Data
    @TableName(value = "student")
    @NoArgsConstructor
    @AllArgsConstructor
    public class Student {
    ?
        private Long id;
    ?
        private String name;
    ?
        private Integer age;
    ?
    }
    ?
    @Mapper
    public interface StudentMapper extends BaseMapper<Student> {
    ?
    ?
    }
    ?

     

  • application.yml

    spring:
      datasource:
        driver-class-name: org.h2.Driver
        url: jdbc:h2:tcp://192.168.180.115:19200/~/mem/test
        username: root
        password: test
    ?
    mybatis-plus:
      global-config:
        db-config:
          id-type: id_worker
          capital-mode: true
      configuration:
        log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

     

  • 数据库SQL

-- noinspection SqlNoDataSourceInspectionForFile
?
DROP TABLE IF EXISTS student;
?
CREATE TABLE student
(
  id      BIGINT (20) NOT NULL COMMENT ‘主键ID‘,
  name    VARCHAR(30) NULL DEFAULT NULL COMMENT ‘姓名‘,
  age     INT (11) NULL DEFAULT NULL COMMENT ‘年龄‘,
  PRIMARY KEY (id)
);
  • 测试类

    @RunWith(SpringRunner.class)
    @SpringBootTest
    public  class AnalysisApplicationTests {
    ?
        private static final Logger LOGGER = LoggerFactory.getLogger(AnalysisApplicationTests.class);
    ?
        @Autowired(required = false)
        private StudentMapper studentMapper;
    ?
        @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<>()),"数据都被删掉了.(┬_┬)");
        }
    ?
    }
     
  • 测试结果

    JDBC Connection [[email protected] wrapping conn0: url=jdbc:h2:tcp://192.168.180.115:19200/~/mem/test user=ROOT] will not be managed by Spring
    ==>  Preparing: SELECT ID,NAME,AGE FROM student 
    ==> Parameters: 
    <==      Total: 0
    Closing non transactional SqlSession [[email protected]]
    Creating a new SqlSession
    SqlSession [[email protected]] was not registered for synchronization because synchronization is not active
    JDBC Connection [[email protected]897801829 wrapping conn0: url=jdbc:h2:tcp://192.168.180.115:19200/~/mem/test user=ROOT] will not be managed by Spring
    ==>  Preparing: DELETE FROM student WHERE ID=? 
    ==> Parameters: 1(Long)
    <==    Updates: 0
    Closing non transactional SqlSession [[email protected]]
    Creating a new SqlSession
    SqlSession [[email protected]] was not registered for synchronization because synchronization is not active
    JDBC Connection [[email protected]1876259196 wrapping conn0: url=jdbc:h2:tcp://192.168.180.115:19200/~/mem/test user=ROOT] will not be managed by Spring
    ==>  Preparing: INSERT INTO student ( ID, NAME, AGE ) VALUES ( ?, ?, ? ) 
    ==> Parameters: 1(Long), test(String), 12(Integer)
    <==    Updates: 1
    Closing non transactional SqlSession [[email protected]]
    Creating a new SqlSession
    SqlSession [[email protected]] was not registered for synchronization because synchronization is not active
    JDBC Connection [[email protected]527247308 wrapping conn0: url=jdbc:h2:tcp://192.168.180.115:19200/~/mem/test user=ROOT] will not be managed by Spring
    ==>  Preparing: UPDATE student SET NAME=? WHERE (id = ?) 
    ==> Parameters: test_update(String), 1(Long)
    <==    Updates: 1