jsp(我用的bootstrap框架,这里没有导入bootstrap代码)

<!-- 前面需要导入bootstrap -->
<c:if test="${pager.totalPage > 1}">      <!-- 总页数大于1,显示分页组件 -->
    <nav aria-label="Page navigation example">
        <ul class="pagination justify-content-center">
            <c:if test="${pager.currentPage > 1}">  <!-- 当前页数大于1,显示上一页 -->
                <li  class="page-item">
                    <a  class="page-link"  href="/book/queryBookByPage/${pager.prePage}" tabindex="-1" aria-disabled="true">上一页</a>
                </li>
            </c:if>
            <c:forEach begin="1" end="${pager.totalPage}"  var="item">  <!-- 循环pager的总页数 -->
                <li class="page-item"><a class="page-link" href="/book/queryBookByPage/${item}">${item}</a></li>
            </c:forEach>
            <c:if test="${pager.currentPage != pager.totalPage&&pager.totalPage > 1}"> <!-- 当前页数不等于总页数&&总页数大于1,显示下一页 -->
                <li  class="page-item">
                    <a class="page-link" href="/book/queryBookByPage/${pager.nextPage}">下一页</a>
                </li>
            </c:if>
        </ul>
    </nav>
</c:if>

Controller

 	//查询所有书籍
    @RequestMapping("/bookList")
    public String BookList(Model model){

        int totalCount = bookService.bookCount();  // 数据库查找所有数据总数(int类型)
        // 分页
        Pager pager = new Pager();     // 初始化pager类
        pager.setTotalCount(totalCount);   // 设置总条数
        pager.setPageSize(10);                 // 设置一页显示条数(我这里是10,可以更改)
        pager.setCurrentPage(1);              // 设置当前页数(这里是最开始进入list页面,默认为第一页)
        // 设置总页数
        pager.setTotalPage(totalCount%pager.getPageSize()==0?totalCount/pager.getPageSize():totalCount/pager.getPageSize()+1); 
		
		//根据pager查询当前页数的数据
        List<Book> books = bookService.queryBookByPage(pager);
		//传到前台
        model.addAttribute("pager" , pager);
        model.addAttribute("bookList",books);

        return "book/bookList";
    }


	//分页查询
    @RequestMapping("queryBookByPage/{currentPage}")
    public String queryBookByPage(@PathVariable("currentPage") int currentPage,Model model){
        int totalCount = bookService.bookCount();

        Pager pager = new Pager();
        pager.setPageSize(10);
        pager.setTotalCount(totalCount);
        pager.setCurrentPage(currentPage);
        pager.setLimitCount((currentPage-1) * pager.getPageSize());
        pager.setTotalPage(totalCount%pager.getPageSize()==0?totalCount/pager.getPageSize():totalCount/pager.getPageSize()+1);

        List<Book> books = bookService.queryBookByPage(pager);

        model.addAttribute("pager",pager);
        model.addAttribute("bookList",books);
        return "book/bookList";
    }

Service

	// 返回数据库查出来的总条数
    public int bookCount(){
        return bookDao.bookCount();
    }
	// 返回对应的list
    public List<Book> queryBookByPage(Pager pager){
        return bookDao.queryBookByPage(pager);
    }

Dao

public class bookDao {

   private final static String NAMESPACE = "twd.dao.bookDao.";
   @Resource
   private SqlSessionTemplate sqlSessionTemplate;

   
   public int bookCount(){
       return sqlSessionTemplate.selectOne(NAMESPACE + "bookCount");
   }

   public List<Book> queryBookByPage(Pager pager){
       List<Book> bookList = sqlSessionTemplate.selectList(NAMESPACE + "queryBookByPage" , pager);
       return bookList;
   }
      
}

book.xml

<mapper namespace="twd.dao.bookDao">

    <select id="bookCount" resultType="int">
        select count(*) from book where isvalid = 1
    </select>

    <select id="queryBookByPage" resultType="twd.bean.book" parameterType="twd.utils.Pager">
        select * from book b,author a,category c where b.author_id=a.id and b.category_id=c.id and b.isvalid = 1 order by b.id asc limit #{limitCount} , #{pageSize}
    </select>
    
</mapper>

Pager实体类

package twd.utils;

public class Pager {
    // 每页多少条记录
    private int pageSize;
    // 当前是第几页
    private int currentPage;
    // 总页数
    private int totalPage;
    // 总数据条数
    private int totalCount;

    //limit起始数
    private int limitCount;

    // 获取上一页
    public int getPrePage() {
        return currentPage==1?1:currentPage-1;
    }
    // 获取下一页
    public int getNextPage() {
        return currentPage==totalPage?totalPage:currentPage+1;
    }

	//get,set省略
	…………
 
}

更多推荐

SSM,JAVA分页