Mybatis-Plus性能分析插件
1、性能分析插件
我们在平时的开发中,会遇到一些慢sql。
测试! druid,…MP也提供性能分析插件,如果超过这个时间就停止运行!
1、导入插件
//SQL执行效率插件
@Bean
@Profile({"dev","test"})//开启环境保护,保证我们的效率
public PerformanceInterceptor performanceInterceptor(){
PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
performanceInterceptor.setMaxTime(1);//设置执行sql的最大时间,如果超过则不执行
performanceInterceptor.setFormat(true);//开启sql语句格式化
return performanceInterceptor;
}
application.yml配置文件
spring:
datasource:
username: root
password: root
url: jdbc:mysql://localhost:3306/mybatis
driver-class-name: com.mysql.jdbc.Driver
#设置开发环境
profiles:
active: dev
#配置日志
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
#配置逻辑删除
global-config:
db-config:
logic-delete-value: 1
logic-not-delete-value: 0
2、测试使用
//测试批量查询
@Test
void SelectByBatchId(){
List<user> users=mapper.selectBatchIds(Arrays.asList(1L,2L,3L));
users.forEach(System.out::println);
}
测试结果
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@17e9bc9e] was not registered for synchronization because synchronization is not active
JDBC Connection [HikariProxyConnection@1690796457 wrapping com.mysql.cj.jdbc.ConnectionImpl@71ed560f] will not be managed by Spring
==> Preparing: SELECT id,name,age,email,version,deleted,create_time,update_time FROM user WHERE id IN ( ? , ? , ? ) AND deleted=0
==> Parameters: 1(Long), 2(Long), 3(Long)
<== Columns: id, name, age, email, version, deleted, create_time, update_time
<== Row: 1, xys444, 11, 95788@afe, 2, 0, 2021-10-05 01:06:16, 2021-10-05 19:49:41
<== Row: 2, v, 22, dsrhdet@ef, 1, 0, 2021-10-01 01:06:22, 2021-10-01 16:45:51
<== Row: 3, a, 12, xdfb@qw, 1, 0, 2021-10-04 00:06:37, 2021-10-04 13:46:21
<== Total: 3
Time:27 ms - ID:com.xys.Mapper.mapper.selectBatchIds
Execute SQL:
SELECT
id,
name,
age,
email,
version,
deleted,
create_time,
update_time
FROM
user
WHERE
id IN (
1 , 2 , 3
)
AND deleted=0
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@17e9bc9e]
user(id=1, name=xys444, age=11, email=95788@afe, version=2, deleted=0, createTime=Tue Oct 05 01:06:16 CST 2021, updateTime=Tue Oct 05 19:49:41 CST 2021)
user(id=2, name=v, age=22, email=dsrhdet@ef, version=1, deleted=0, createTime=Fri Oct 01 01:06:22 CST 2021, updateTime=Fri Oct 01 16:45:51 CST 2021)
user(id=3, name=a, age=12, email=xdfb@qw, version=1, deleted=0, createTime=Mon Oct 04 00:06:37 CST 2021, updateTime=Mon Oct 04 13:46:21 CST 2021)