性能分析插件

我们在平时的开发中,会遇到一些慢sql,测试,druid

MP(MyBatisPlus)也提供性能分析插件,如果超过这个时间就停止

不过官方在3.2版本的时候取消了,原因如下

条件构造器

十分重要: Wrapper

我们写一些复杂查询的时候

首先创建一个测试类

  1. @SpringBootTest
  2. public class MyBatisPlusWrapperTest {
  3. @Autowired
  4. private AirMapper airMapper;
  5. }
  1. // 查询一些用户:
  2. // 查询一下pm10为22且monitoring_station不为空的用户,
  3. @Test
  4. public void test1(){
  5. QueryWrapper<Air> wrapper = new QueryWrapper<>();
  6. wrapper.isNotNull("monitoring_station")//数据库中的名字,而不是实体类中的名字
  7. .eq("pm10",22);
  8. List<Air> airList = airMapper.selectList(wrapper);//可以对比下map的查询
  9. airList.forEach(System.out::println);//循环遍历输出
  10. }
  11. //查询单个用户
  12. @Test
  13. public void test2() {
  14. QueryWrapper<Air> wrapper = new QueryWrapper<>();
  15. wrapper.eq("id",222);
  16. airMapper.selectOne(wrapper);
  17. }
  18. //Butween And
  19. //查询pm25在40-60之间的用户和数量
  20. @Test
  21. public void test3() {
  22. QueryWrapper<Air> wrapper = new QueryWrapper<>();
  23. wrapper.between("pm25",40,60);//区间
  24. airMapper.selectList(wrapper).forEach(System.out::println);
  25. System.out.println(airMapper.selectCount(wrapper));//查询结果数
  26. }
  27. //模糊查询
  28. //查询monitor_station中带"站"的,切不带"哈"的
  29. @Test
  30. public void test4() {
  31. QueryWrapper<Air> wrapper = new QueryWrapper<>();
  32. wrapper.like("monitoring_station","站").notLike("monitoring_station","哈");
  33. airMapper.selectList(wrapper).forEach(System.out::println);
  34. }
  35. //查询以哈开头切以站结尾的 哈% %站
  36. @Test
  37. public void test5() {
  38. QueryWrapper<Air> wrapper = new QueryWrapper<>();
  39. wrapper.likeLeft("monitoring_station","站").likeRight("monitoring_station","哈");
  40. airMapper.selectList(wrapper).forEach(System.out::println);
  41. }
  42. //嵌入sql查询
  43. @Test
  44. public void test6() {
  45. QueryWrapper<Air> wrapper = new QueryWrapper<>();
  46. wrapper.inSql("district_id","select id from air where district_id = id");
  47. airMapper.selectObjs(wrapper).forEach(System.out::println);
  48. }
  49. //多表查询
  50. //通过id进行排序
  51. @Test
  52. public void test7() {
  53. QueryWrapper<Air> wrapper = new QueryWrapper<>();
  54. wrapper.orderByDesc("id");
  55. airMapper.selectList(wrapper).forEach(System.out::println);
  56. }

代码生成器

导入依赖

在3.0.3版本以后代码生成器需要手动添加依赖

  1. <dependency>
  2. <groupId>com.baomidou</groupId>
  3. <artifactId>mybatis-plus-generator</artifactId>
  4. <version>3.3.2</version>
  5. </dependency>

根据前端的模板引擎导入相应依赖(我没写前端页面就随便导入了一个velocity的)记住一定要加入其中一个,否则会报错

