• pom.xml

    1. <dependencies>
    2. <dependency>
    3. <groupId>org.springframework.boot</groupId>
    4. <artifactId>spring-boot-starter</artifactId>
    5. </dependency>
    6. <dependency>
    7. <groupId>com.baomidou</groupId>
    8. <artifactId>mybatis-plus-boot-starter</artifactId>
    9. <version>3.2.0</version>
    10. </dependency>
    11. <dependency>
    12. <groupId>org.projectlombok</groupId>
    13. <artifactId>lombok</artifactId>
    14. </dependency>
    15. <!-- https://mvnrepository.com/artifact/p6spy/p6spy -->
    16. <dependency>
    17. <groupId>p6spy</groupId>
    18. <artifactId>p6spy</artifactId>
    19. <version>3.8.0</version>
    20. </dependency>
    21. <dependency>
    22. <groupId>com.h2database</groupId>
    23. <artifactId>h2</artifactId>
    24. <scope>runtime</scope>
    25. </dependency>

    26. <dependency>
    27. <groupId>com.alibaba</groupId>
    28. <artifactId>fastjson</artifactId>
    29. <version>1.2.49</version>
    30. <scope>test</scope>
    31. </dependency>
    32. <!-- for testing -->
    33. <dependency>
    34. <groupId>org.springframework.boot</groupId>
    35. <artifactId>spring-boot-starter-test</artifactId>
    36. <scope>test</scope>
    37. </dependency>
    38. </dependencies>
  • application.yml

    1. spring:
    2. datasource:
    3. driver-class-name: com.p6spy.engine.spy.P6SpyDriver
    4. url: jdbc:p6spy:h2:tcp://192.168.180.115:19200/~/mem/test
    5. username: root
    6. password: test
  • 实体类

    1. @Data
    2. @TableName(value = "student")
    3. public class Student {

    4. private Long id;

    5. private String name;

    6. private Integer age;

    7. }

    8. @Mapper
    9. public interface StudentMapper extends BaseMapper<Student> {

    10. void deleteAll();

    11. }
  • 注入器及方法

    1. public class DeleteAll extends AbstractMethod {

    2. @Override
    3. public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
    4. /* 执行 SQL ,动态 SQL 参考类 SqlMethod */
    5. String sql = "delete from " + tableInfo.getTableName();
    6. /* mapper 接口方法名一致 */
    7. String method = "deleteAll";
    8. SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
    9. return this.addDeleteMappedStatement(mapperClass, method, sqlSource);
    10. }
    11. }

    12. @Component
    13. public class MySqlInjector extends DefaultSqlInjector {

    14. @Override
    15. public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
    16. List<AbstractMethod> methodList = super.getMethodList(mapperClass);
    17. //增加自定义方法
    18. methodList.add(new DeleteAll());
    19. return methodList;
    20. }
    21. }
  • 测试类

    1. @SpringBootTest
    2. class InjectorApplicationTests {

    3. @Autowired(required = false)
    4. private StudentMapper studentMapper;

    5. @Test
    6. public void test(){
    7. studentMapper.deleteAll();
    8. }

    9. }
  • 测试结果

    1. 2019-10-31 10:40:22.780 INFO 9484 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
    2. Consume Time0 ms 2019-10-31 10:40:22
    3. Execute SQLdelete from student

SpringBoot整合MybatisPlus3.X之SQL注入器(九)的更多相关文章

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

    pom.xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId& ...

  2. SpringBoot整合MybatisPlus3.X之自定义Mapper(十)

    pom.xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId& ...

  3. SpringBoot整合MyBatis-Plus3.1详细教程

    作者:Sans_ juejin.im/post/5cfa6e465188254ee433bc69 一.说明 Mybatis-Plus是一个Mybatis框架的增强插件,根据官方描述,MP只做增强不做改 ...

  4. SpringBoot整合MybatisPlus3.X之乐观锁(十三)

    主要适用场景 意图: 当要更新一条记录的时候,希望这条记录没有被别人更新 乐观锁实现方式: 取出记录时,获取当前version 更新时,带上这个version 执行更新时, set version = ...

  5. SpringBoot整合MybatisPlus3.X之Wrapper(五)

    官方文档说明: 以下出现的第一个入参boolean condition表示该条件是否加入最后生成的sql中 以下代码块内的多个方法均为从上往下补全个别boolean类型的入参,默认为true 以下出现 ...

  6. SpringBoot整合MybatisPlus3.X之分页插件(四)

    注:详细请看2.X博客中,3.X直接上代码. 建议装一个MybatisX插件,可以在Mapper和Xml来回切换 pom.xml <dependencies> <dependency ...

  7. SpringBoot整合MybatisPlus3.X之逻辑删除(三)

    pom.xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId& ...

  8. SpringBoot整合MybatisPlus3.X之Sequence(二)

    数据库脚本 DELETE FROM user; ​ INSERT INTO user (id, name, age, email) VALUES (, , 'test1@baomidou.com'), ...

  9. SpringBoot整合Mybatisplus3.x之CRUD(一)

    pom.xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId& ...

随机推荐

  1. python3在word文档中查找多行文字是否存在

    工作中碰到这样一个情况:有多个关键词存在文本文档txt中,想查找下在某个较大的word文档中,这些关键词是否都含有,没有关键词的显示出来. 因为关键词比较多,并且这个工作还是经常会有的,这个情况我试着 ...

  2. Spring 梳理 - 视图解析器 VS 视图(View,ViewResolver)

    View View接口表示一个响应给用户的视图,例如jsp文件,pdf文件,html文件等 该接口只有两个方法定义,分别表明该视图的ContentType和如何被渲染 Spring中提供了丰富的视图支 ...

  3. Linux 文件复制命令cp

    文件复制命令cp 命令格式:cp [-adfilprsu] 源文件(source) 目标文件(destination) cp [option] source1 source2 source3 ... ...

  4. HDU 1159——Common Subsequence(DP)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 题解 #include<iostream> #include<cstring> ...

  5. Thinkphp5.0终章

    thinkphp5.0最终总结 前期刚开始我是跟着b站上的千峰教育的视频走的,一路上做笔记进行深化与实际操作,中间因为不会开报错,并且视频里面也没有讲到怎么弄报错,因为是新手,那种出错了却不知道错在哪 ...

  6. Thinkphp5.0第四篇

    删除数据 当前模型删除 $user=UserModel::get(1); if($user->delete()){return '删除成功';} else{return '删除失败';} 根据主 ...

  7. C#刷遍Leetcode面试题系列连载(2): No.38 - 报数

    目录 前言 题目描述 相关话题 相似题目 解题思路: 运行结果: 代码要点: 参考资料: 文末彩蛋 前言 前文传送门: C# 刷遍 Leetcode 面试题系列连载(1) - 入门与工具简介 上篇文章 ...

  8. poll(2) 源码分析

    poll(2) poll(2) 系统调用的功能和 select(2) 类似:等待一个文件集合中的文件描述符就绪进行I/O操作. 使用 实现 select(2) 的局限性: 关注的文件描述符集合大小最大 ...

  9. 【SQL】 收入支出求盈亏

    求项目ID为1000的盈亏 表名为:T 字段:ID    P_ID   AMOUNT   TYPE(1:收入 2:支出) '

  10. Web性能优化:雅虎35条

    对web性能优化,一直知道是个很重要的方面,平时有注意到,但是对于雅虎35条是第一次听说,查了一下,发现平时都有用过,只是没有总结到一块,今天就总结一下吧. 雅虎35条: 1.[内容]尽量减少HTTP ...