• 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

     

  • 相关阅读:
    福大软工1816 · 第三次作业
    福大软工1816 · 第二次作业
    2018福大软工第一次作业
    20180823-软工实践第一次作业-自我介绍
    福大软工1816 · 第一次作业
    开博之作 · 简单的自我介绍
    2018软件工程实践第一次作业
    系列最终篇!
    含继承多态思想的四则运算器和简单工厂模式初步
    作业六 栈的使用和界面编程探索
  • 原文地址:https://www.cnblogs.com/dalianpai/p/11779676.html
Copyright © 2020-2023  润新知