天猫整站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
略