java分页查询的实现

19 篇文章 0 订阅
7 篇文章 0 订阅
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.util.List,com.zl.entity.*" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>商品操作页面</title>
</head>
<body>
<c:choose>
    <c:when test="${ empty userinfo}">
     <jsp:forward page="login.html"></jsp:forward>
    </c:when>
    <c:otherwise>
    <c:out value="欢迎${userinfo.name}来到系统"></c:out>
<center>
<br/><br/><br/>
    <table width=70% border=1 cellspacing=0 cellpadding=0 style='text-align:center'>
        <caption>商品列表</caption>
            <tr>
                <th>商品编号</th>
                <th>商品名称</th>
                <th>商品价格</th>
                <th>商品描述</th>
                <th>生产日期</th>
                <th>商品类型</th>
                <th>操作</th>
            </tr>  
             <c:forEach items="${goodlist}" var="goodlist">           
            <tr>
               
	            <td>${goodlist.gid}</td>
	            <td>${goodlist.gname}</td>
	            <td>${goodlist.price}</td>
	            <td>${goodlist.gdesc}</td>
	            <td>${goodlist.createDate}</td>
	            <td>${goodlist.typename}</td>
	            <td><a href="goodsCRUD?method=showUpdate&gid=${goodlist.gid}">修改</a>&nbsp;|&nbsp;<a href="goodsCRUD?method=del&gid=${goodlist.gid}">删除</a></td>
              
            </tr>
              </c:forEach> 
            <tr>
                <td colspan="7"><a href="goodsCRUD?method=getGoodstype">添加</a></td>
            </tr>
      </table>
      <br/><br/>
      <a href="goodsCRUD?method=showSplit&currPage=1">首页</a>
      <a href="goodsCRUD?method=showSplit&currPage=${page.currPage-1<=1?1:page.currPage-1}">上一页</a>
      <c:forEach begin="1" end="${page.totalPage}" var="i">
        <a href="goodsCRUD?method=showSplit&currPage=${i}">${i}</a>
      </c:forEach>
      <a href="goodsCRUD?method=showSplit&currPage=${page.currPage+1>=page.totalPage?page.totalPage:page.currPage+1}">下一页</a>
      <a href="goodsCRUD?method=showSplit&currPage=${page.totalPage}">最后一页</a>
</center>
</c:otherwise>
</c:choose>
</body>
</html>
package com.zl.service;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import com.zl.base.BaseServlet;
import com.zl.dao.GoodCRUDDao;
import com.zl.dao.GoodsTypeDao;
import com.zl.daoimpl.GoodsDaoImpl;
import com.zl.daoimpl.GoodsTypeDaoImpl;
import com.zl.entity.Goods;
import com.zl.entity.Goodstype;
import com.zl.util.Page;

/**
 * 
 * @author 丢了风筝的线
 *
 * @see 2020年1月10日
 */
@WebServlet("/goodsCRUD")
public class GoodsService extends BaseServlet {
	GoodCRUDDao gd = new GoodsDaoImpl();
	GoodsTypeDao dta = new GoodsTypeDaoImpl();
	Page page = new Page();

	// 分页显示数据
	public void showSplit(HttpServletRequest rq, HttpServletResponse rp) throws ServletException, IOException {
		String currPage = rq.getParameter("currPage");
		if (currPage == null || currPage == "") {
			currPage = "1";
		}
		// 设置当前页数
		page.setCurrPage(Integer.parseInt(currPage));
		// 设置当前页面的起始记录数
		page.setStartNum(Integer.parseInt(currPage));
		// 计算最大的页数
		int n = gd.count();
		page.setTotalPage(n);
		rq.setAttribute("page", page);
		List<Goods> list = gd.selectSplit(page);
		if (list.size() > 0) {
			// 绑定list数据,发送到商品操作页面
			rq.setAttribute("goodlist", list);
			rq.getRequestDispatcher("WEB-INF/goodsCRUD.jsp").forward(rq, rp);
		} else {
			rp.getWriter().print("当前没有商品");
		}
	}

	// 删除商品
	public void del(HttpServletRequest rq, HttpServletResponse rp) throws ServletException, IOException {

		int n = gd.del(Integer.parseInt(rq.getParameter("gid")));
		if (n > 0) {
			showSplit(rq, rp);
		} else {
			rp.getWriter().print("删除失败!");
			rp.sendRedirect("goodsCRUD?method=showSplit");
		}

	}

	// 添加商品
	public void add(HttpServletRequest rq, HttpServletResponse rp) throws ServletException, IOException {
		Goods g = new Goods();
		try {
			Map<String, String[]> map = rq.getParameterMap();
			if (map.isEmpty()) {
				rp.getWriter().print("添加失败");
			} else {
				BeanUtils.populate(g, map);
				int n = gd.add(g);
				if (n > 0) {
					rp.sendRedirect("goodsCRUD?method=showSplit");
				}
			}
		} catch (IllegalAccessException e) {

			e.printStackTrace();
		} catch (InvocationTargetException e) {

			e.printStackTrace();
		}

	}

