【springboot+mybatis-plus】使用IPage进行分页查询,返回全部数据
没有定义config层的MPConfig拦截器。
·
这里写自定义目录标题
DeBug
一、出现情况
SpringBoot.在使用nyBatis plus时候做一个分页查询,使用getPage奈何返回全部数据。
二、项目架构
2-1、Dao层
package com.cloudguest.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cloudguest.domain.TrainSchedule;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface TrainScheduleDao extends BaseMapper<TrainSchedule> {
}
2-2、service层
2-2-1、service接口
package com.cloudguest.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.cloudguest.domain.TrainSchedule;
import java.util.HashMap;
import java.util.List;
public interface TrainScheduleService extends IService<TrainSchedule> {
IPage<TrainSchedule> getPage(Integer pageNumber, Integer pageSize);
List<TrainSchedule> slectByMap(HashMap<String, Object> map);
}
2-2-2、serviceImpl 实现类
package com.cloudguest.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* 列车查询类
* 可以根据始发站和终点站查询到合适的列车
* 之后返回到前端
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class TrainSchedule {
private String train_number; // 车次
private String departure_station; // 出发站
private String end_station; // 到达站
private String departure_time; // 出发时间
private String arrival_time; // 到达时间
private String business_seat; // 特等座
private String soft_bed_first_class_bed;// 一等座/软卧
}
2-2-3、controller控制层
package com.cloudguest.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cloudguest.domain.TrainSchedule;
import com.cloudguest.service.TrainScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/schedule")
public class TrainScheduleController {
@Autowired
private TrainScheduleService trainScheduleService;
@GetMapping("{pageNumber}/{pageSize}")
public IPage<TrainSchedule> findPage(@PathVariable Integer pageNumber, @PathVariable Integer pageSize) {
return trainScheduleService.getPage(pageNumber, pageSize);
}
}
三、原因及解决方法
3-1、原因
- 没有添加config层的MPConfig配置信息,拦截器
3-2 解决方法
package com.cloudguest.config;
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 MPconfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
// 1、定义MP拦截器
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// 2、添加具体的拦截器
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return interceptor;
}
}
3-3、最终查询结果
3-4、最终目录结构
更多推荐
已为社区贡献1条内容
所有评论(0)