后台分页查询

一、首先编写一个分页工具类:

package com.zte.domain;

import java.util.List;

/**
 * Created by 6092002943 on 2019/1/16.
 */
public class PageBean {
        // 当前页
        private Integer currentPage = 1;
        // 每页显示的总条数
        private Integer pageSize = 10;
        // 总条数
        private Integer totalNum;
        // 是否有下一页
        private Integer isMore;
        // 总页数
        private Integer totalPage;
        // 开始索引
        private Integer startIndex;
        // 分页结果
        private List items;

        public PageBean() {
            super();
        }

        public PageBean(Integer currentPage, Integer pageSize, Integer totalNum) {
            super();
            this.currentPage = currentPage;
            this.pageSize = pageSize;
            this.totalNum = totalNum;
            this.totalPage = (this.totalNum + this.pageSize - 1) / this.pageSize;
            this.startIndex = (this.currentPage - 1) * this.pageSize;
            this.isMore = this.currentPage >= this.totalPage ? 0 : 1;
        }

        public Integer getCurrentPage() {
            return currentPage;
        }

        public void setCurrentPage(Integer currentPage) {
            this.currentPage = currentPage;
        }

        public Integer getPageSize() {
            return pageSize;
        }

        public void setPageSize(Integer pageSize) {
            this.pageSize = pageSize;
        }

        public Integer getTotalNum() {
            return totalNum;
        }

        public void setTotalNum(Integer totalNum) {
            this.totalNum = totalNum;
        }

        public Integer getIsMore() {
            return isMore;
        }

        public void setIsMore(Integer isMore) {
            this.isMore = isMore;
        }

        public Integer getTotalPage() {
            return totalPage;
        }

        public void setTotalPage(Integer totalPage) {
            this.totalPage = totalPage;
        }

        public Integer getStartIndex() {
            return startIndex;
        }

        public void setStartIndex(Integer startIndex) {
            this.startIndex = startIndex;
        }

        public List getItems() {
            return items;
        }

        public void setItems(List items) {
            this.items = items;
        }
}

 二、编写测试接口:

@Controller
public class FtTestController {

    @Autowired
    private FtTestService ftTestService;

    @RequestMapping(value = "/ft/build/select",method = RequestMethod.GET)
    @ResponseBody
    public PageBean getFtBuildInfo(int pageIndex,int pageSize){
        return ftTestService.getFtBuildPage(new FtBuildInfo(),pageIndex,pageSize);
    }
}

三、编写服务层:

@Service
public class FtTestService {

    @Autowired
    private FtTestDao ftTestDao;

    public PageBean getFtBuildPage(FtBuildInfo ftBuildInfo, int currentPage, int pageSize) {
        Map map = new HashMap<>();

        if(ftBuildInfo.getId()!=0) {
            map.put("id", ftBuildInfo.getId());
        }
        if(ftBuildInfo.getProjectName()!=null) {
            map.put("projectName", ftBuildInfo.getProjectName());
        }
        if(ftBuildInfo.getProjectVersion()!=null) {
            map.put("projectVersion", ftBuildInfo.getProjectVersion());
        }
        if(ftBuildInfo.getStartTime()!=0) {
            map.put("startTime", ftBuildInfo.getStartTime());
        }
        if(ftBuildInfo.getEndTime()!=0) {
            map.put("endTime", ftBuildInfo.getEndTime());
        }
        map.put("pageStart", (currentPage-1)*pageSize);
        map.put("pageSize", pageSize);
        List buildInfos = ftTestDao.getBuildInfoPage(map);//查询全部测试结果
        int totalCount = ftTestDao.getTotalCount(ftBuildInfo);//根据条件查询符合条件的总数
        PageBean pageData = new PageBean<>(currentPage, pageSize, totalCount);
        pageData.setItems(buildInfos);
        return pageData;

    }

 四、编写数据访问层:

public interface FtTestDao {

    List getBuildInfoPage(Map map);

    int getTotalCount(FtBuildInfo ftBuildInfo);
}

 五、编写xml文件:

/*查询符合条件额集合*/

总结:后台实现分页其实也没有那么难,好好思考一下是不是感觉也挺简单的。希望能帮助哪些刚入行的程序猿

 

你可能感兴趣的:(后台分页)