分页工具的使用

首先写一个分页类:

package com.dk.util;

import java.util.List;
import java.util.Map;


public class PageBean {

    private List list;        //要返回的某一页的记录列表

    private int allRow;         //总记录数
    private int totalPage;        //总页数
    private int currentPage;    //当前页
    private int pageSize;        //每页记录数

    private boolean isFirstPage;    //是否为第一页
    private boolean isLastPage;        //是否为最后一页
    private boolean hasPreviousPage;    //是否有前一页
    private boolean hasNextPage;        //是否有下一页


    public List getList() {
        return list;
    }
    public void setList(List list) {
        this.list = list;
    }
    public int getAllRow() {
        return allRow;
    }
    public void setAllRow(int allRow) {
        this.allRow = allRow;
    }
    public int getTotalPage() {
        return totalPage;
    }
    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }
    public int getCurrentPage() {
        return currentPage;
    }
    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }
    public int getPageSize() {
        return pageSize;
    }
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    /** *//**
     * 初始化分页信息
     */
    public void init(){
        this.isFirstPage = isFirstPage();
        this.isLastPage = isLastPage();
        this.hasPreviousPage = isHasPreviousPage();
        this.hasNextPage = isHasNextPage();
    }

    /** *//**
     * 以下判断页的信息,只需getter方法(is方法)即可
     * @return
     */

    public boolean isFirstPage() {
        return currentPage == 1;    // 如是当前页是第1页
    }
    public boolean isLastPage() {
        return currentPage == totalPage;    //如果当前页是最后一页
    }
    public boolean isHasPreviousPage() {
        return currentPage != 1;        //只要当前页不是第1页
    }
    public boolean isHasNextPage() {
        return currentPage != totalPage;    //只要当前页不是最后1页
    }


    /** *//**
     * 计算总页数,静态方法,供外部直接通过类名调用
     * @param pageSize 每页记录数
     * @param allRow 总记录数
     * @return 总页数
     */
    public static int countTotalPage(final int pageSize,final int allRow){
        int totalPage = (allRow % pageSize == 0 && allRow != 0) ? allRow/pageSize : allRow/pageSize+1;
        return totalPage;
    }

    /** *//**
     * 计算当前页开始记录
     * @param pageSize 每页记录数
     * @param currentPage 当前第几页
     * @return 当前页开始记录号
     */
    public static int countOffset(final int pageSize,final int currentPage){
        final int offset;
        if(currentPage == 0){
            offset = 0;
        }else{
            offset = pageSize*(currentPage-1);
        }
        return offset;
    }

    /** *//**
     * 计算当前页,若为0或者请求的URL中没有"?page=",则用1代替
     * @param page 传入的参数(可能为空,即0,则返回1)
     * @return 当前页
     */
    public static int countCurrentPage(int page){
        final int curPage = (page==0?1:page);
        return curPage;
    }

    public static String queryStr(Map<String, String> queryMap) {
        if(null!=queryMap){
            String queryUrl="";
            for(Map.Entry<String, String> qm : queryMap.entrySet()){
                if(qm.getValue()!=null && !qm.getValue().equals("") && qm.getValue().length()>0){
                    queryUrl += "&query." + qm.getKey()+"=" + qm.getValue();
                }
            }
            return queryUrl;
        }
        return "";
    }


}
package com.dk.core.dao;

import com.dk.core.vo.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

@Mapper
public interface UserMapper {
    @Select("${sql}")
    List<com.dk.core.vo.User> findUser(@Param("sql")String sql);

    @Select("${sqlCount}")
    int findUserCount(@Param("sqlCount")String sqlCount);

  
}

service 

package com.dk.core.service;

import com.dk.core.dao.UserMapper;
import com.dk.core.vo.User;
import com.dk.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Transactional
@Service
public class UserService implements IUserService{

    @Autowired
    private UserMapper userMapper;


    @Override
    public PageBean getList(int pageSize, int page, Map<String, String> query) {
        String sql = "from User obj ";
        if(query != null && query.size() >0 ){
            sql += " where obj.deleteStatus = false " ;
        }

        //System.out.println("hql = "+hql);
        int allRow = this.getAllRowCount(sql);
        int totalPage = PageBean.countTotalPage(pageSize, allRow);
        final int offset = PageBean.countOffset(pageSize, page);
        final int length = pageSize;
        final int currentPage = PageBean.countCurrentPage(page);
        List list = this.query(sql, offset, length);
        PageBean pageBean = new PageBean();
        pageBean.setPageSize(pageSize);
        pageBean.setCurrentPage(currentPage);
        pageBean.setAllRow(allRow);
        pageBean.setTotalPage(totalPage);
        pageBean.setList(list);
        pageBean.init();
        return pageBean;
    }

    @Override
    public int getAllRowCount(String sql) {
        return userMapper.findUserCount(sql);
    }

    @Override
    public List<User> query(String sql, int offset, int length) {
        if (offset!=-1 || length!= -1){
            sql +=" limit "+offset+","+length;
        }
        return userMapper.findUser(sql);
    }
}
package com.dk.core.service;

import com.dk.core.vo.User;
import com.dk.util.PageBean;

import java.util.List;
import java.util.Map;

public interface IUserService {


    PageBean getList(int pageSize, int page, Map<String, String> query);


    int getAllRowCount(String sql);

    List<User> query(String sql, int offset,int length);
}

 

package com.dk.api.control;
import com.dk.core.service.IUserService;
import com.dk.core.vo.User;
import com.dk.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



@RestController
@RequestMapping("/json")
public class UserJsonControllor {

    @Autowired
    IUserService userService;

 


    @RequestMapping(value = "/userList")
    public Result userList(int page){
        int pageSize = 10;
        String sql = "select count(obj.id) from shopping_user obj";
        int allRow = userService.getAllRowCount(sql);
        //总共多少页
        int totalPage = PageBean.countTotalPage(pageSize, allRow);
        if (page>totalPage){
            page=totalPage;
        }

        int offset = PageBean.countOffset(pageSize, page);
        String userSql = "select * from shopping_user obj";
        List<User> userList = userService.query(userSql,offset,pageSize);
        Map map =new HashMap();
        map.put("list",userList);
        return Result.success(map);
    }

}

 

转载于:https://www.cnblogs.com/wyf-love-dch/p/11328829.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值