具体配置代码

  1. package com.cloudcentury.mybatis;
  2. import com.baomidou.mybatisplus.annotation.DbType;
  3. import com.baomidou.mybatisplus.annotation.FieldFill;
  4. import com.baomidou.mybatisplus.annotation.IdType;
  5. import com.baomidou.mybatisplus.generator.AutoGenerator;
  6. import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
  7. import com.baomidou.mybatisplus.generator.config.GlobalConfig;
  8. import com.baomidou.mybatisplus.generator.config.PackageConfig;
  9. import com.baomidou.mybatisplus.generator.config.StrategyConfig;
  10. import com.baomidou.mybatisplus.generator.config.po.TableFill;
  11. import com.baomidou.mybatisplus.generator.config.rules.DateType;
  12. import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
  13. import java.util.ArrayList;
  14. /**
  15. * ClassName:臭狗屎王溥松
  16. * Package:com.cloudcentury.mybatis
  17. *
  18. * @date:2020/8/7 10:22
  19. * @author:2628710400@qq.com Description:
  20. */
  21. public class CodeAuto {
  22. public static void main(String[] args) {
  23. //需要构建一个代码自动生成器对象
  24. AutoGenerator ag = new AutoGenerator();//代码生辰器对象
  25. //配置执行策略
  26. //1.全局配置
  27. GlobalConfig gc = new GlobalConfig();//全局配置对象
  28. String property = System.getProperty("user.dir");//获取项目名称
  29. System.out.println(property);
  30. gc.setOutputDir(property+"/src/main/java");//设置代码存放路径
  31. gc.setAuthor("臭狗屎");//设置作者
  32. gc.setOpen(false);//设置是否打开资源管理器(生成完毕后)
  33. gc.setFileOverride(false);//是否覆盖代码
  34. gc.setServiceName("%sService");//去掉Service的I前缀
  35. gc.setIdType(IdType.AUTO);//设置id自动生成类型
  36. gc.setDateType(DateType.ONLY_DATE);//日期时间,仅仅时间
  37. gc.setSwagger2(false);//是否设置swagger
  38. ag.setGlobalConfig(gc);//将全局配置放到里面
  39. //设置数据源
  40. DataSourceConfig desc = new DataSourceConfig();//数据源配置对象
  41. //设置url
  42. desc.setUrl("jdbc:mysql://localhost:3306/air?characterEncoding=utf8&serverTimezone=GMT");
  43. desc.setDriverName("com.mysql.cj.jdbc.Driver");//设置驱动
  44. desc.setUsername("root");//设置用户名
  45. desc.setPassword("12345");//设置密码
  46. desc.setDbType(DbType.MYSQL);//设置数据库类型
  47. ag.setDataSource(desc);//将数据源放到里面
  48. //包的配置
  49. //说白了就是说需要生成那些包,叫什么
  50. PackageConfig pc = new PackageConfig();//包配置对象
  51. pc.setModuleName("com");//模块名字
  52. pc.setParent("com.cloudcentury");//父模块名字
  53. pc.setEntity("entity");//Entity包的名字
  54. pc.setMapper("mapper");//mapper包的名字
  55. pc.setService("service");//service包的名字
  56. pc.setController("controller");//controller包的名字
  57. ag.setPackageInfo(pc);//将包的配置放到里面
  58. //策略配置
  59. StrategyConfig sc = new StrategyConfig();
  60. sc.setInclude("air","district"); //设置要映射的表名,这个一定要设置的
  61. sc.setNaming(NamingStrategy.underline_to_camel);//设置名字下划线转大写
  62. sc.setColumnNaming(NamingStrategy.underline_to_camel);//设置列明下划线转大写
  63. sc.setEntityLombokModel(true);//自动生成lombok
  64. sc.setLogicDeleteFieldName("deleted");//逻辑删除的名字
  65. //自动填充配置
  66. TableFill monitor_time = new TableFill("monitor_time", FieldFill.INSERT);//执行插入是更新时间
  67. TableFill last_modify_time = new TableFill("last_modify_time", FieldFill.INSERT);//执行更新时执行的操作
  68. ArrayList<TableFill> tableFills = new ArrayList<>();//创建一个List
  69. tableFills.add(monitor_time);
  70. tableFills.add(last_modify_time);
  71. sc.setTableFillList(tableFills);//这里只有这一个用list的方法
  72. sc.setVersionFieldName("version"); //乐观锁的配置
  73. sc.setRestControllerStyle(true);//开启rest式的驼峰命名
  74. sc.setControllerMappingHyphenStyle(true);//下划线命名:localhost:8080/hello_id_2
  75. ag.setStrategy(sc);//策略配置对象
  76. ag.execute();//执行
  77. }
  78. }