	// 获取商品的所有类型
	public void getGoodstype(HttpServletRequest rq, HttpServletResponse rp) throws ServletException, IOException {
		List<Goodstype> typelist = dta.selectAll();
		if (typelist != null) {
			rq.setAttribute("typelist", typelist);
			rq.getRequestDispatcher("WEB-INF/addGoods.jsp").forward(rq, rp);
		}

	}

	// 展示要修改的商品的信息
	public void showUpdate(HttpServletRequest rq, HttpServletResponse rp) throws ServletException, IOException {

		int gid = Integer.parseInt(rq.getParameter("gid"));
		Goods g = gd.selectById(gid);
		if (g != null) {
			rq.setAttribute("good", g);
			rq.getRequestDispatcher("WEB-INF/goodUpdate.jsp").forward(rq, rp);
		}
	}

	// 修改商品的信息
	public void update(HttpServletRequest rq, HttpServletResponse rp) throws ServletException, IOException {
		Goods g = new Goods();
		try {
			BeanUtils.populate(g, rq.getParameterMap());
			int n = gd.update(g);
			rp.sendRedirect("goodsCRUD?method=showSplit");

		} catch (IllegalAccessException e) {

			e.printStackTrace();
		} catch (InvocationTargetException e) {

			e.printStackTrace();
		}
	}
}

package com.zl.util;

/**
 * 当前页实体类
 * 
 * @author 丢了风筝的线
 *
 * @see 2020年1月14日
 */
public class Page {
	private int currPage;// 当前的页数
	private static int SIZE = 5;// 每页的显示数
	private int totalPage;// 总页数
	private int startNum;// 当前页起始记录

	public Page() {
		init();

	}

	public Page(int currPage, int totalPage, int startNum) {
		super();
		this.currPage = currPage;
		this.totalPage = totalPage;
		this.startNum = startNum;
	}

	public void init() {
		if (currPage <= 1) {
			currPage = 1;
		}
	}

	public int getCurrPage() {
		return currPage;
	}

	public void setCurrPage(int currPage) {
		this.currPage = currPage;
	}

	public int getSize() {
		return SIZE;
	}

	public void setSize(int size) {
		this.SIZE = size;
	}

	public int getTotalPage() {

		return totalPage;
	}

	public void setTotalPage(int n) {
		if (n % SIZE == 0) {
			this.totalPage = n / SIZE;
		} else {
			this.totalPage = n / SIZE + 1;
		}
	}

	public int getStartNum() {

		return startNum;
	}

	public void setStartNum(int currPage) {
		this.startNum = (currPage - 1) * SIZE;
	}

}

package com.zl.daoimpl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import com.zl.dao.GoodCRUDDao;
import com.zl.entity.Goods;
import com.zl.util.Page;

/**
 * 商品操作实现类
 * 
 * @author 丢了风筝的线
 *
 * @see 2020年1月9日
 */
public class GoodsDaoImpl implements GoodCRUDDao {
	// 查询所有商品信息
	@Override
	public List<Goods> selectAll() {
		String sql = "select * from goods";
		List<Goods> list = null;
		try {
			list = QR.query(sql, new BeanListHandler<Goods>(Goods.class));

		} catch (SQLException e) {

			e.printStackTrace();
		}

		return list;
	}

	@Override
	public Goods selectById(int id) {
		String sql = "select * from goods where gid = ?";
		Goods g = null;
		try {
			g = QR.query(sql, new BeanHandler<Goods>(Goods.class), id);
		} catch (SQLException e) {

			e.printStackTrace();
		}
		return g;
	}

	@Override
	public int add(Goods g) {
		String sql = "insert into goods(gname,price,gdesc,createDate,typename)values(?,?,?,?,?)";
		int n = 0;
		try {
			n = QR.update(sql, g.getGname(), g.getPrice(), g.getGdesc(), g.getCreateDate(), g.getTypename());

		} catch (SQLException e) {

			e.printStackTrace();
		}
		return n;
	}

	@Override
	public int del(int id) {
		String sql = "delete from goods where gid=?";
		int n = 0;
		try {
			n = QR.update(sql, id);
		} catch (SQLException e) {

			e.printStackTrace();
		}
		return n;
	}

	@Override
	public int update(Goods g) {
		String sql = "update goods set gname=?,price=?,gdesc=?,createDate=? where gid=?";
		int n = 0;
		try {
			n = QR.update(sql, g.getGname(), g.getPrice(), g.getGdesc(), g.getCreateDate(), g.getGid());
		} catch (SQLException e) {

			e.printStackTrace();
		}
		return n;
	}

	@Override
	public List<Goods> selectSplit(Page page) {
		String sql = "select * from goods limit ?,?";
		List<Goods> list = null;
		try {
			list = QR.query(sql, new BeanListHandler<Goods>(Goods.class), page.getStartNum(), page.getSize());
		} catch (SQLException e) {

			e.printStackTrace();
		}
		return list;
	}

	// 查询商品的记录数
	public int count() {
		String sql = "select count(gid) from goods";
		Number n = 0;
		try {
			n = QR.query(sql, new ScalarHandler<Number>());
		} catch (SQLException e) {

			e.printStackTrace();
		}
		return n.intValue();
	}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值