springboot集成mybatis-plus分页查询+条件分页查询

本文介绍了如何利用MyBatis-Plus进行默认无条件和自定义条件的分页查询。通过创建抽象服务、定义查询参数类、实现Service和ServiceImpl,以及编写Controller,实现了便捷的分页查询功能。示例代码展示了具体的实现步骤和测试结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如题:

mybatis-plus整合了很多我们无需重复操作的功能,直接使用即可,下面举出两个分页的例子。

1.默认无条件分页查询

public abstract IService<T> getIService();

/**
 * 分页查询
 * @param current
 * @param size
 * @return
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
@PostMapping("/page")
@ApiOperation(value="分页查询", notes="比如:当前第1页查询10条数据,则current=1,size=10")
@ApiImplicitParams({
    @ApiImplicitParam(name = "current", value = "当前第几页", required = true, dataType = "Integer"),
    @ApiImplicitParam(name = "size", value = "查询几条", required = true, dataType = "Integer")
})
public ResponseResult<?> page(@RequestParam Integer current, @RequestParam Integer size) {
	Page<T> page = new Page(current, size);
	return ResponseUtils.success(getIService().page(page));
}

只需要传入current和size即可查询出实体表的对应分页数据,测试截图如下:

2.自定义带条件分页查询

首先,自定义一个支持类,将所有需要的字段准备进去。

package com.bbnet.demo.vo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@ApiModel(value="Area查询参数对象")
@Data
public class AreaReqParams{

	@ApiModelProperty(value = "名称", dataType = "String")
    private String name;
    
    @ApiModelProperty(value = "类型 1=省会/直辖市 2=城市 3=区县", dataType = "Integer")
    private Integer type;
    
    @ApiModelProperty(value = "当前第几页", dataType = "Integer")
	Integer current;
	
	@ApiModelProperty(value = "查询几条", dataType = "Integer")
	Integer size;

}

然后,service和impl对该类中的字段进行赋值使用。

package com.bbnet.demo.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.bbnet.demo.vo.Area;
import com.bbnet.demo.vo.AreaReqParams;

public interface AreaService extends IService<Area> {
	
	Page<Area> pageByConditions(AreaReqParams t);
	
	
}
package com.bbnet.demo.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bbnet.demo.mapper.AreaMapper;
import com.bbnet.demo.service.AreaService;
import com.bbnet.demo.vo.Area;
import com.bbnet.demo.vo.AreaReqParams;

@Service
public class AreaServiceImpl extends ServiceImpl<AreaMapper, Area> implements AreaService {

	@Autowired
	private AreaMapper areaMapper;
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public Page<Area> pageByConditions(AreaReqParams t) {
		Page<Area> page = new Page(t.getCurrent(), t.getSize());
		QueryWrapper<Area> wrapper = new QueryWrapper<>();
		wrapper.eq("type", t.getType());
		wrapper.like("name", t.getName());
		return this.page(page, wrapper);
	}

}

最后,controller使用该支持类与请求方交互。

/**
 * 分页查询
 * @param current
 * @param size
 * @return
 */
@SuppressWarnings("unchecked")
@PostMapping("/pageByConditions")
@ApiOperation(value="分页条件查询", notes="比如:当前第1页查询10条数据,则current=1,size=10")
public ResponseResult<Area> pageByConditions(@RequestBody AreaReqParams t) {
	return ResponseUtils.success(areaService.pageByConditions(t));
}

测试请求报文为:

{
  "current": 0,
  "name": "省",
  "size": 10,
  "type": 1
}

测试截图为:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cgv3

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值