MyBatisPlus性能分析插件,条件构造器,代码自动生成器详解的更多相关文章

  1. MySQL性能分析、及调优工具使用详解

    本文汇总了MySQL DBA日常工作中用到的些工具,方便初学者,也便于自己查阅. 先介绍下基础设施(CPU.IO.网络等)检查的工具: vmstat.sar(sysstat工具包).mpstat.op ...

  2. Mybatis-plus<一> Springboot框架使用MybatisPlus代码自动生成器

    Mybatis-plus<一> Springboot框架使用MybatisPlus代码自动生成器 Mybatis-plus官网: https://mp.baomidou.com/ Demo ...

  3. MyBatis-plus 代码自动生成器

    MyBatis-plus  代码自动生成器 1.添加pom文件依赖 <!-- Mybatis-Plus 自动生成实体类--> <dependency> <groupId& ...

  4. Mybatis-Plus03 代码自动生成器

    先看完Mybatis-Plus01和Mybatis-Plus02再看Mybatis-Plus03 AutoGenerator 是 MyBatis-Plus 的代码生成器,通过 AutoGenerato ...

  5. 专门为小白准备的入门级mybatis-plus-generator代码自动生成器,提高开发效率。值得收藏

    引入依赖 <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-g ...

  6. 单元测试系列之四:Sonar平台中项目主要指标以及代码坏味道详解

    更多原创测试技术文章同步更新到微信公众号 :三国测,敬请扫码关注个人的微信号,感谢! 原文链接:http://www.cnblogs.com/zishi/p/6766994.html 众所周知Sona ...

  7. php调用C代码的方法详解和zend_parse_parameters函数详解

    php调用C代码的方法详解 在php程序中需要用到C代码,应该是下面两种情况: 1 已有C代码,在php程序中想直接用 2 由于php的性能问题,需要用C来实现部分功能   针对第一种情况,最合适的方 ...

  8. Understand:高效代码静态分析神器详解(转)

    之前用Windows系统,一直用source insight查看代码非常方便,但是年前换到mac下面,虽说很多东西都方便了,但是却没有了静态代码分析工具,很幸运,前段时间找到一款比source ins ...

  9. Understand:高效代码静态分析神器详解(一)

    Understand:高效代码静态分析神器详解(一) Understand   之前用Windows系统,一直用source insight查看代码非常方便,但是年前换到mac下面,虽说很多东西都方便 ...

随机推荐

  1. POJ 1057 File Mapping 最详细的解题报告

    题目来源:POJ 1057 File Mapping 题目大意:像我的电脑那样显示文件夹和文件信息,其中在同一级目录内,文件夹排在文件的前面并且文件夹的顺序不变,同一级目录中文件按字母序排列.文件以‘ ...

  2. JavaScript定时器及回调用法

    JavaScript定时器及回调用法 循环定时任务 // 假设现在有这样一个需求:我需要请求一个接口,根据返回结果判断需不需要重复请求,直到达到某一条件为止,停止请求执行某操作 <script ...

  3. 测试人员应该掌握的oracle知识体系

    闲来无事,总结了一下,软件测试人员应该掌握的基本的oracle数据库知识体系 1.安装 1.1 oracle安装 1.2 oracle升级 1.3 oracle补丁 2.管理 2.1数据库创建(dbc ...

  4. Python Ethical Hacking - BACKDOORS(4)

    REVERSE_BACKDOOR - cd command Access file system: cd command changes current working directory. It h ...

  5. Python Ethical Hacking - VULNERABILITY SCANNER(6)

    EXPLOITATION - XSS VULNS EXPLOITING XSS Run any javascript code. Beef framework can be used to hook ...

  6. js 判断传入参数是域名还是地址

    var get = function(url) { if(location.protocol === "http") { return url; } var reg = /^(ht ...

  7. ORACLE_19c用户密码登录失败的问题以及ORA-28040

    测试环境19c 本地登录无异常,创建测试用户,电脑Plsql登录提示报错ORA-28040,处理后再次登录提示密码错误,最后重置密码再次登录OK? 通过这个问题再次测试及反思: 1.ORA-28040 ...

  8. scrapy分布式浅谈+京东示例

    scrapy分布式浅谈+京东示例: 学习目标: 分布式概念与使用场景 浅谈去重 浅谈断点续爬 分布式爬虫编写流程 基于scrapy_redis的分布式爬虫(阳关院务与京东图书案例) 环境准备: 下载r ...

  9. Merging 和 Rebasing 的大比拼

    虽然 merging 和 rebasing 在 git 中相似时,但他们提供不同的功能.为了让你的历史尽可能的干净和完整,你应该知道以下几点. git rebase 命令已 神奇的 Git voodo ...

  10. Oracle DataGuard故障转移(failover)后使用RMAN还原失败的主库

    (一)DG故障转移后切换为备库的方法 在DG执行故障转移之后,主库与从库的关系就被破坏了.这个时候如果要恢复主从关系,可以使用下面的3种方法: 将失败的主库重新搭建为备库,该方法比较耗时: 使用数据库 ...