MyBatis-Plus——插件讲解

哈喽!大家好,我是【一心同学】,一位上进心十足的【Java领域博主】!

✨【一心同学】的写作风格:喜欢用【通俗易懂】的文笔去讲解每一个知识点,而不喜欢用【高大上】的官方陈述。

✨【一心同学】博客的领域是【面向后端技术】的学习,未来会持续更新更多的【后端技术】以及【学习心得】。

✨如果有对【后端技术】感兴趣的【小可爱】,欢迎关注一心同学

❤️❤️❤️感谢各位大可爱小可爱!❤️❤️❤️ 


目录

前言

一、准备工作

二、分页插件

步骤一:编写配置类

步骤二:测试

三、性能分析插件

步骤一:编写配置文件

步骤二:环境设置

步骤三:测试

小结


前言

本篇博客将会讲解在我们开发中较常见的两个插件【分页插件】和【执行性能分析插件】,这两款插件可以减少我们在开发中的【大量时间】,接下来就跟着【一心同学】的步伐往下看看。

一、准备工作

1、建立数据库信息表


CREATE DATABASE mybatis_plus_db;

USE `mybatis_plus_db`;

DROP TABLE IF EXISTS `student`;

CREATE TABLE `student` (
  `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键',
  `name` varchar(20) DEFAULT NULL COMMENT '名字',
  `age` int DEFAULT NULL COMMENT '年龄',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;


insert  into `student`(`id`,`name`,`age`) values 
(1,'一心同学',18),
(2,'张三',20),
(3,'李四',22),
(4,'王五',21),
(5,'Alice',21),
(6,'Tom',22);

2、创建一个SpringBoot项目

初始化以下依赖:

MyBatis-Plus——插件讲解_第1张图片

3、导入依赖

        
        
            mysql
            mysql-connector-java
        

        
        
            org.projectlombok
            lombok
        

        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.0.5
        

4、编写配置文件

application.properties:

# mysql 5 驱动不同 com.mysql.jdbc.Driver
# mysql 8 驱动不同com.mysql.cj.jdbc.Driver、需要增加时区的配置 serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus_db?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

# 配置日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

5、编写实体类

package com.yixin.pojo;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.Version;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {

    @TableId(type = IdType.AUTO)
    private Long id;
    private String name;
    private Integer age;

}

6、编写实体类对应的Mapper接口

package com.yixin.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yixin.pojo.Student;
import org.springframework.stereotype.Repository;


// 在对应的Mapper上面继承基本的类 BaseMapper
@Repository// 代表持久层
public interface StudentMapper extends BaseMapper {
    // 所有的CRUD操作都已经编写完成了
    // 我们不需要像以前的配置一大堆文件了!
}

7、主启动器类添加注解

增加注解:@MapperScan("com.yixin.mapper")

package com.yixin;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@MapperScan("com.yixin.mapper")
@SpringBootApplication
public class MybatisPlusApplication {

    public static void main(String[] args) {
        SpringApplication.run(MybatisPlusApplication.class, args);
    }

}

8、测试

package com.yixin;

import com.yixin.mapper.StudentMapper;
import com.yixin.pojo.Student;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
class MybatisPlusApplicationTests {
    
    @Autowired
    private StudentMapper studentMapper;

    @Test
    void contextLoads() {
        List studentList=studentMapper.selectList(null);
        System.out.println(studentList);
    }

}

结果

出现以下信息,说明我们的搭建工作完成啦!

MyBatis-Plus——插件讲解_第2张图片

二、分页插件

回想我们之前要写一个分页功能,我们得先去编写一个分页类Page,然后再通过外部引用才能完成分页功能的实现,而现在MyBatis-Plus把这个功能也实现了,我们只需要配置好分页插件,就可以进行分页操作了!

步骤一:编写配置类

package com.yixin.config;


import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

// 扫描我们的 mapper 文件夹
@MapperScan("com.yixin.mapper")
@EnableTransactionManagement
@Configuration // 配置类
public class MyBatisPlusConfig {

    // 分页插件
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return  new PaginationInterceptor();
    }

}

步骤二:测试

    @Test
    void test1(){

        // 参数一:当前页
        // 参数二:页面大小
        Page page=new Page<>(1,5);

        studentMapper.selectPage(page,null);

        page.getRecords().forEach(System.out::println);

        System.out.println("当前页:" + page.getCurrent());
        System.out.println("总页数:" + page.getPages());
        System.out.println("记录数:" + page.getTotal());
        System.out.println("是否有上一页:" + page.hasPrevious());
        System.out.println("是否有下一页:" + page.hasNext());
        
    }

结果:

测试成功!

MyBatis-Plus——插件讲解_第3张图片

三、性能分析插件

作用:性能分析拦截器,用于输出每条 SQL 语句及其执行时间,而且我们可以设置相应的时间,如果超过了这个时间,就停止运行!

步骤一:编写配置文件

    /**
     * SQL执行效率插件
     */
    @Bean
    @Profile({"dev","test"})// 设置 dev test 环境开启,保证我们的效率
    public PerformanceInterceptor performanceInterceptor() {
        PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
        performanceInterceptor.setMaxTime(100); //ms 设置sql执行的最大时间,如果超过了则不执行
        performanceInterceptor.setFormat(true);
        return performanceInterceptor;
    }

步骤二:环境设置

tip:将我们的环境设置为dev环境,为了模拟更加真实的环境。

application.properties:

spring.profiles.active=dev

步骤三:测试

    @Test
    void contextLoads() {
        List studentList=studentMapper.selectList(null);
        System.out.println(studentList);
    }

结果:

MyBatis-Plus——插件讲解_第4张图片


小结

以上就是【一心同学】对【分页插件】和【性能分析插件】的讲解,可以说这两个插件的安装过程也是【十分简单】的,只需要我们编写好配置类就可以正常使用了,在开发中性能分析插件会显得重要,因为我们需要根据其执行的性能进行一系列的【优化】。

如果这篇【文章】有帮助到你,希望可以给【一心同学】点个,创作不易,相比官方的陈述,我更喜欢用【通俗易懂】的文笔去讲解每一个知识点,如果有对【后端技术】感兴趣的小可爱,也欢迎关注❤️❤️❤️ 【一心同学】❤️❤️❤️,我将会给你带来巨大的【收获与惊喜】

你可能感兴趣的:(MyBatis-Plus,mybatis,plus,分页插件,执行性能分析插件,后端)