在各种功能中会一直出现页面分页的问题。


对此,可以使用pojo对象,来一直管理页面分页的功能。
01.创建相关的pojo对象。

由于属性是来辅助sql语句的,这个pojo对象。


@Setter
@Getter
@ToString
@NoArgsConstructor
 public class PageObject<T> implements Serializable {
 private static final long serialVersionUID = -2313616516582778245L;
    
    //总页数
    private  Long pagesall=0l;
    //页面大小
    private Integer pageSize=5;
    //当前页数
    private Integer pageCurrent=0;
    //总row数
    private Long rowCount=0l;
    //当前页总记录
    private List<T> rcords;
    }
   public PageObject(Integer pageSize, Long pageCurrent, Long rowCount, List<T> rcords) {
        this.pageSize = pageSize;
        this.pageCurrent = pageCurrent;
        this.rowCount = rowCount;
        this.rcords = rcords;
        this.pagesall=rowCount/pageSize;
        if (rowCount%pageSize!=0){
            pagesall++;
        }//this.pageCount=(rowCount-1)/pageSize+1;
    }

03.service接口
由于pageObject是一个泛型类,这个泛型类可以用于用户接口,以后的菜单,等等不同的表都可以用这个pageObject。


public interface sysLogService {
     /**
           * @param name 基于条件查询时的参数名
           * @param pageCurrent 当前的页码值
           * @return 当前页记录+分页信息
           */
 public  PageObject<sysLog> findPageObjects(@Param("username") String username,
        @Param("pageCurrent") Integer pageCurrent);

}

04.serviceimpl类


@Service
public class sysLogServiceImpl implements sysLogService {
 @Autowired
    SysLogMapper sysLogMapper;
    @Override
    public PageObject<sysLog> findPageObjects(String username, Integer pageCurrent) {
        return null;
    }
}

其中需要有四个步骤:
//1.参数校验

if(pageCurrent==null||pageCurrent<1)
throw new IllegalArgumentException(“页码值不对”);

//2.查询记录总数并校验

Long rowCount = sysLogdao.getrownum(username);
if(rowCount==0){
throw new NoSuchElementException(“没找到对应记录”);
}
//3.查询当前记录
int pageSize=3;
long startIndex = (pageCurrent - 1) * pageSize;
List records = sysLogdao.findall(username,startIndex, pageSize);
return null;
//4.对查询结果进行封装
return new PageObject(pageSize,pageCurrent,rowCount,records);

完整


@Service
public class sysLogServiceImpl implements sysLogService {
    @Autowired
    sysLogDao sysLogdao;
    @Override
    public PageObject<sysLog> findPageObjects(String username, Integer pageCurrent) {

        if(pageCurrent==null||pageCurrent<1)
            throw new IllegalArgumentException("页码值不对");

        Long rowCount = sysLogdao.getrownum(username);
        if(rowCount==0){
            throw new NoSuchElementException("没找到对应记录");
        }

        int pageSize=3;
        long startIndex = (pageCurrent - 1) * pageSize;
        List<sysLog> records = sysLogdao.findall(username,startIndex, pageSize);


        return new PageObject<sysLog>(pageSize,pageCurrent,rowCount,records);

    }
}

更多推荐

基于springboot和ajax的简单项目 02.一直会出现的页面的上一页,下一页,分页与总页数 (下)