MybatisPlus性能分析插件
作用
用于输出每条sql语句执行的时间,便于优化sql语句
基于这几篇博客修改
MybatisPlusConfig 类配置sql分析插件
package com.xiang.config;
import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
/**
* Created by IntelliJ IDEA.
* User: xiang
* Date: 2021/10/23 0:07
*/
@Configuration
@MapperScan("com.xiang.mapper")
//添加扫描注解,写入mapper的路径,否则会找不到接口并报错
public class MybatisPlusConfig {
//乐观锁插件
@Bean
public OptimisticLockerInterceptor OptimisticLockerInterceptor() {
return new OptimisticLockerInterceptor();
}
//分页插件
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
//逻辑删除插件
// @Bean
// public ISqlInjector sqlInjector(){
// return new LogicSqlInjector();
// }
/**
* Sql性能分析插件
* 开发环境使用,上线项目不推荐使用
*
* @return
*/
@Bean
@Profile({"dev", "test"})//设置使用环境
public PerformanceInterceptor performanceInterceptor() {
PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
performanceInterceptor.setMaxTime(100);//ms值,超过此处的毫秒值则不执行
performanceInterceptor.setFormat(true);
return performanceInterceptor;
}
}
开发环境介绍:
- dev:开发环境
- test:测试环境
- prod:生产环境(真正上线交给用户使用的环境)
application.yml 设置环境
spring:
datasource:
url: jdbc:mysql://localhost:3307/webapp1
username: webapp1
password: webapp1
driver-class-name: com.mysql.cj.jdbc.Driver
#设置当前环境:dev;test;prod
profiles:
active: dev
#mybatis日志:添加后可以查看执行的sql语句
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
#设置逻辑删除的值:1-已删除;0-没有删除
#可自行设置值,但是一般不设置,1和0为默认值
# global-config:
# db-config:
# logic-delete-value: 1
# logic-not-delete-value: 0
测试类
package com.xiang;
import com.xiang.mapper.UserMapper;
import com.xiang.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;
/**
* Created by IntelliJ IDEA.
* User: xiang
* Date: 2021/10/23 21:28
*/
@SpringBootTest
public class PerformanceAnalysis {
@Autowired
UserMapper userMapper;
/**
* MybatisPlus性能分析
*/
@Test
/**
* 查所有
*/
void findAll() {
List<User> list = userMapper.selectList(null);
for (User user : list) {
System.out.println(user);
}
}
}
运行结果
JDBC Connection [HikariProxyConnection@2016442966 wrapping com.mysql.cj.jdbc.ConnectionImpl@40d52be7] will not be managed by Spring
==> Preparing: SELECT id,username,sex,age,birthday,create_time,update_time,version,deleted FROM user WHERE deleted=0
==> Parameters:
<== Columns: id, username, sex, age, birthday, create_time, update_time, version, deleted
<== Row: 1, xiang, 男, 18, 2021-10-03, null, null, null, 0
<== Row: 559, 小向, 男, 18, 2021-10-04, null, null, null, 0
<== Row: 602, admin, 女, 18, 2021-10-20, null, null, null, 0
<== Row: 605, 小二, 女, 18, null, null, null, null, 0
<== Row: 607, 小四, 女, 21, null, null, null, null, 0
<== Row: 609, 小五, 女, 0, null, null, null, null, 0
<== Row: 1451097869404868609, 向某, 男, 0, null, null, null, null, 0
<== Row: 1451098975287668738, 周某, 男, 0, null, null, null, null, 0
<== Total: 8
Time:53 ms - ID:com.xiang.mapper.UserMapper.selectList
Execute SQL:
SELECT
id,
username,
sex,
age,
birthday,
create_time,
update_time,
version,
deleted
FROM
user
WHERE
deleted=0
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@2c748a15]
User(id=1, username=xiang, sex=男, age=18, birthday=Sun Oct 03 00:00:00 CST 2021, createTime=null, updateTime=null, version=0, deleted=0)
User(id=559, username=小向, sex=男, age=18, birthday=Mon Oct 04 00:00:00 CST 2021, createTime=null, updateTime=null, version=0, deleted=0)
User(id=602, username=admin, sex=女, age=18, birthday=Wed Oct 20 00:00:00 CST 2021, createTime=null, updateTime=null, version=0, deleted=0)
User(id=605, username=小二, sex=女, age=18, birthday=null, createTime=null, updateTime=null, version=0, deleted=0)
User(id=607, username=小四, sex=女, age=21, birthday=null, createTime=null, updateTime=null, version=0, deleted=0)
User(id=609, username=小五, sex=女, age=0, birthday=null, createTime=null, updateTime=null, version=0, deleted=0)
User(id=1451097869404868609, username=向某, sex=男, age=0, birthday=null, createTime=null, updateTime=null, version=0, deleted=0)
User(id=1451098975287668738, username=周某, sex=男, age=0, birthday=null, createTime=null, updateTime=null, version=0, deleted=0)