SpringData JPA实现查询分页demo

网友投稿 314 2023-06-07


SpringData JPA实现查询分页demo

SpringData JPA 的 PagingAndSortingRepository接口已经提供了对分页的支持,查询的时候我们只需要传入一个 org.springframework.data.domain.Pageable

接口的实现类,指定PageNumber和pageSize即可

springData包中的 PageRequest类已经实现了Pageable接口,我们可以直接使用下边是部分代码:

DAO:

package com.jiaoyiping.jdjy.sourcecode.dao;

import com.jiaoyiping.jdjy.sourcecode.bean.SourceCode;

import org.springframework.data.repository.PagingAndSortingRepository;

/**

* Created with IntelliJ IDEA.

* User: 焦一平

* Date: 14-11-20

* Time: 下午11:18

* To change this template use File | Settings | File Templates.

*/

public interface SourceCodeDao extends PagingAndSortingRepository {

}

service:

package com.jiaoyiping.jdjy.sourcecode.service;

import com.jiaoyiping.jdjy.sourcecode.bean.SourceCode;

import com.jiaoyiping.jdjy.sourcecode.dao.SourceCodeDao;

import org.apache.solr.client.solrj.SolrServerException;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.domain.Page;

import org.springframework.data.domain.PageRequest;

import javax.transaction.Transactional;

import java.io.IOException;

import java.sql.Timestamp;

import java.util.List;

/**

* Created with IntelliJ IDEA.

* User: 焦一平

* Date: 14-11-20

* Time: 下午11:24

* To change this template use File | Settings | File Templates.

*/

public class SourceCodeService {

@Autowired

private SourceCodeDao sourceCodeDao;public Page getSourceCode(int pageNumber,int pageSize){

PageRequest request = this.buildPageRequest(pageNumber,pageSize);

Page sourceCodes= this.sourceCodeDao.findAll(request);

return sourceCodes;

}

  //构建PageRequest

private PageRequest buildPageRequest(int pageNumber, int pagzSize) {

return new PageRequest(pageNumber - 1, pagzSize, null);

}

}

controller:

package com.jiaoyiping.jdjy.sourcecode.controller;

import com.jiaoyiping.jdjy.sourcecode.Const;

import com.jiaoyiping.jdjy.sourcecode.bean.SourceCode;

import com.jiaoyiping.jdjy.sourcecode.service.SourceCodeService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.domain.Page;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

/**

* Created with IntelliJ IDEA.

* User: 焦一平

* Date: 14-11-20

* Time: 下午11:22

* To change this template use File | Settings | File Templates.

*/

@Controller

@RequestMapping(value = "/sourcecode")

public class SourceCodeController {

@Autowired

private SourceCodeService sourceCodeService;

@RequestMapping(value = "list")

public ModelAndView listSourceCode(HttpServletRequest request, HttpServletResponse response){

String pageNumberStr=request.getParameter("pageNumber");

if(pageNumberStr==null ||"".equals(pageNumberStr)){

pageNumberStr="1";

}

int pageNumber = Integer.parseInt(pageNumberStr);

int pageSize = Const.PAGE_SIZE;

ModelAndView modelAndView = new ModelAndView();

modelAndView.setViewName("/sourcecode/listSourceCode");

Page sourceCodes = this.sourceCodeService.getSourceCode(pageNumber, pageSize);

modelAndView.addObject("sourceCodeList",sourceCodes.getContent());

modelAndView.addObject("totalPageNumber",sourceCodes.getTotalElements());

modelAndView.addObjechttp://t("pageSize",pageSize);

return modelAndView;

}

}

前端分页:

前端分页组件我们使用bootstrap提供的分页组件:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<%--

Created by IntelliJ IDEA.

User: 焦一平

Date: 2014/12/27

Time: 9:57

To change this template use File | Settings | File Templates.

--%>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%

String basePath = request.getContextPath();

String MethodURL=basePath+"/sourcecode/list.action?pageNumber=";

%>

源代码列表

" class="list-group-item">

最终结果如下:


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java实现验证码验证功能
下一篇:Java中的集合框架
相关文章

 发表评论

暂时没有评论,来抢沙发吧~