MybatisPlus详解(四)

本文深入探讨了MybatisPlus的性能分析插件,解释了其作用、注册方法、设置开发环境和实际查询测试。同时,介绍了条件构造器的六个测试案例。此外,还详细阐述了代码生成器的概述,包括如何构建、引入依赖以及执行后的结果观察。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

性能分析插件

1、 作用

性能分析拦截器,用于输出每条SQL语句及其执行时间

2、 注册性能分析插件

// SQL执行效率插件
@Bean
@Profile({"dev","test"}) // 设置 dev test 环境开启,保证生产效率
public PerformanceInterceptor performanceInterceptor() {

    PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
    performanceInterceptor.setMaxTime(1);//ms 设置sql执行最大时间,如果超过了则不执行
    performanceInterceptor.setFormat(true);// 开启格式化支持

    return performanceInterceptor;
}

3、 设置开发环境

#设置开发环境
spring.profiles.active=dev

4、 测试查询全部用户

// 查询所有用户
@Test
void contextLoads() {

    //查询全部用户
    List<User> userList = userMapper.selectList(null);

    userList.forEach(System.out::println);
}

5、 观察输出结果

在这里插入图片描述
在这里插入图片描述

条件构造器

##1、 六个测试案例

package com.will;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.will.mapper.UserMapper;
import com.will.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
public class WrapperTests {

    @Autowired
    private UserMapper userMapper;

    @Test
    void contextLoads() {

        // 查询 name 不为空的用户,并且邮箱不为空,年龄大于12

        QueryWrapper<User> wrapper = new QueryWrapper();
        wrapper.isNotNull("name")
                .isNotNull("email")
                .gt("age", 12);

        userMapper.selectList(wrapper).forEach(System.out::println);
    }

    @Test
    void test2() {

        //查询 name 为 will 的用户

        QueryWrapper<User> wrapper = new QueryWrapper<>();

        wrapper.eq("name", "will");

        User user = userMapper.selectOne(wrapper); // 查询一个数据,出现多个结果不能使用

        System.out.println(user);

    }

    @Test
    void test3() {

        //查询年龄在 20 到 30 岁之间的用户

        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.between("age", 20 ,30);

        Integer count = userMapper.selectCount(wrapper);

        System.out.println(count);
    }

    @Test
    void test4() {

        // 模糊查询 name 中不包含字母 e ,邮箱以字母 t 开头
        QueryWrapper<User> wrapper = new QueryWrapper<>();

        //likeRight指 % 在右边,likeLeft指 % 在左边,like指左边均出现 %

        wrapper.notLike("name", "e")
                .likeRight("email", "t");

        userMapper.selectMaps(wrapper).forEach(System.out::println); // 查询方式一

        userMapper.selectList(wrapper).forEach(System.out::println); // 查询方式二
    }

    @Test
    void test5() {

        // 子查询,查询 id 小于 3 的数据

        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.inSql("id", "select id from user where id < 3");

        userMapper.selectObjs(wrapper).forEach(System.out::println);
    }

    @Test
    void test6() {

        // 通过 id 进行排序
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");

        userMapper.selectList(wrapper).forEach(System.out::println);

    }
}

代码生成器

1、 概述

AutoGenerator 是 MyBatis-Plus 的代码生成器,通过 AutoGenerator 可以快速生成 Entity、Mapper、Mapper XML、Service、Controller 等各个模块的代码,极大的提升了开发效率。

2、 构建代码生成器

package com.will;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.AutoGenerator;
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.io.File;
import java.util.Arrays;

public class AppCoder {

    public static void main(String[] args) {

        // 构建一个代码生成器对象

        AutoGenerator autoGenerator = new AutoGenerator();

        // 配置策略

        // 1、全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        String projDir = System.getProperty("user.dir");// 项目目录
        globalConfig.setOutputDir(projDir+File.separator+"01-mybatis-plus"+ File.separator+"/src/main/java"); // 自动生成代码输出路径
        globalConfig.setAuthor("will"); // 设置作者
        globalConfig.setOpen(false); // 生成之后是否打开资源管理器
        globalConfig.setFileOverride(false); // 是否覆盖原来生成的
        globalConfig.setServiceName("%sService"); // 取出Service接口的 “I” 前缀,
        globalConfig.setIdType(IdType.ID_WORKER); // 设置主键生成策略
        globalConfig.setDateType(DateType.ONLY_DATE); // 设置日期类型
        globalConfig.setSwagger2(false); // 是否配置swagger

        autoGenerator.setGlobalConfig(globalConfig); // 将全局配置放入生成器中

        // 2、设置数据源
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/mybatis_plus?useSSL=false&userUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8");
        dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
        dataSourceConfig.setUsername("root");
        dataSourceConfig.setPassword("root");
        dataSourceConfig.setDbType(DbType.MYSQL);

        autoGenerator.setDataSource(dataSourceConfig);

        // 3、包的配置
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setModuleName("auto"); // 包的模块
        packageConfig.setParent("com.will"); // 包的路径
        packageConfig.setEntity("pojo"); // 实体类的包名称
        packageConfig.setMapper("mapper"); // mapper包名称
        packageConfig.setService("service"); // 业务层包名称
        packageConfig.setController("controller"); // 控制层包名称

        autoGenerator.setPackageInfo(packageConfig);

        // 4、策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setInclude("user"); // 需要生成器构建的表名 :"teacher","student"
        strategy.setNaming(NamingStrategy.underline_to_camel); // 包命名规则:下划线转驼峰规则
        strategy.setColumnNaming(NamingStrategy.underline_to_camel); // 列命名规则:下划线转驼峰规则
        //strategy.setSuperEntityClass("你自己的父类实体,没有就不用设置!");
        strategy.setEntityLombokModel(true); // 自动 lombok
        strategy.setRestControllerStyle(true); // 支持 RestController 风格
        strategy.setLogicDeleteFieldName("deleted"); // 设置逻辑删除字段
        strategy.setVersionFieldName("version"); // 设置乐观锁
        strategy.setControllerMappingHyphenStyle(true); // 支撑下划线命名规则 : http://localhost:8080/hello_id_2

        // 自动填充
        strategy.setTableFillList(Arrays.asList(
                new TableFill("gmt_create", FieldFill.INSERT),
                new TableFill("gmt_modified", FieldFill.INSERT_UPDATE)
        ));

        autoGenerator.setStrategy(strategy);

        autoGenerator.execute();// 执行
    }

}

3、 引入依赖

<!--Velocity-->
<dependency>
    <groupId>org.apache.velocity</groupId>
    <artifactId>velocity-engine-core</artifactId>
    <version>2.3</version>
</dependency>

4、 执行并观察结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅帅的猪头

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值