zoukankan      html  css  js  c++  java
  • MyBatisPlus性能分析插件,条件构造器,代码自动生成器详解

    性能分析插件

    我们在平时的开发中,会遇到一些慢sql,测试,druid
    MP(MyBatisPlus)也提供性能分析插件,如果超过这个时间就停止
    不过官方在3.2版本的时候取消了,原因如下

    条件构造器

    十分重要: Wrapper
    我们写一些复杂查询的时候

    首先创建一个测试类

    @SpringBootTest
    public class MyBatisPlusWrapperTest {
        @Autowired
        private AirMapper airMapper;   
    }
    

    实体类

    @Data
    @EqualsAndHashCode(callSuper = false)
    public class Air implements Serializable {
    
        private static final long serialVersionUID=1L;
    
          @TableId(value = "id", type = IdType.AUTO)
        private Long id;
    
        private Integer districtId;
    
          @TableField(fill = FieldFill.INSERT)
        private Date monitorTime;
    
        private Integer pm10;
    
        private Integer pm25;
    
        private String monitoringStation;
    
          @TableField(fill = FieldFill.INSERT)
        private Date lastModifyTime;
    
        @Version
        private Integer version;
    
        @TableLogic
        private Integer deleted;
    }
    

    建表语句(.sql文件)

    /*
     Navicat Premium Data Transfer
    
     Source Server         : wpsPractice1
     Source Server Type    : MySQL
     Source Server Version : 80020
     Source Host           : localhost:3306
     Source Schema         : air
    
     Target Server Type    : MySQL
     Target Server Version : 80020
     File Encoding         : 65001
    
     Date: 07/08/2020 11:44:27
    */
    
    SET NAMES utf8mb4;
    SET FOREIGN_KEY_CHECKS = 0;
    
    -- ----------------------------
    -- Table structure for air
    -- ----------------------------
    DROP TABLE IF EXISTS `air`;
    CREATE TABLE `air`  (
      `id` bigint unsigned NOT NULL,
      `district_id` int(0) NULL DEFAULT NULL,
      `monitor_time` datetime(0) NULL DEFAULT NULL,
      `pm10` int(0) NULL DEFAULT NULL,
      `pm25` int(0) NULL DEFAULT NULL,
      `monitoring_station` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
      `last_modify_time` datetime(0) NULL DEFAULT NULL,
      `version` int(0) NOT NULL DEFAULT 1,
      `deleted` int(0) NOT NULL DEFAULT 0,
      PRIMARY KEY (`id`) USING BTREE
    ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
    
    -- ----------------------------
    -- Records of air
    -- ----------------------------
    INSERT INTO `air` VALUES (4, 4, '2020-06-04 00:00:00', 22, 23, '哈哈哈哈站', '2020-07-15 00:00:00', 1, 1);
    INSERT INTO `air` VALUES (5, 5, '2020-06-04 00:00:00', 22, 23, '哈哈哈哈站', '2020-07-15 00:00:00', 1, 0);
    INSERT INTO `air` VALUES (6, 6, '2020-06-04 00:00:00', 22, 23, '哈哈哈哈站', '2020-07-15 00:00:00', 1, 0);
    INSERT INTO `air` VALUES (7, 7, '2020-06-04 00:00:00', 22, 23, '休息休息站', '2020-08-19 00:00:00', 1, 0);
    INSERT INTO `air` VALUES (8, 8, '2020-08-20 00:00:00', 33, 44, '嘎嘎嘎嘎站', '2020-08-27 00:00:00', 1, 0);
    INSERT INTO `air` VALUES (9, 4, '2020-08-05 03:56:49', 33, 65, '哈马屁', '2020-08-05 03:56:49', 1, 0);
    INSERT INTO `air` VALUES (11, 11, '2020-08-05 03:51:08', 33, 65, '哈哈哈哈哈哈', '2020-08-05 03:51:08', 1, 0);
    INSERT INTO `air` VALUES (222, 11, '2020-08-06 15:57:42', 33, 44, '快乐', '2020-08-19 15:57:59', 1, 0);
    INSERT INTO `air` VALUES (1290858950387945474, 11, '2020-08-05 03:55:31', 33, 65, '哈哈哈哈哈哈', '2020-08-05 03:55:31', 1, 0);
    
    SET FOREIGN_KEY_CHECKS = 1;
    
    
    // 查询一些用户:
        // 查询一下pm10为22且monitoring_station不为空的用户,
        @Test
        public void test1(){
            QueryWrapper<Air> wrapper = new QueryWrapper<>();
            wrapper.isNotNull("monitoring_station")//数据库中的名字,而不是实体类中的名字
                    .eq("pm10",22);
            List<Air> airList = airMapper.selectList(wrapper);//可以对比下map的查询
            airList.forEach(System.out::println);//循环遍历输出
        }
        //查询单个用户
        @Test
        public void test2() {
            QueryWrapper<Air> wrapper = new QueryWrapper<>();
            wrapper.eq("id",222);
            airMapper.selectOne(wrapper);
        }
        //Butween And
        //查询pm25在40-60之间的用户和数量
        @Test
        public void test3() {
            QueryWrapper<Air> wrapper = new QueryWrapper<>();
            wrapper.between("pm25",40,60);//区间
            airMapper.selectList(wrapper).forEach(System.out::println);
            System.out.println(airMapper.selectCount(wrapper));//查询结果数
        }
        //模糊查询
        //查询monitor_station中带"站"的,切不带"哈"的
        @Test
        public void test4() {
            QueryWrapper<Air> wrapper = new QueryWrapper<>();
            wrapper.like("monitoring_station","站").notLike("monitoring_station","哈");
            airMapper.selectList(wrapper).forEach(System.out::println);
        }
        //查询以哈开头切以站结尾的 哈%  %站
        @Test
        public void test5() {
            QueryWrapper<Air> wrapper = new QueryWrapper<>();
            wrapper.likeLeft("monitoring_station","站").likeRight("monitoring_station","哈");
            airMapper.selectList(wrapper).forEach(System.out::println);
        }
        //嵌入sql查询
        @Test
        public void test6() {
            QueryWrapper<Air> wrapper = new QueryWrapper<>();
            wrapper.inSql("district_id","select id from air where district_id = id");
            airMapper.selectObjs(wrapper).forEach(System.out::println);
        }
        //多表查询
        //通过id进行排序
        @Test
        public void test7() {
            QueryWrapper<Air> wrapper = new QueryWrapper<>();
            wrapper.orderByDesc("id");
            airMapper.selectList(wrapper).forEach(System.out::println);
        }
    

    代码生成器

    导入依赖

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

    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-generator</artifactId>
        <version>3.3.2</version>
    </dependency>
    

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

    具体配置代码

    package com.cloudcentury.mybatis;
    
    import com.baomidou.mybatisplus.annotation.DbType;
    import com.baomidou.mybatisplus.annotation.FieldFill;
    import com.baomidou.mybatisplus.annotation.IdType;
    import com.baomidou.mybatisplus.generator.AutoGenerator;
    import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
    import com.baomidou.mybatisplus.generator.config.GlobalConfig;
    import com.baomidou.mybatisplus.generator.config.PackageConfig;
    import com.baomidou.mybatisplus.generator.config.StrategyConfig;
    import com.baomidou.mybatisplus.generator.config.po.TableFill;
    import com.baomidou.mybatisplus.generator.config.rules.DateType;
    import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
    
    import java.util.ArrayList;
    
    /**
     * ClassName:臭狗屎
     * Package:com.cloudcentury.mybatis
     *
     * @date:2020/8/7 10:22
     * @author:2628710400@qq.com Description:
     */
    public class CodeAuto {
        public static void main(String[] args) {
            //需要构建一个代码自动生成器对象
    
            AutoGenerator ag = new AutoGenerator();//代码生辰器对象
            //配置执行策略
            //1.全局配置
            GlobalConfig gc = new GlobalConfig();//全局配置对象
            String property = System.getProperty("user.dir");//获取项目名称
            System.out.println(property);
            gc.setOutputDir(property+"/src/main/java");//设置代码存放路径
            gc.setAuthor("臭狗屎");//设置作者
            gc.setOpen(false);//设置是否打开资源管理器(生成完毕后)
            gc.setFileOverride(false);//是否覆盖代码
            gc.setServiceName("%sService");//去掉Service的I前缀
            gc.setIdType(IdType.AUTO);//设置id自动生成类型
            gc.setDateType(DateType.ONLY_DATE);//日期时间,仅仅时间
            gc.setSwagger2(false);//是否设置swagger
            ag.setGlobalConfig(gc);//将全局配置放到里面
    
            //设置数据源
            DataSourceConfig desc = new DataSourceConfig();//数据源配置对象
            //设置url
            desc.setUrl("jdbc:mysql://localhost:3306/air?characterEncoding=utf8&serverTimezone=GMT");
            desc.setDriverName("com.mysql.cj.jdbc.Driver");//设置驱动
            desc.setUsername("root");//设置用户名
            desc.setPassword("12345");//设置密码
            desc.setDbType(DbType.MYSQL);//设置数据库类型
            ag.setDataSource(desc);//将数据源放到里面
    
            //包的配置
            //说白了就是说需要生成那些包,叫什么
            PackageConfig pc = new PackageConfig();//包配置对象
            pc.setModuleName("com");//模块名字
            pc.setParent("com.cloudcentury");//父模块名字
            pc.setEntity("entity");//Entity包的名字
            pc.setMapper("mapper");//mapper包的名字
            pc.setService("service");//service包的名字
            pc.setController("controller");//controller包的名字
            ag.setPackageInfo(pc);//将包的配置放到里面
    
            //策略配置
            StrategyConfig sc = new StrategyConfig();
            sc.setInclude("air","district"); //设置要映射的表名,这个一定要设置的
            sc.setNaming(NamingStrategy.underline_to_camel);//设置名字下划线转大写
            sc.setColumnNaming(NamingStrategy.underline_to_camel);//设置列明下划线转大写
            sc.setEntityLombokModel(true);//自动生成lombok
            sc.setLogicDeleteFieldName("deleted");//逻辑删除的名字
    
            //自动填充配置
            TableFill monitor_time = new TableFill("monitor_time", FieldFill.INSERT);//执行插入是更新时间
            TableFill last_modify_time = new TableFill("last_modify_time", FieldFill.INSERT);//执行更新时执行的操作
            ArrayList<TableFill> tableFills = new ArrayList<>();//创建一个List
            tableFills.add(monitor_time);
            tableFills.add(last_modify_time);
            sc.setTableFillList(tableFills);//这里只有这一个用list的方法
            sc.setVersionFieldName("version");  //乐观锁的配置
            sc.setRestControllerStyle(true);//开启rest式的驼峰命名
            sc.setControllerMappingHyphenStyle(true);//下划线命名:localhost:8080/hello_id_2
            ag.setStrategy(sc);//策略配置对象
            ag.execute();//执行
        }
    }
    
    
    我会写代码+代码改变世界=我会改变世界! 为什么要改变世界? 如果无法改变世界,那么,世界就会改变我......
  • 相关阅读:
    2019/3/20统计单词数
    2019/3/20日历问题
    2019/3/20计算器1
    2019/3/17素数因子
    2019/3/17日历问题2
    2019/2/14多项式输出
    2019/2/13打印华氏温度与摄氏温度对照表
    2019/2/12孪生素数
    2019/2/12开灯问题
    2019/2/11 6084问题
  • 原文地址:https://www.cnblogs.com/chougoushi/p/13451800.html
Copyright © 2011-2022 走看看