MyBatisPlus分页查询踩坑小记

记录下

Q1:使用MyBatis中selectPage()查询语句,没有limit

源代码中的方法:

IPage<T> selectPage(IPage<T> page, @Param("ew") Wrapper<T> queryWrapper);

解决方法:

步骤1:检查pom依赖,没有则添加

    <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>${mybatis.plus.boot.starter.version}</version>
    </dependency>

步骤2:配置分页类

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

/**
 * create by FenQing1213
 **/
@EnableTransactionManagement(proxyTargetClass = true)
@Configuration
public class MybatisPlusConfig {
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}

步骤3:controller层和dao层代码这里省略,service代码实现如下

public Result query(QuerySomeThing query) {
        QueryWrapper<QuerySomeThing> queryWrapper = new QueryWrapper();
        queryWrapper.like("lie_ming", query.getKeywords())
                .orderByAsc("id");
        Page<QuerySomeThing> page = new Page<>(1,10); // 第一个参数是pageNum,第二个参数是pageSize
        IPage<QuerySomeThing> iPage = yourMapperName().selectPage(page, queryWrapper); // 调用mybatis中自带的查询分页方法
        List<QuerySomeThing> records = iPage.getRecords();
        return records;
    }

Q2:MybatisPlus自定义分页查询,没有limit

起初按照百度的方法,使用自定义的分页查询,但是看执行SQL,语句中还是没有limit

解决方法

按照Q1中提到的步骤1、2,新增一个配置类即可。

Q3:MyBatisPlus中selectMapsPage()返回数据没有转化

源代码的方法:

IPage<Map<String, Object>> selectMapsPage(IPage<T> page, @Param("ew") Wrapper<T> queryWrapper);

解决方法

用selectPage不好吗?

你可能感兴趣的:(学习小记,走过的坑)