springboot中Mybaits-plus 分页插件使用及sql执行效率插件

首页是jar包的引入


	com.baomidou
	mybatis-plus-boot-starter
	3.0.7.1	

接下来是springboot创建bean,不然分页插件不会生效

package com.ipzoe.ndk.config;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author swy
 */
@Configuration
@MapperScan("com.ipzoe.ndk.mapper")
@Slf4j
public class MybatisPlusConfig {
    /**
     * mybatis-plus SQL执行效率插件【生产环境可以关闭】
     */
    @Bean
    public PerformanceInterceptor performanceInterceptor() {
        return new PerformanceInterceptor();
    }

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

以上配置结束就i可以使用插件了,插件的使用方法

package com.ipzoe.ndk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ipzoe.ndk.domain.entity.AdminUser;
import com.ipzoe.ndk.mapper.AdminUserMapper;
import com.ipzoe.ndk.service.AdminUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;


@Service
public class AdminUserServiceImpl extends ServiceImpl implements AdminUserService {

    @Override
    public Object getList(Integer pageSize,Integer currentPage) {
        QueryWrapper query = Wrappers.query();
        query.orderByDesc("id");
        if(null != pageSize && currentPage != null){
            return page(new Page<>(currentPage,pageSize),query);
        }
        return AdminUser.INSTANCE.selectList(Wrappers.query().orderByDesc("id"));
    }
}

sql执行过程中,效率插件会生效,在打印台输出查询的sql语句

你可能感兴趣的:(springboot中Mybaits-plus 分页插件使用及sql执行效率插件)