在信息量较大的时候一般都需要分页查询,本文将展示如何实现分页查询算法。
1.首先建立一个实体类:
package cn.shinelon.vo;
/**
* 书类
* @author Shinelon
*
*/
public class Book {
private int id;
private String bname; //书名
private double bprice; //书的价格
private String bauthor; //书的作者
private int bbuy; //是否购买书,1表示购买,0表示不购买
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBname() {
return bname;
}
public void setBname(String bname) {
this.bname = bname;
}
public String getBauthor() {
return bauthor;
}
public void setBauthor(String bauthor) {
this.bauthor = bauthor;
}
public int getBbuy() {
return bbuy;
}
public void setBbuy(int bbuy) {
this.bbuy = bbuy;
}
public double getBprice() {
return bprice;
}
public void setBprice(double bprice) {
this.bprice = bprice;
}
public Book(int id, String bname, double bprice, String bauthor, int bbuy) {
super();
this.id = id;
this.bname = bname;
this.bprice = bprice;
this.bauthor = bauthor;
this.bbuy = bbuy;
}
public Book() {
// TODO Auto-generated constructor stub
}
}
然后就是实体类的映射文件:
<?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="mybatis">
<select id="bookSelect" resultType="cn.just.vo.Book">
select * from book
</select>
<insert id="bookInsert" parameterType="cn.just.vo.Book">
insert into book values(#{id},#{bname},#{bprice},#{bauthor},#{bbuy})
</insert>
<delete id="bookDelete" parameterType="int">
delete from book where id=#{id}
</delete>
</mapper>
2.DAO层
package cn.just.dao;
import java.util.List;
import cn.just.vo.Book;
public interface BookDao {
public List<Book> getBook();
}
package cn.just.dao.impl;
import java.util.List;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import cn.just.dao.BookDao;
import cn.just.vo.Book;
@Repository("bookDao")
public class BookDaoImpl extends SqlSessionDaoSupport implements BookDao{
@Autowired
@Override
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
super.setSqlSessionFactory(sqlSessionFactory);
}
@Override
public List<Book> getBook() {
return getSqlSession().selectList("mybatis.bookSelect");
}
}
3.service层:
package cn.just.service;
import java.util.List;
import cn.just.vo.Book;
public interface BookService{
public List<Book> getBook();
}
package cn.just.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.just.dao.BookDao;
import cn.just.service.BookService;
import cn.just.vo.Book;
@Service("bookService")
public class BookServiceImpl implements BookService{
@Autowired
public BookDao bookDao;
public List<Book> getBook() {
return bookDao.getBook();
}
public void setBookDao(BookDao bookDao) {
this.bookDao = bookDao;
}
public BookDao getBookDao() {
return bookDao;
}
}
4.controller层:
package cn.just.controller;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import cn.just.service.BookService;
import cn.just.vo.Book;
@Controller
public class BookController {
@Autowired
// 向前台展示总书列表
public BookService bookService;
List<Book> list = new ArrayList<Book>();
int total; // 图书的总数
int pages; // 前台视图显示的页数
// 分页
int currentpage; // 当前页
List<Book> currentlist = new ArrayList<Book>(); // 一页的书列表
int count;
@RequestMapping("/book")
public String getBook(int indexpages, ModelMap model, HttpServletRequest req) {
list = bookService.getBook();
total = list.size();
pages = total / 4 + 1; // 列表页数
count = (indexpages - 1) * 4 + 1;
currentlist.clear();
if (indexpages != pages)
for (int i = count - 1, j = 0; j < 4; i++, j++) {
// System.out.println("============" + list.get(i).getBname());
currentlist.add(list.get(i));
}
if (indexpages == pages)
for (int i = count - 1, j = 0; j < total % 4; i++, j++) {
System.out.println("============" + list.get(i).getBname());
currentlist.add(list.get(i));
}
model.addAttribute("currentlist", currentlist);
// System.out.println("==========" + pages);
return "/jsp/booklist.jsp?pages=" + pages;
}
public void setBookService(BookService bookService) {
this.bookService = bookService;
}
}
5.前端jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'rightFrame.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" src="./js/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#sub").click(function(){
var indexpages=$("#txt").val();
alert(indexpages);
if(indexpages!=""){
indexpages=parseInt(indexpages);
$.get("book.spring?indexpages="+indexpages,function(data,status){alert(data+" "+status);});
}else{
alert('请输入跳转的页数');
}
});
});
</script>
<script type="text/javascript" src="./js/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#btn").click(function(){
alert('点击');
$.post("insertCar.spring",{'uname':'刘军强','bname':$(#bname).val(),'bprice':$(#bprice),'bauthor':$(#bauthor)});
alert('成功加入购物车');
});
});
</script>
</head>
<body>
<table border="1" align="center">
<tr>
<th width="30%" align="center">书名</th>
<th width="30%" align="center">定价</th>
<th width="30%" align="center">作者</th>
<th width="30%" align="center">求购</th>
</tr>
<c:forEach items="${currentlist}" var="book">
<tr>
<td height="30%" align="center">${book.bname}<input type="hidden" id="bname" value="${book.bname}"></td>
<td height="30%" align="center">${book.bprice}<input type="hidden" id="bprice" value="${book.bprice}"></td>
<td height="30%" align="center">${book.bauthor}<input type="hidden" id="bauthor" value="${book.bauthor}"></td>
<td height="30%" align="center"><button id="btn" type="button">加入购物车</button></td>
</tr>
</c:forEach>
</table>
<%!
int pages=0; //默认从第一页开始
int currentpages=1; //设置当前页默认为1
%>
<%
pages=Integer.parseInt((String)request.getParameter("pages"));
%>
<%
if(pages>1&¤tpages>1&¤tpages!=pages){
%>
<a href="book.spring?indexpages=<%=currentpages=1%>" align="center">首页</a>
<a href="book.spring?indexpages=<%=currentpages=currentpages-1%>" align="center">上一页</a>
<a href="book.spring?indexpages=<%=currentpages=currentpages+1%>" align="center">下一页</a>
<%
}
%>
<%
if(pages>1&¤tpages==pages){
%>
<a href="book.spring?indexpages=<%=1%>" align="center">首页</a>
<a href="book.spring?indexpages=<%=currentpages=currentpages-1%>" align="center">上一页</a>
<%
}
%>
<%
if(pages>1&¤tpages==1){
%>
<a href="book.spring?indexpages=<%=currentpages=currentpages+1%>" align="center">下一页</a>
<%
}
//具体跳转到某一页
%>
<input type="text" name="pagecounts" id="txt" size="2"/>
<button id="sub" type="button">查询</button>
</body>
</html>
然后通过index.jsp页面切入:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
window.location="book.spring?indexpages=1";
</script>
</head>
<body>
</body>
</html>
如下图所示,访问index.jsp页面即可实现分页查询。