Controller
这里传的status是查询条件,可以根据需求更改。
还有一个要点,查询的分页数据的条件,查询总记录数也要加上一样的条件,不然分页算法会计算出错。
@RequestMapping(value="/eventPageList", produces = "text/json;charset=UTF-8")
@ResponseBody
public String eventPageList(@RequestParam("status") int status,HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
//System.out.println("方法执行了...");
response.setContentType("textml;charset=UTF-8");
response.setHeader("Content-type", "application/json;charset=UTF-8");
JsonResult jr = new JsonResult();
int currentPage=request.getParameter("page")==null?1:Integer.parseInt(request.getParameter("page"));
int pageSize=30;
SinoHttpNoticeListAll pageData=eventNotificationService.queryEventPageList(status,currentPage,pageSize);
jr.setData("pageData", pageData);
String r = JSON.toJSONString(jr);
return r;
Service 分页算法,这里总页数也可以这么写 。
int pageCount=(int) Math.ceil(resultCount/pageSize);
public SinoHttpNoticeListAll queryEventPageList(int status,int currentPage, int pageSize) {
SinoHttpNoticeListAll page=new SinoHttpNoticeListAll();
//总记录数
int resultCount=eventNotificationDao.findResultCount(status);
int startIndex=(currentPage-1)*pageSize;
int pageCount=(resultCount%pageSize==0)?resultCount/pageSize:(resultCount/pageSize+1);
if(resultCount==0){
page.setCount(0);
page.setCurrentPage(currentPage);
page.setPageSize(pageSize);
page.setTotalPage(0);
return page;
}
List<SinoHttpNoticeListAll> list3=eventNotificationDao.findResultCountListData(status,startIndex,pageSize);
page.setCount(resultCount);
page.setCurrentPage(currentPage);
page.setPageSize(pageSize);
page.setTotalPage(pageCount);
page.setPageData(list3);
return page;
}
Dao 我用Spring JDBC 查询的,这里根据不同的框架和需求更改sql,但是思路差不多,可以进行参考。
findResultCount Dao
public int findResultCount(int status) {
// TODO Auto-generated method stub
Map<String, Object> paramMap = new HashMap<String, Object>();
String sql = " SELECT "
+" count( t1.id ) "
+" FROM "
+" exhibit_sino_http_notice_list t1, "
+" exhibit_sino_http_notice_type t2, "
+" exhibit_sino_http_result t3 "
+" WHERE "
+" t1.type = t2.id "
+" AND t1.siteId = t3.id "
+" AND status = :status ";
// 1:已通知/0:待通知
if(status == 0){
paramMap.put("status", "待通知");
}else if(status == 1){
paramMap.put("status", "已通知");
}
return this.getNamedParameterJdbcTemplate().queryForInt(sql, paramMap);
}
findResultCountListData Dao
public List<SinoHttpNoticeListAll> findResultCountListData(int status,int startIndex, int pageSize) {
Map<String, Object> paramMap = new HashMap<String, Object>();
String sql = " SELECT "
+" t1.id, "
+" t1.recipient, "
+" t2.taskType, "
+" t1.time, "
+" t1.noticeId, "
+" t1.taskId, "
+" t1.status, "
+" t1.noticeDescribe, "
+" t1.domainName, "
+" t3.name, "
+" t3.startlink, "
+" t3.ip "
+" FROM "
+" exhibit_sino_http_notice_list t1, "
+" exhibit_sino_http_notice_type t2, "
+" exhibit_sino_http_result t3 "
+" WHERE "
+" t1.type = t2.id "
+" AND t1.siteId = t3.id "
+" AND t1.status = :status "
+" LIMIT "+startIndex+","+pageSize ;
// 1:已通知/0:待通知
if(status == 0){
paramMap.put("status", "待通知");
}else if(status == 1){
paramMap.put("status", "已通知");
}
return this.getNamedParameterJdbcTemplate().query(sql, paramMap, rowMapper3);
}
entity 分页的类,自己的类要继承这个分页类,并且加上一条 存返回的数据
eg: private List<SinoHttpNoticeListAll> pageData;
package cn.secure.entity;
import java.io.Serializable;
public class PageInfo implements Serializable {
private static final long serialVersionUID = 587754556498974978L;
private Integer currentPage;
private Integer pageSize;
private Integer count;
private Integer totalPage;
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 getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
public Integer getTotalPage() {
return totalPage;
}
public void setTotalPage(Integer totalPage) {
this.totalPage = totalPage;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
}
package cn.secure.entity;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
public class SinoHttpNoticeListAll extends PageInfo implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* id
*/
private int id;
/**
* 接收人
*/
private String recipient;
/**
* 类型
*/
private String type;
/**
* 时间
*/
private Date time;
/**
* 告警id
*/
private int noticeId;
/**
* taskid
*/
private int taskId;
/**
* 状态:(1:已通知/0:待通知)
*/
private String status;
/**
* 描述
*/
private String noticeDescribe;
/**
* 域名
*/
private String domainName;
/**
* 站点id
*/
private int siteId;
/**
* 站点名
*/
private String siteName;
/**
* url
*/
private String startlink;
/**
* ip
*/
private String ip;
private List<SinoHttpNoticeListAll> pageData;
public List<SinoHttpNoticeListAll> getPageData() {
return pageData;
}
public void setPageData(List<SinoHttpNoticeListAll> pageData) {
this.pageData = pageData;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getRecipient() {
return recipient;
}
public void setRecipient(String recipient) {
this.recipient = recipient;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}
public int getNoticeId() {
return noticeId;
}
public void setNoticeId(int noticeId) {
this.noticeId = noticeId;
}
public int getTaskId() {
return taskId;
}
public void setTaskId(int taskId) {
this.taskId = taskId;
}
public String getNoticeDescribe() {
return noticeDescribe;
}
public void setNoticeDescribe(String noticeDescribe) {
this.noticeDescribe = noticeDescribe;
}
public String getDomainName() {
return domainName;
}
public void setDomainName(String domainName) {
this.domainName = domainName;
}
public int getSiteId() {
return siteId;
}
public void setSiteId(int siteId) {
this.siteId = siteId;
}
public String getSiteName() {
return siteName;
}
public void setSiteName(String siteName) {
this.siteName = siteName;
}
public String getStartlink() {
return startlink;
}
public void setStartlink(String startlink) {
this.startlink = startlink;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}