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

java1234

共 8581字,需浏览 18分钟

 ·

2020-08-10 21:25

点击上方蓝色字体,选择“标星公众号”

优质文章,第一时间送达

  作 | Asong臭狗屎

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

66套java从入门到精通实战课程分享

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(0NULL DEFAULT NULL,
  `monitor_time` datetime(0NULL DEFAULT NULL,
  `pm10` int(0NULL DEFAULT NULL,
  `pm25` int(0NULL DEFAULT NULL,
  `monitoring_station` varchar(255CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `last_modify_time` datetime(0NULL DEFAULT NULL,
  `version` int(0NOT NULL DEFAULT 1,
  `deleted` int(0NOT 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 (44'2020-06-04 00:00:00'2223'哈哈哈哈站''2020-07-15 00:00:00'11);
INSERT INTO `air` VALUES (55'2020-06-04 00:00:00'2223'哈哈哈哈站''2020-07-15 00:00:00'10);
INSERT INTO `air` VALUES (66'2020-06-04 00:00:00'2223'哈哈哈哈站''2020-07-15 00:00:00'10);
INSERT INTO `air` VALUES (77'2020-06-04 00:00:00'2223'休息休息站''2020-08-19 00:00:00'10);
INSERT INTO `air` VALUES (88'2020-08-20 00:00:00'3344'嘎嘎嘎嘎站''2020-08-27 00:00:00'10);
INSERT INTO `air` VALUES (94'2020-08-05 03:56:49'3365'哈马屁''2020-08-05 03:56:49'10);
INSERT INTO `air` VALUES (1111'2020-08-05 03:51:08'3365'哈哈哈哈哈哈''2020-08-05 03:51:08'10);
INSERT INTO `air` VALUES (22211'2020-08-06 15:57:42'3344'快乐''2020-08-19 15:57:59'10);
INSERT INTO `air` VALUES (129085895038794547411'2020-08-05 03:55:31'3365'哈哈哈哈哈哈''2020-08-05 03:55:31'10);

SET FOREIGN_KEY_CHECKS = 1;



// 查询一些用户:
    // 查询一下pm10为22且monitoring_station不为空的用户,
    @Test
    public void test1(){
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.isNotNull("monitoring_station")//数据库中的名字,而不是实体类中的名字
                .eq("pm10",22);
        List airList = airMapper.selectList(wrapper);//可以对比下map的查询
        airList.forEach(System.out::println);//循环遍历输出
    }
    //查询单个用户
    @Test
    public void test2() {
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("id",222);
        airMapper.selectOne(wrapper);
    }
    //Butween And
    //查询pm25在40-60之间的用户和数量
    @Test
    public void test3() {
        QueryWrapper 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 wrapper = new QueryWrapper<>();
        wrapper.like("monitoring_station","站").notLike("monitoring_station","哈");
        airMapper.selectList(wrapper).forEach(System.out::println);
    }
    //查询以哈开头切以站结尾的 哈% %站
    @Test
    public void test5() {
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.likeLeft("monitoring_station","站").likeRight("monitoring_station","哈");
        airMapper.selectList(wrapper).forEach(System.out::println);
    }
    //嵌入sql查询
    @Test
    public void test6() {
        QueryWrapper 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 wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        airMapper.selectList(wrapper).forEach(System.out::println);
    }



3、代码生成器

(1)导入依赖


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


<dependency>
    <groupId>com.baomidougroupId>
    <artifactId>mybatis-plus-generatorartifactId>
    <version>3.3.2version>
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 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();//执行
    }
}



粉丝福利:108本java从入门到大神精选电子书领取

???

?长按上方二维码 2 秒
回复「1234」即可获取资料以及
可以进入java1234官方微信群



感谢点赞支持下哈 



浏览 36
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报