MyBatisPlus--性能分析插件

本文详细介绍了如何在SpringMVC框架中使用MyBatis Plus的性能分析插件,通过配置PerformanceInterceptor来监控SQL执行时间,设置最大执行时间和SQL格式化,以实现数据库操作的性能优化。并通过一个具体的例子展示了如何使用QueryWrapper进行条件查询,以及控制台输出的SQL执行信息和查询结果。

SpringMVC配置

mybatis-config.xml

<configuration>
    <!-- 性能分析插件 -->
    <plugins>
        <plugin interceptor="com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor">
            <!-- 最大的执行时间,单位为毫秒 -->
            <property name="maxTime" value="100"/>
            <!--SQL是否格式化 默认false-->
            <property name="format" value="true"/>
        </plugin>
    </plugins>
</configuration>

SpringBoot配置

/**
     * SQL执行效率插件
     */
    @Bean
    public PerformanceInterceptor performanceInterceptor() {
        PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
        performanceInterceptor.setMaxTime(100);
        performanceInterceptor.setFormat(true);
        return performanceInterceptor;
    }

测试

	@Test
   public void testSelectList(){
      User user = new User();
      QueryWrapper<User> wrapper = new QueryWrapper<>();
      wrapper.gt("age", "1");//查询条件:age大于1
      List<User> users = user.selectList(wrapper);
      for (User u : users)
         System.out.println(u);
   }

控制台打印:

 Time:8 ms - ID:psers.zhang.demo.mapper.UserMapper.selectList
Execute SQL:
    SELECT
        id,
        user_name,
        password,
        name,
        age,
        email 
    FROM
        tb_user 
    WHERE
        age > '1'

User(id=2, userName=三狗, password=123456, name=李四, age=88, email=test2@qq.com, address=null)
User(id=4, userName=zhaoliu, password=123456, name=赵六, age=48, email=test4@qq.com, address=null)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值