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

Posted 天宇轩-王

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot整合MybatisPlus3.X之SQL执行分析插件(十四)相关的知识,希望对你有一定的参考价值。

  • 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 [HikariProxyConnection@356338363 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 [org.apache.ibatis.session.defaults.DefaultSqlSession@73ba6fe6]
    Creating a new SqlSession
    SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1d96d872] was not registered for synchronization because synchronization is not active
    JDBC Connection [HikariProxyConnection@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 [org.apache.ibatis.session.defaults.DefaultSqlSession@1d96d872]
    Creating a new SqlSession
    SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@49cf9028] was not registered for synchronization because synchronization is not active
    JDBC Connection [HikariProxyConnection@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 [org.apache.ibatis.session.defaults.DefaultSqlSession@49cf9028]
    Creating a new SqlSession
    SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@408b87aa] was not registered for synchronization because synchronization is not active
    JDBC Connection [HikariProxyConnection@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

     

 

以上是关于SpringBoot整合MybatisPlus3.X之SQL执行分析插件(十四)的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot整合MybatisPlus3.X之自定义Mapper

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

springboot-mybatisplus笔记

Vue+SpringBoot超详细!一周开发一个SpringBoot + Vue+MybatisPlus+Shiro+JWT+Redis前后端分离个人博客项目!!!项目完结

Vue+SpringBoot超详细!一周开发一个SpringBoot + Vue+MybatisPlus+Shiro+JWT+Redis前后端分离个人博客项目!!!项目完结

阿昌教你看懂mybatisplus的sql语句的创建过程