首先是分页实体类Pageinfo
package dk.entity;
import java.util.List;
/**
* 分页
*/
public class PageInfo<T> {
private Integer currentPage;//当前页
private Integer pageSize;//每页显示记录数
private Integer totalPage;//总页数
private Integer totalRecord;//总记录数
private List<T> tlist;//存储对象
private Integer form;//起始页
public void setTotalPage(Integer totalPage) {
this.totalPage = totalPage;
}
public void setForm(Integer form) {
this.form = form;
}
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 getTotalRecord() {
return totalRecord;
}
public void setTotalRecord(Integer totalRecord) {
this.totalRecord = totalRecord;
}
public List<T> getTlist() {
return tlist;
}
public void setTlist(List<T> tlist) {
this.tlist = tlist;
}
public Integer getTotalPage() {
this.totalPage=this.totalRecord/this.pageSize;
if(this.totalRecord%this.pageSize!=0){
this.totalPage++;
}
return totalPage;
}
public Integer getForm() {
return (this.currentPage-1)*this.pageSize;
}
}
业务的实体类Information
package dk.entity;
import java.sql.Timestamp;
/**
* 资讯
*/
import java.util.Date;
public class Information {
private Integer inforid; //编号
private Integer userid; //用户编号
private Integer typeid; //资讯类型编号
private Timestamp infortime; //时间
private String backpic; //图片路径
private String context; //内容
private String title; //标题
private String name; //用户姓名
private User user;
private Infortype infortype;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Infortype getInfortype() {
return infortype;
}
public void setInfortype(Infortype infortype) {
this.infortype = infortype;
}
public Integer getTypeid() {
return typeid;
}
public void setTypeid(Integer typeid) {
this.typeid = typeid;
}
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getInforid() {
return inforid;
}
public void setInforid(Integer inforid) {
this.inforid = inforid;
}
public Integer getUserid() {
return userid;
}
public void setUserid(Integer userid) {
this.userid = userid;
}
public Timestamp getInfortime() {
return infortime;
}
public void setInfortime(Timestamp infortime) {
this.infortime = infortime;
}
public String getBackpic() {
return backpic;
}
public void setBackpic(String backpic) {
this.backpic = backpic;
}
}
Dao 数据访问层InformationDao
package dk.dao;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import dk.entity.Information;
import dk.entity.PageInfo;
public interface InformationDao {
/**
* 根据类型查询所有信息
*/
List<Information> selectInformationAll(@Param("from") Integer from, @Param("pageSize") Integer pageSize,
@Param("typeid") String typeid)throws Exception;
/**
* 根据类型查所有信息数量
*/
Integer informationCount(@Param("typeid") String typeid)throws Exception;
}
InformationDao.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="dk.dao.InformationDao">
<resultMap type="dk.entity.Information" id="inforid">
<id property="id" column="inforid"/>
<result property="userid" column="userid"/>
<result property="infortime" column="infortime"/>
<result property="backpic" column="backpic"/>
<result property="typeid" column="typeid"/>
<result property="context" column="context"/>
<result property="title" column="title"/>
<association property="User" javaType="dk.entity.User">
<id property="id" column="userid"/>
<result property="account" column="account"/>
<result property="password" column="password"/>
<result property="name" column="name"/>
<result property="sex" column="sex"/>
<result property="occupation" column="occupation"/>
<result property="workunit" column="workunit"/>
<result property="phone" column="phone"/>
</association>
</resultMap>
<resultMap type="dk.entity.Information" id="information">
<id property="inforid" column="inforid"/>
<result property="userid" column="userid"/>
<result property="infortime" column="infortime"/>
<result property="backpic" column="backpic"/>
<result property="typeid" column="typeid"/>
<result property="context" column="context"/>
<result property="title" column="title"/>
<association property="infortype" column="typeid"
select="dk.dao.InfortypeDao.selectById"></association>
<association property="user" column="userid"
select="dk.dao.UserDao.selectUserById"></association>
</resultMap>
<select id="selectInformationAll" resultMap="information">
SELECT * FROM information i,USER u WHERE i.userid=u.userid
<if test="typeid!=null and typeid!=''">
and typeid LIKE concat('%',#{typeid},'%')
</if>
GROUP BY i.inforid ORDER BY i.infortime DESC LIMIT #{from},#{pageSize};
</select>
<select id="informationCount" resultType="Integer">
SELECT COUNT(*) FROM information i,USER u WHERE i.userid=u.userid
<if test="typeid!=null and typeid!=''">
and typeid LIKE concat('%',#{typeid},'%')
</if>
</select>
</mapper>
BIz业务逻辑层InformationBiz
package dk.biz;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import dk.entity.Information;
public interface InformationBiz {
/**
* 根据类型查询所有信息
*/
List<Information> findInformationAll(@Param("from") Integer from, @Param("pageSize") Integer pageSize,
@Param("typeid") String typeid)throws Exception;
/**
* 根据类型查询所有信息数量
*/
Integer informationCount(@Param("typeid") String typeid)throws Exception;
}
Service服务层InformationBizImpl
package dk.biz.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import dk.biz.InformationBiz;
import dk.dao.InformationDao;
import dk.entity.Information;
@Service
public class InformationBizImpl implements InformationBiz {
@Resource
private InformationDao informationDao;
@Override
public List<Information> findInformationAll(Integer from, Integer pageSize, String typeid) throws Exception {
// TODO Auto-generated method stub
return this.informationDao.selectInformationAll(from, pageSize, typeid);
}
@Override
public Integer informationCount(String typeid) throws Exception {
// TODO Auto-generated method stub
return this.informationDao.informationCount(typeid);
}
}
Controler层InformationController
package dk.web;
import java.io.File;
import java.
sql.Timestamp;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.alibaba.fastjson.JSON;
import dk.biz.InformationBiz;
import dk.biz.InfortypeBiz;
import dk.entity.*;
@Controller
public class InformationController {
@Resource
private InformationBiz informationBiz;
@Resource
private InfortypeBiz infortypeBiz;
/**
* 去健康主题页 分页
*/
@RequestMapping("/toHealthItem")
public String toInformationRelease(Model model,
@RequestParam(value = "currentPage", required = false) Integer currentPage,
@RequestParam(value = "typeid", required = false) String typeid) throws Exception {
if (currentPage == null) {
currentPage = 1;
}
PageInfo<Information> pageInfo = new PageInfo<Information>();
pageInfo.setPageSize(6);
pageInfo.setCurrentPage(currentPage);
List<Information> informationList=this.informationBiz.findInformationAll(pageInfo.getForm(), pageInfo.getPageSize(), typeid);
Integer totalRecord = this.informationBiz.informationCount(typeid);
List<Infortype> inforTypeList=this.infortypeBiz.findAll();
pageInfo.setTotalRecord(totalRecord);
pageInfo.setTlist(informationList);
model.addAttribute("pageInfo", pageInfo);
model.addAttribute("inforTypeList", inforTypeList);
model.addAttribute("informationList", informationList);
return "fore-end/healthItem";
}
//无刷新分页
@ResponseBody
@RequestMapping("/wushuaxinfenye")
public String wushuaxinfenye(HttpServletRequest request,@RequestParam(value = "currentPage", required = false) Integer currentPage,@RequestParam(value = "typeid", required = false) String typeid,Model model) throws Exception{
if (currentPage == null || currentPage == 0) {
currentPage = 1;
}
PageInfo<Information> pageInfo = new PageInfo<Information>();
pageInfo.setPageSize(6);
pageInfo.setCurrentPage(currentPage);
List<Information> informationList=this.informationBiz.findInformationAll(pageInfo.getForm(), pageInfo.getPageSize(), typeid);
Integer totalRecord = this.informationBiz.informationCount(typeid);
List<Infortype> inforTypeList=this.infortypeBiz.findAll();
pageInfo.setTotalRecord(totalRecord);
pageInfo.setTlist(informationList);
model.addAttribute("pageInfo", pageInfo);
model.addAttribute("inforTypeList", inforTypeList);
model.addAttribute("informationList", informationList);
return JSON.toJSONString(pageInfo);
}
}
Jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page import="dk.entity.PageInfo"%>
<%
PageInfo pageInfo = (PageInfo) request.getAttribute("pageInfo");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="css/reset.css" />
<link rel="stylesheet" type="text/css" href="css/style.css" />
<title></title>
<style type="text/css">
#tiaoPage{
-web-kit-appearance:none;
-moz-appearance: none;
font-size:1.4em;
height:1.4em;
width:3em;
border-radius:4px;
border:1px solid #c8cccf;
color:#6a6f77;
outline:0;
}
</style>
</head>
<body>
<div class="insideList" >
</div>
<script src="js/jquery.min.js"></script>
<script>
//分页
function tiaozhuan(xuHao,typeid){
var currentPage=null;
var getTotalPage=null;
currentPage=$('#cpage').text();
getTotalPage=$('#tpage').text();
switch(xuHao)
{
case 0: if(currentPage!=1){
currentPage=1;
// $('#cpage').text(currentPage);
}else{
alert("已是首页!");
};
break;
case 1:if(currentPage>1){
currentPage--;
// $('#cpage').text(currentPage);
}else{
alert("已是首页!");
};
break;
case 2:if(parseInt(currentPage)<parseInt(getTotalPage)){
currentPage++;
// $('#cpage').text(currentPage);
}else{
alert("已是尾页!");
};
break;
case 3:if(currentPage!=getTotalPage){
currentPage=getTotalPage
// $('#cpage').text(currentPage);
}else{
alert("已是尾页!");
};
break;
case 4: if($('#tiaoPage').val()>getTotalPage){
alert("没有该页!!!");
}else{
currentPage=$('#tiaoPage').val();
$('#cpage').val(currentPage);};
break;
}
var num=$('#num').val();
if(num==''){num=0;}
$.ajax({
url:"wushuaxinfenye",
type:"post",
datatype:"json",
data:{"typeid":typeid,
"currentPage":currentPage},
success:function(data){
if(data !=null){
$('#tpage').text(data.totalPage);
$('#cpage').text(data.currentPage);
var arr=data.tlist;
$('.insideList').html("");//清空数据
for(var i=0;i<data.tlist.length;i++){
var time=new Date(arr[i].infortime).toLocaleString()
$('.insideList').append("<ul class='leftList'>"
+"<li>"
+"<div class='photo'>"+"<a href='selectInforDtl?inforid="+arr[i].inforid+"'>"+'查看详情'+"</a>"+"<img src='statics/uploadfiles/"+arr[i].backpic+"' alt='' width='240' height='140'/>"+"</div>"
+"<dl>"
+"<dt>"
+"<a href='selectInforDtl?inforid="+arr[i].inforid+"' class='f-20 c-main'>"+arr[i].title+"</a>"
+"<p class='f-18 c-999'>"+"<span class='c-999 mr-20'>"+arr[i].name+"</span>"+'|'+"<span class='c-999 ml-20'>"+time+"</span>"+"</p>"
+"</dt>"
+"<dd class='mt-10 f-14'>"
+"<p class='c-999' style=' -webkit-box-orient: vertical;-webkit-line-clamp: 2;overflow : hidden;display: -webkit-box;text-overflow: ellipsis;'>"+arr[i].context+"</p>"
+"</dd>"
+"</dl>"
+"</li>"
+"</ul>"
);
}
$('.insideList').append("<div class='pageBox'>"
+"<ul class='page'>"
+"<li id='shouye_id' onClick=tiaozhuan(0,"+typeid+")>"+"首页"+"</li>"
+"<li id='shangyiye_id' onClick=tiaozhuan(1,"+typeid+")>"+"上一页"+"</li>"
+"<li>"+"<lable id='cpage'>"+data.currentPage+"</lable>"+"/"+"<lable id='tpage'>"+data.totalPage+"</lable>"+"</li>"
+"<li id='xiayiye_id' onClick=tiaozhuan(2,"+typeid+")>"+"下一页"+"</li>"
+"<li id='weiye_id' onClick=tiaozhuan(3,"+typeid+") >"+"尾页"+"</li>"
+"<li onClick=tiaozhuan(4,"+typeid+")>"+"跳转  "+"<input type='text' id='tiaoPage' value='1'/>"+"</li>"
+"</ul>"
+"</div>");
}else{alert("没有该页数据");
$('#cpage').val(1);
}
}
});
}
</script>
</body>
</html>