百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 编程网 > 正文

MyBatisPlus性能分析插件,条件构造器,代码自动生成器详解

yuyutoo 2024-10-12 00:04 7 浏览 0 评论

作者 | Asong臭狗屎

来源 | cnblogs.com/chougoushi/p/13451800.html

1、性能分析插件

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

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

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



2、条件构造器

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


(1)首先创建一个测试类


@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、代码生成器

(1)导入依赖


在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();//执行
    }
}

相关推荐

Mysql和Oracle实现序列自增(oracle创建序列的sql)

Mysql和Oracle实现序列自增/*ORACLE设置自增序列oracle本身不支持如mysql的AUTO_INCREMENT自增方式,我们可以用序列加触发器的形式实现,假如有一个表T_WORKM...

关于Oracle数据库12c 新特性总结(oracle数据库19c与12c)

概述今天主要简单介绍一下Oracle12c的一些新特性,仅供参考。参考:http://docs.oracle.com/database/121/NEWFT/chapter12102.htm#NEWFT...

MySQL CREATE TABLE 简单设计模板交流

推荐用MySQL8.0(2018/4/19发布,开发者说同比5.7快2倍)或同类型以上版本....

mysql学习9:创建数据库(mysql5.5创建数据库)

前言:我也是在学习过程中,不对的地方请谅解showdatabases;#查看数据库表createdatabasename...

MySQL面试题-CREATE TABLE AS 与CREATE TABLE LIKE的区别

执行"CREATETABLE新表ASSELECT*FROM原表;"后,新表与原表的字段一致,但主键、索引不会复制到新表,会把原表的表记录复制到新表。...

Nike Dunk High Volt 和 Bright Spruce 预计将于 12 月推出

在街上看到的PandaDunk的超载可能让一些球鞋迷们望而却步,但Dunk的浪潮仍然强劲,看不到尽头。我们看到的很多版本都是为女性和儿童制作的,这种新配色为后者引入了一种令人耳目一新的新选择,而...

美国多功能舰载雷达及美国海军舰载多功能雷达系统技术介绍

多功能雷达AN/SPY-1的特性和技术能力,该雷达已经在美国海军服役了30多年,其修改-AN/SPY-1A、AN/SPY-1B(V)、AN/SPY-1D、AN/SPY-1D(V),以及雷神...

汽车音响怎么玩,安装技术知识(汽车音响怎么玩,安装技术知识视频)

全面分析汽车音响使用或安装技术常识一:主机是大多数人最熟习的音响器材,有关主机的各种性能及规格,也是耳熟能详的事,以下是一些在使用或安装时,比较需要注意的事项:LOUDNESS:几年前的主机,此按...

【推荐】ProAc Response系列扬声器逐个看

有考牌(公认好声音)扬声器之称ProAcTablette小音箱,相信不少音响发烧友都曾经,或者现在依然持有,正当大家逐渐掌握Tablette的摆位设定与器材配搭之后,下一步就会考虑升级至表现更全...

#本站首晒# 漂洋过海来看你 — BLACK&amp;DECKER 百得 BDH2000L无绳吸尘器 开箱

作者:初吻给了烟sco混迹张大妈时日不短了,手没少剁。家里有了汪星人,吸尘器使用频率相当高,偶尔零星打扫用卧式的实在麻烦(汪星人:你这分明是找借口,我掉毛是满屋子都有,铲屎君都是用卧式满屋子吸的,你...

专题|一个品牌一件产品(英国篇)之Quested(罗杰之声)

Quested(罗杰之声)代表产品:Q212FS品牌介绍Quested(罗杰之声)是录音监听领域的传奇品牌,由英国录音师RogerQuested于1985年创立。在成立Quested之前,Roger...

常用半导体中英对照表(建议收藏)(半导体英文术语)

作为一个源自国外的技术,半导体产业涉及许多英文术语。加之从业者很多都有海外经历或习惯于用英文表达相关技术和工艺节点,这就导致许多英文术语翻译成中文后,仍有不少人照应不上或不知如何翻译。为此,我们整理了...

Fyne Audio F502SP 2.5音路低音反射式落地音箱评测

FyneAudio的F500系列,有新成员了!不过,新成员不是新的款式,却是根据原有款式提出特别版。特别版产品在原有型号后标注了SP字样,意思是SpecialProduction。Fyne一共推出...

有哪些免费的内存数据库(In-Memory Database)

以下是一些常见的免费的内存数据库:1.Redis:Redis是一个开源的内存数据库,它支持多种数据结构,如字符串、哈希表、列表、集合和有序集合。Redis提供了快速的读写操作,并且支持持久化数据到磁...

RazorSQL Mac版(SQL数据库查询工具)

RazorSQLMac特别版是一款看似简单实则功能非常出色的SQL数据库查询、编辑、浏览和管理工具。RazorSQLformac特别版可以帮你管理多个数据库,支持主流的30多种数据库,包括Ca...

取消回复欢迎 发表评论: