页面分页

来源:互联网 发布:小米note顶配 windows 编辑:程序博客网 时间:2024/05/21 14:08

查询列表,实现页面分页:

public class Page {private int curPage;//当前页private int length;//每页条数private int count;//总条数private int pageNum;//总页数private int start;public Page(int curPage ,int length){this.curPage = curPage;this.length = length;if(this.curPage < 1){this.curPage = 1;}if(this.length < 1){this.length = 1 ;}}public int getCurPage() {return curPage;}public void setCurPage(int curPage) {this.curPage = curPage;}public int getLength() {return length;}public void setLength(int length) {this.length = length;}public int getCount() {return count;}public void setCount(int count) {this.count = count;this.pageNum = (count+length-1) / length; if(pageNum == 0)pageNum = 1;if(pageNum < curPage){curPage = pageNum;}start = (curPage-1) * length;}public int getPageNum() {return pageNum;}public void setPageNum(int pageNum) {this.pageNum = pageNum;}public int getStart() {return start;}public void setStart(int start) {this.start = start;}}
sql语句:
           <select id="getById" parameterType="long" resultMap="ContentResultMap" >   <span style="white-space:pre"></span>   SELECT * FROM t_content WHERE id = #{id}<span style="white-space:pre"></span>   </select>                                                                                                                                                 <select id="queryContentList" parameterType="java.util.Map" resultMap="ContentResultMap"><span style="white-space:pre"></span>   <span style="white-space:pre"></span>SELECT <span style="white-space:pre"></span>   <span style="white-space:pre"></span>c.ID,<span style="white-space:pre"></span>   <span style="white-space:pre"></span>c.TITLE,<span style="white-space:pre"></span>   <span style="white-space:pre"></span>c.time,<span style="white-space:pre"></span>   <span style="white-space:pre"></span>c.pv,<span style="white-space:pre"></span>   <span style="white-space:pre"></span>c.from_name,<span style="white-space:pre"></span>   <span style="white-space:pre"></span>c.writer,<span style="white-space:pre"></span>   <span style="white-space:pre"></span>c.content,<span style="white-space:pre"></span>   <span style="white-space:pre"></span>c.top_thumb_url,<span style="white-space:pre"></span>   <span style="white-space:pre"></span>c.article_abstract ,<span style="white-space:pre"></span>   <span style="white-space:pre"></span>p.name as channel_name,//其他表字段<span style="white-space:pre"></span>   <span style="white-space:pre"></span>c.channel_id,<span style="white-space:pre"></span>   <span style="white-space:pre"></span>c.type <span style="white-space:pre"></span><span style="white-space:pre"></span>   <span style="white-space:pre"></span>FROM t_content c <span style="white-space:pre"></span>   <span style="white-space:pre"></span>LEFT JOIN t_plate p ON c.CHANNEL_ID = p.ID//左外连接<span style="white-space:pre"></span>   <span style="white-space:pre"></span>WHERE p.id = #{id} <span style="white-space:pre"></span>   <span style="white-space:pre"></span><if test="type != 0"><span style="white-space:pre"></span>   <span style="white-space:pre"></span>AND c.TYPE = #{type} <span style="white-space:pre"></span>   <span style="white-space:pre"></span></if><span style="white-space:pre"></span>   <span style="white-space:pre"></span>AND c.STATUS = 2<span style="white-space:pre"></span>   <span style="white-space:pre"></span>ORDER BY c.time DESC LIMIT #{start} , #{length}//分页时每一页长度<span style="white-space:pre"></span>   </select><span style="white-space:pre"></span>   <span style="white-space:pre"></span>   <select id="queryContentListCount" parameterType="java.util.Map" resultType="int"><span style="white-space:pre"></span>   <span style="white-space:pre"></span>SELECT count(c.id) FROM t_content c <span style="white-space:pre"></span>   <span style="white-space:pre"></span>LEFT JOIN t_plate p ON c.CHANNEL_ID = p.ID<span style="white-space:pre"></span>   <span style="white-space:pre"></span>WHERE p.id = #{id} <span style="white-space:pre"></span>   <span style="white-space:pre"></span><if test="type != 0"><span style="white-space:pre"></span>   <span style="white-space:pre"></span>AND c.TYPE = #{type} <span style="white-space:pre"></span>   <span style="white-space:pre"></span></if><span style="white-space:pre"></span>   <span style="white-space:pre"></span>AND c.STATUS = 2<span style="white-space:pre"></span>   </select>   <span style="white-space:pre"></span>
mapper、service、serviceimpl

public Content getById(long id);                                                                                                                          public List<Content> queryContentList(Map<String,Object> map);public int queryContentListCount(Map<String,Object> map);
public Content getContentById(long id);                                                                                                                   public List<Content> queryContentList(Page page,long id,int type);public int queryContentListCount(long id,int type);<span style="font-family: Arial, Helvetica, sans-serif;"></span>
public List<Content> queryContentList(Page page,long id,int type){Map<String,Object> map = new HashMap<String,Object>();map.put("start", page.getStart());map.put("length",page.getLength());map.put("id", id);map.put("type", type);List<Content> list = contentMapper.queryContentList(map);return list;}public int queryContentListCount(long id,int type){Map<String,Object> map = new HashMap<String,Object>();map.put("id", id);map.put("type", type);return contentMapper.queryContentListCount(map);}
controller

@RequestMapping(value="/list/{channelId}.html")public ModelAndView contentPage(@PathVariable long channelId,ModelMap model,HttpServletRequest request,HttpServletResponse rps,@RequestParam(defaultValue="1") int curPage) throws IOException{Channel channel = channelService.getById(channelId);String pageView = "";Page page = new Page(curPage,20);if(channel != null && channel.getList() != null && channel.getList().size() > 0){//跳转3级栏目页面pageView = "f/content3";}else{//查询内容页信息int count1 = contentService.queryContentListCount(channelId, 0);page.setCount(count1);List<Content> contentList = contentService.queryContentList(page, channelId, 0);if(count1 == 1){Content content = contentList.get(0);Customer customer = null;if(content.getDoctorId() != null){customer = customerService.getCustomerById(content.getDoctorId());if(customer != null){content.setJob(customer.getTitle());content.setDepartmentName(customer.getDeptmentName());}}contentService.updateContentPv(content.getId());content.setPv(content.getPv() + 1);model.addAttribute("content", content);pageView = "f/content";}else{//列表pageView = "f/contentList";model.addAttribute("contentList", contentList);model.addAttribute("page", page);}}model.addAttribute("curId", channelId);model.addAttribute("channel", channel);List<Channel> channelList = channel.getParent().getList();model.addAttribute("channelList", channelList);model.addAttribute("childChannelList", channel.getList());return new ModelAndView(pageView);}
jsp

<!--分页--><div class="feyestyle" style="float:none;"><c:set var="pageStart" value="1"></c:set><c:if test="${page.curPage - 7 > 1 }"><c:set var="pageStart" value="${page.curPage - 7  }"></c:set></c:if><c:set var="pageEnd" value="${page.pageNum }"></c:set><c:if test="${page.curPage + 7 < page.pageNum }"><c:set var="pageEnd" value="${page.curPage + 7  }"></c:set></c:if><c:choose><c:when test="${page.curPage - 1 > 0 }"><a href="<ph:root/>/list/${channel.id }.html?curPage=${page.curPage-1}">< 上一页</a></c:when><c:otherwise><span class="feyedisabled">< 上一页</span></c:otherwise></c:choose><c:forEach var="incPage" begin="${pageStart }" end="${pageEnd }"step="1"><c:choose><c:when test="${page.curPage == incPage }"><span class="feyecurrent"><c:out value="${incPage }"></c:out></span></c:when><c:otherwise><a href="<ph:root/>/list/${channel.id }.html?curPage=${incPage}"><c:out value="${incPage }"></c:out></a></c:otherwise></c:choose></c:forEach><c:choose><c:when test="${page.curPage + 1 <= page.pageNum }"><a href="<ph:root/>/list/${channel.id }.html?curPage=${page.curPage+1}">下一页  > </a></c:when><c:otherwise><span class="feyedisabled">< 下一页</span></c:otherwise></c:choose><span style="margin-left: 20px;">共计 <font class="feyefont-color">${page.count}</font> 条信息</span></div><!--分页-->








0 0
原创粉丝点击