栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA 入门级分页查询

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JAVA 入门级分页查询

分页PageVO

package 包名;

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

import java.util.ArrayList;
import java.util.List;


@Data
public class PageVO {

    @ApiModelProperty(value = "列表")
    private List list = new ArrayList<>();

    @ApiModelProperty(value = "总条数")
    private Long total = 0L;

    public PageVO(List list, long total) {
        this.list.addAll(list);
        this.total += total;
    }
}

BasePageDTO
这个是在maven里的公用jar包里的,maven里的jar包也是可以自己写的,
大多数 查询条件DTO需要extends BasePageDTO

package com.gas.base.dto;

import io.swagger.annotations.ApiModelProperty;
import javax.validation.constraints.NotNull;

public class BasePageDTO {
    @ApiModelProperty(
        value = "当前页",
        example = "1",
        required = true
    )
    @NotNull(
        message = "当前页条数,值不能为空"
    )
    private Long current = 1L;
    @ApiModelProperty(
        value = "当前页条数",
        example = "10",
        required = true
    )
    @NotNull(
        message = "当前页条数,值不能为空"
    )
    private Long size = 10L;

    public BasePageDTO() {
    }

    public Long getCurrent() {
        return this.current;
    }

    public Long getSize() {
        return this.size;
    }

    public void setCurrent(final Long current) {
        this.current = current;
    }

    public void setSize(final Long size) {
        this.size = size;
    }

    public boolean equals(final Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof BasePageDTO)) {
            return false;
        } else {
            BasePageDTO other = (BasePageDTO)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                Object this$current = this.getCurrent();
                Object other$current = other.getCurrent();
                if (this$current == null) {
                    if (other$current != null) {
                        return false;
                    }
                } else if (!this$current.equals(other$current)) {
                    return false;
                }

                Object this$size = this.getSize();
                Object other$size = other.getSize();
                if (this$size == null) {
                    if (other$size != null) {
                        return false;
                    }
                } else if (!this$size.equals(other$size)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(final Object other) {
        return other instanceof BasePageDTO;
    }

    public int hashCode() {
        int PRIME = true;
        int result = 1;
        Object $current = this.getCurrent();
        int result = result * 59 + ($current == null ? 43 : $current.hashCode());
        Object $size = this.getSize();
        result = result * 59 + ($size == null ? 43 : $size.hashCode());
        return result;
    }

    public String toString() {
        return "BasePageDTO(current=" + this.getCurrent() + ", size=" + this.getSize() + ")";
    }
}

苞米豆的Page
这个baomidou直接引入就行

package com.baomidou.mybatisplus.extension.plugins.pagination;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Predicate;

public class Page implements IPage {
    private static final long serialVersionUID = 8545996863226528798L;
    protected List records;
    protected long total;
    protected long size;
    protected long current;
    protected List orders;
    protected boolean optimizeCountSql;
    protected boolean searchCount;
    protected boolean optimizeJoinOfCountSql;
    protected String countId;
    protected Long maxLimit;
    //第28行代码
    //这里省略了很多代码
    //第240行代码
}

分页查询Controller

@Api(tags = {"计量组"})
@Slf4j
@Validated
@RestController
@RequestMapping("metering")
public class MeteringController{
	@Resource
    private MeteringService service;
    
	@ApiOperation(value = "分页查询", notes="metering-page")
	@GetMapping("/page")
	//我这里是计量组的分页
	@SaCheckPermission("metering-page")
	public R> page(@Validated 查询条件DTO dto) {
	    PageVO<返回值VO> page =  service.findPage(dto);
	    return R.ok(page);
	}
}

ServiceImpl

@Service
public class MeteringServiceImpl extends ServiceImpl implements MeteringService {

    //计量组
    @Resource
    private MeteringMapper mapper;

    //计量组设备详情
    @Resource
    private MeteringDetailsMapper detailsMapper;

    
    @Override
    public PageVO<返回值VO> findPage(查询条件DTO dto) {

		//如果查询条件的DTO  没有强制 让前端传页数 和查询条数,可以用下边两行代码
    	Long current = Optional.ofNullable(sysUserDeptDTO.getCurrent()).orElse(1L);
        Long size = Optional.ofNullable(sysUserDeptDTO.getSize()).orElse(10L);

		//执行查询
        Page<返回值VO> page = baseMapper.findPage(new Page(dto.getCurrent(),dto.getSize()),dto);
        
        //page.getRecords就是我们要查的list
        List<返回值VO> list = page.getRecords();
        //page.getTotal()其实就是总条数
        long total = page.getTotal();
        
        //用枚举赋值
        list.forEach(e -> e.setEqTypeName(MeteringEnum.name(e.getEqType())));

        return new PageVO(list, total );
    }

Mapper
这里写这个mapper,主要是让自己别忘了把分页的参数传过来

//分页查询
Page<返回值VO> findPage(Page page, @Param("model") 查询条件DTO dto);
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1039884.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号