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

如题:

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

1.默认无条件分页查询

public abstract IService 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 page = new Page(current, size);
	return ResponseUtils.success(getIService().page(page));
}

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

springboot集成mybatis-plus分页查询+条件分页查询_第1张图片

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 {
	
	Page 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 implements AreaService {

	@Autowired
	private AreaMapper areaMapper;
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public Page pageByConditions(AreaReqParams t) {
		Page page = new Page(t.getCurrent(), t.getSize());
		QueryWrapper 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 pageByConditions(@RequestBody AreaReqParams t) {
	return ResponseUtils.success(areaService.pageByConditions(t));
}

测试请求报文为:

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

测试截图为:

springboot集成mybatis-plus分页查询+条件分页查询_第2张图片

你可能感兴趣的:(springboot,spring,boot,java,分页查询,分页条件查询,条件查询)