利用MySQL实现分页的web小项目

1、准备工作:

1.1、背景:

以一个网上书城为背景,每页展示5本待售的书籍,展示给用户的内容包括:书籍的名字、作者、售价。要求实现基本的页面跳转功能。

1.2、数据源:

在MySQL中新建一个test的数据库,里面新建一张表,表名:book ,涉及到字段有:id、name、author、price

1.3、需要导入的jar包:

针对MySQL数据库:mysql-connector-java-5.1.7-bin.jar、c3p0-0.9.2.1.jar、mchange-commons-java-0.2.3.4.jar
针对前台页面:jstl-1.0.2.jar、standard-1.0.1.jar(依赖包)
补充:jquery-1.7.2.js(前台jquery使用)、c3p0-config.xml(配置c3p0数据库连接池使用)

2、开发过程

2.1、后台:

2.1.1、Javabeans:Book.java和page.java

下面是Book.java(用来封装关于书籍book的类)
package com.xiaojie.beans;

public class Book {
	private Integer id;
	private String name;
	private String author;
	private Integer price;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	public Integer getPrice() {
		return price;
	}
	public void setPrice(Integer price) {
		this.price = price;
	}
	@Override
	public String toString() {
		return "Book [id=" + id + ", name=" + name + ", author=" + author + ", price=" + price + "]";
	}
	public Book(Integer id, String name, String author, Integer price) {
		super();
		this.id = id;
		this.name = name;
		this.author = author;
		this.price = price;
	}
	public Book() {
		super();
	}
	
}
下面是page.java(用来封装关于页面展示page的类)
package com.xiaojie.beans;

import java.util.List;

public class Page {
	private int pageno;
	private List books;
	private int pagesize=5;
	private long totalitemnumner;
	public Page(int pageno) {
		super();
		this.pageno = pageno;
	}
	public int getPageno() {
		if(pageno<0){
			pageno=1;
			
		}
		if(pageno>getTotalpagenumber()){
			pageno=getTotalpagenumber();
		}
		return pageno;
	}
	public int getPagesize() {
		return pagesize;
	}
	public void setBooks(List books) {
		this.books = books;
	}
	 public List getBooks() {
		return books;
	}
	
	//得到总共有多少页
	 public int getTotalpagenumber(){
		 int totalpagenumber=(int) (totalitemnumner/pagesize);
		 if(totalitemnumner%pagesize!=0){
			 totalpagenumber++;
		 }
		return totalpagenumber;
	 }
	 public void setTotalitemnumner(long totalitemnumner) {
		this.totalitemnumner = (int) totalitemnumner;
	}
	 //判断是否有下一页
	 public boolean ishasnext(){
		 if(getPageno()1){
			 return true;
		 }
		 return false; 
	 }
	 //得到上一页的页码
	 public int getprevpage(){
		 if(ishasprev()){
			 return getPageno()-1;
		 }
		return getPageno();
	 }
	//得到下一页的页码
	public int getnextpage(){
		if(ishasnext()){
			return getPageno()+1;
		}
		return getPageno();
	}
	@Override
	public String toString() {
		return "Page [pageno=" + pageno + ", books=" + books+ ", pagesize=" + pagesize + ", totalitemnumner="
				+ totalitemnumner + "]";
	}
	
}

2.1.2、DAO模块:jdbctools、BookDao、Bookimp

jdbctools(获取数据库连接、释放连接)
package com.xiaojie.dao;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class Jdbctools {
	private static DataSource ds=null;  
    static{  
        ds=new ComboPooledDataSource("helloc3p0");  
    }  
    public static  Connection getConnection() throws ClassNotFoundException, SQLException, IOException{  
        return ds.getConnection();      
    } 
    public static void shifanglianjie(Connection ct, PreparedStatement ps,ResultSet rs) { 
    	if(rs!=null){
    		try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	}
    	if(ps!=null){
    		try {
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	}
    	if(ct!=null){
    		try {
				ct.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	}
    }

}

BookDao(这里我们只需要两个功能:展示所有的books、查询出数据库中究竟有多少条记录)
package com.xiaojie.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.xiaojie.beans.Book;
import com.xiaojie.dao.Jdbctools;
public class BookDao {
	public List  querylist(String sql,Object...args){
		Book book=null;
		Connection ct=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		Listbooks=new ArrayList();
		try {
			ct=Jdbctools.getConnection();
			System.out.println("获取到连接了");
			ps=ct.prepareStatement(sql);
			for(int i=0;i

Bookimp(传sql 语句等参数以实现BookDao里的两大功能)
package com.xiaojie.dao;


import java.util.ArrayList;
import java.util.List;

import com.xiaojie.beans.Book;

public class Bookimp extends BookDao{
	public Listgetbooks(int pageno,int pagesize){
		String sql="select * from book limit ?,?";
		return querylist(sql, (pageno-1)*5,5);
		
	}
	public long getTotalBookNumber() {
		String sql="select count(id) from book ";
		return getForValue(sql);
	}
}

2.1.3、service模块:BookService(业务部分)

BookService的作用是:用页码(pageno)、当前要展示的书籍(books)、总共有多少本书(totalitemnumner)封装成一个page对象。即这个page对象要携带它本该具备的所有属性的值。这样的目的就是为了,便于将来servlet调用后展示在前台页面。
package com.xiaojie.service;

import com.xiaojie.beans.Page;
import com.xiaojie.dao.Bookimp;

public class BookService {
	private Bookimp bookimp=new Bookimp();
	
	public Page getpage(int pageno){
		Page page=new Page(pageno);
		page.setBooks(bookimp.getbooks(pageno, 5));
		page.setTotalitemnumner(bookimp.getTotalBookNumber());
		System.out.println(page+"service");
		return page;
		
	}
}

2.1.4、servlet模块:PageServlet(控制器部分)

package com.xiaojie.servlet;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

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

import com.xiaojie.beans.Book;
import com.xiaojie.beans.Page;
import com.xiaojie.service.BookService;

/**
 * Servlet implementation class PageServlet
 */
@WebServlet("/pageservlet")
public class PageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    String methodname=request.getParameter("method");  
	    try {  
	        Method method=getClass().getDeclaredMethod(methodname,HttpServletRequest.class,HttpServletResponse.class);  
	        method.invoke(this, request,response);//调用各自的方法  
	    } catch (Exception e) {  
	        e.printStackTrace();  
	    }  
	}
	private BookService bookservice=new BookService();
	protected void getpage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("getpage进来了");
		String pagenoStr=request.getParameter("pageno");
		int pageno=1;
		try {
			pageno=Integer.parseInt(pagenoStr);
		} catch (Exception e) {
			// TODO: handle exception
		}
		Page page=new Page(pageno);
		page=bookservice.getpage(pageno);
		request.setAttribute("bookpage", page);
		request.getRequestDispatcher("/WEB-INF/pages/page.jsp").forward(request, response);
	}
}

2.2、前台:

index.jsp(启动控制器servlet)
<% response.sendRedirect(request.getContextPath()+"/pageservlet?method=getpage");%>
page.jsp(展示分页效果的页面)
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>




MySQL实现分页的web小项目




	
	
以下是一个分页展示
书名 作者 单价
${book.name } ${book.author } ${book.price }

共${bookpage.totalpagenumber }页    当前第${bookpage.pageno }页    首页    上一页 下一页    末页    转到

3、效果图



4、项目目录结构


如有疑问请联系本人qq:1913284695

或者微信号:fyydbc








你可能感兴趣的:(小项目总结)