天猫整站SSM-分页-总结(做个人学习笔记整理用)
生活随笔
收集整理的這篇文章主要介紹了
天猫整站SSM-分页-总结(做个人学习笔记整理用)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
天貓整站SSM-分頁-herf(做個人學習筆記整理用)
先寫Page.java
package com.how2java.tmall.util;public class Page {private int start; //開始頁數private int count; //每頁顯示個數private int total; //總個數private String param; //參數private static final int defaultCount = 5; //默認每頁顯示5條public int getStart() {return start;}public void setStart(int start) {this.start = start;}public int getCount() {return count;}public void setCount(int count) {this.count = count;}public Page (){count = defaultCount;}public Page(int start, int count) {this();this.start = start;this.count = count;}public boolean isHasPreviouse(){if(start==0)return false;return true;}public boolean isHasNext(){if(start==getLast())return false;return true;}public int getTotalPage(){int totalPage;// 假設總數是50,是能夠被5整除的,那么就有10頁if (0 == total % count)totalPage = total /count;// 假設總數是51,不能夠被5整除的,那么就有11頁elsetotalPage = total / count + 1;if(0==totalPage)totalPage = 1;return totalPage;}public int getLast(){int last;// 假設總數是50,是能夠被5整除的,那么最后一頁的開始就是45if (0 == total % count)last = total - count;// 假設總數是51,不能夠被5整除的,那么最后一頁的開始就是50elselast = total - total % count;last = last<0?0:last;return last;}@Overridepublic String toString() {return "Page [start=" + start + ", count=" + count + ", total=" + total + ", getStart()=" + getStart()+ ", getCount()=" + getCount() + ", isHasPreviouse()=" + isHasPreviouse() + ", isHasNext()="+ isHasNext() + ", getTotalPage()=" + getTotalPage() + ", getLast()=" + getLast() + "]";}public int getTotal() {return total;}public void setTotal(int total) {this.total = total;}public String getParam() {return param;}public void setParam(String param) {this.param = param;}}再寫CategoryMapper.xml:修改CategoryMapper.xml,以提供帶分頁的查詢語句和獲取總數的sql語句
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.how2java.tmall.mapper.CategoryMapper"><select id="list" resultType="Category">select * from category order by id desc<if test="start!=null and count!=null">limit #{start},#{count}</if></select><select id="total" resultType="int">select count(*) from category</select> </mapper>接著寫CategoryMapper
package com.how2java.tmall.mapper;import com.how2java.tmall.util.Page; import com.how2java.tmall.pojo.Category;import java.util.List;public interface CategoryMapper {public List<Category> list(Page page);public int total(); }CategoryService
package com.how2java.tmall.service;import com.how2java.tmall.pojo.Category; import com.how2java.tmall.util.Page; import java.util.List;public interface CategoryService{int total();List<Category> list(Page page); }CategoryServiceImpl
package com.how2java.tmall.service.impl; import com.how2java.tmall.util.Page; import com.how2java.tmall.mapper.CategoryMapper; import com.how2java.tmall.pojo.Category; import com.how2java.tmall.service.CategoryService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class CategoryServiceImpl implements CategoryService {@AutowiredCategoryMapper categoryMapper;@Overridepublic List<Category> list(Page page) {return categoryMapper.list(page);}@Overridepublic int total() {return categoryMapper.total();} }CategoryController
package com.how2java.tmall.controller;import com.how2java.tmall.pojo.Category; import com.how2java.tmall.service.CategoryService; import com.how2java.tmall.util.Page; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping;import java.util.List;@Controller @RequestMapping("") public class CategoryController {@AutowiredCategoryService categoryService;@RequestMapping("admin_category_list")public String list(Model model,Page page){List<Category> cs= categoryService.list(page);int total = categoryService.total();page.setTotal(total);model.addAttribute("cs", cs);model.addAttribute("page", page);return "admin/listCategory";}}listCategory.jsp
略
總結
以上是生活随笔為你收集整理的天猫整站SSM-分页-总结(做个人学习笔记整理用)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java手机飞行模式_用了这么多年手机才
- 下一篇: 自学笔记-使用MyBatis建立数据库基