首页 > 其他分享 >springboot+mybatisplus分页查询

springboot+mybatisplus分页查询

时间:2024-09-03 10:27:45浏览次数:13  
标签:mybatisplus springboot wrapper MybatisPlusInterceptor new import com 分页

需求:类似sqlserver sql语句查询top 100条数据,如何在springboot+mybatisplus中实现(使用的是sqlserver2012数据库)

1.pom文件中引入mybatisplus依赖

        
        <!-- mybatis-plus -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.5</version>
        </dependency>

        <!-- SQL Server -->
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <version>8.4.1.jre8</version>
        </dependency>

2.由于非页面点击下一页的使用场景,只是接口中查询前100条数据,故使用selectList方法即可

 3.创建分页查询拦截器(必须要有此配置类,否则只调用selectList方法无效)

package com.example.webserver.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MybatisPlusConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.SQL_SERVER));
        return interceptor;
    }
}

4.编写分页查询方法(sqlserver必须要先排序才能分页,否则报““OFFSET”附近有语法错误”)

            //查询数据库待报工数据
            QueryWrapper<ZkProductStation> wrapper = new QueryWrapper<ZkProductStation>();
            wrapper.eq("Reported", 0);
            wrapper.orderByAsc("CreationTime");
            IPage<ZkProductStation> page = new Page<>(1,100);
            List<ZkProductStation> productStationsList = zkProductStationMapper.selectList(page,wrapper);
            System.out.println("前2条数据:"+productStationsList.toString());

 

标签:mybatisplus,springboot,wrapper,MybatisPlusInterceptor,new,import,com,分页
From: https://www.cnblogs.com/luna-hehe/p/18394060

相关文章