一,创建项目
二,在pom.xml文件中导入依赖
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
在web.xml文件中创建servlet映射
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>bill</servlet-name>
<servlet-class>com.yhzz.servlet.BillServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>bill</servlet-name>
<url-pattern>/bill</url-pattern>
</servlet-mapping>
<!-- 数据源 -->
<resource-ref>
<res-ref-name>jdbc/Smbms_bill</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<servlet>
<servlet-name>upload</servlet-name>
<servlet-class>com.yhzz.servlet.UploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>upload</servlet-name>
<url-pattern>/upload</url-pattern>
</servlet-mapping>
</web-app>
三,搭建架构
四,在util包下创建类获取连接数据库,方法学成static,方便调用
package com.yhzz.util;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import javax.xml.transform.Result;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class BaseDaoUtil {
/**
* 获取数据库连接
*/
public static Connection getConn(){
try {
Context context = new InitialContext();
DataSource dataSource = (DataSource) context.lookup("java:comp/env/jdbc/Smbms_bill");
return dataSource.getConnection();
} catch (NamingException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return null;
}
/**
* 释放资源
* @param connection
* @param statement
* @param resultSet
*/
public static void closeAll(Connection connection, PreparedStatement statement, ResultSet resultSet){
try {
if (resultSet!=null){
resultSet.close();
}
if (statement!=null){
statement.close();
}
if (connection!=null){
connection.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
五,在dao层中创建一个impl包下创建一个类,dao层下创建一个接口,代码如下:
package com.yhzz.dao;
import com.yhzz.pojo.Bill;
import java.util.List;
public interface BillDao {
int queryBillCount(); //获取总条数
/**
* 起始索引=(当前页码数-1)*页面容量
* @return
*/
List<Bill> queryBillinit(int currentPageNo, int pageSize);
}
用类实现接口
package com.yhzz.dao.impl;
import com.yhzz.dao.BillDao;
import com.yhzz.pojo.Bill;
import com.yhzz.util.BaseDaoUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class BillDaoImpl implements BillDao {
@Override
public int queryBillCount() {
//建立连接
Connection conn = BaseDaoUtil.getConn();
PreparedStatement statement = null;
ResultSet resultSet = null;
int count = 0;
//获取statement对象
try {
statement = conn.prepareStatement("select count(1) from smbms_bill");
resultSet = statement.executeQuery();
while (resultSet.next()){
count = resultSet.getInt(1);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
BaseDaoUtil.closeAll(conn,statement,resultSet);
}
return count;
}
@Override
public List<Bill> queryBillinit(int currentPageNo, int pageSize) {
//获取连接
Connection conn = BaseDaoUtil.getConn();
PreparedStatement statement = null;
ResultSet resultSet = null;
String sql = "select * from smbms_bill limit ?,?";
try {
statement = conn.prepareStatement(sql);
statement.setInt(1,(currentPageNo-1)*pageSize);
statement.setInt(2,pageSize);
resultSet = statement.executeQuery();
List<Bill> billList = new ArrayList<>();
while (resultSet.next()){
Bill bill = new Bill();
bill.setId(resultSet.getInt("id"));
bill.setProductName(resultSet.getString("productName"));
bill.setTotalPrice(resultSet.getDouble("totalPrice"));
bill.setProductUnit(resultSet.getString("productUnit"));
billList.add(bill);
}
return billList;
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
BaseDaoUtil.closeAll(conn,statement,resultSet);
}
return null;
}
}
六,在service层中创建类和接口
package com.yhzz.service;
import com.yhzz.pojo.Bill;
import java.util.List;
public interface BillService {
int queryBillCount(); //获取总条数
/**
* 起始索引=(当前页码数-1)*页面容量
* @return
*/
List<Bill> queryBillinit(int currentPageNo, int pageSize);
}
用类实现接口,返回dao层接口的方法
package com.yhzz.service.impl;
import com.yhzz.dao.BillDao;
import com.yhzz.dao.impl.BillDaoImpl;
import com.yhzz.pojo.Bill;
import com.yhzz.service.BillService;
import java.util.List;
public class BillServiceImpl implements BillService {
BillDao billDao = new BillDaoImpl();
@Override
public int queryBillCount() {
return billDao.queryBillCount();
}
@Override
public List<Bill> queryBillinit(int currentPageNo, int pageSize) {
return billDao.queryBillinit(currentPageNo,pageSize);
}
}
七,在pojo中创建实体类,实现封装,创建get/set方法用于存取值
package com.yhzz.pojo;
public class Bill {
private int id;
private String productName ; //货物名称
private String productUnit; //单位
private double totalPrice; //总价
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getProductUnit() {
return productUnit;
}
public void setProductUnit(String productUnit) {
this.productUnit = productUnit;
}
public double getTotalPrice() {
return totalPrice;
}
public void setTotalPrice(double totalPrice) {
this.totalPrice = totalPrice;
}
public Bill(int id, String productName, String productUnit, double totalPrice) {
this.id = id;
this.productName = productName;
this.productUnit = productUnit;
this.totalPrice = totalPrice;
}
public Bill() {
}
}
创建分页实体类
package com.yhzz.pojo;
public class PageObj {
//页面容量 pageSize
private int pageSize = 3;
//总条数 totalCount
private int totalCount = 0;
//总页数 totalPageCount
private int totalPageCount = 0;
//当前页码数:currentPageNo
private int currentPageNo = 1;
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
if (pageSize>0){
this.pageSize = pageSize;
}
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
this.totalPageCount = totalCount%pageSize==0?totalCount/pageSize:totalCount/pageSize+1;
}
public int getTotalPageCount() {
return totalPageCount;
}
public int getCurrentPageNo() {
return currentPageNo;
}
public void setCurrentPageNo(int currentPageNo) {
if (currentPageNo>0){
this.currentPageNo = currentPageNo;
}
}
}
八,使用servlet中创建类实现分页
package com.yhzz.servlet;
import com.yhzz.pojo.Bill;
import com.yhzz.pojo.PageObj;
import com.yhzz.service.BillService;
import com.yhzz.service.impl.BillServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
public class BillServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String currentPageNo = req.getParameter("currentPageNo");
int currPageNo = 1;
if (currentPageNo!=null && !"".equals(currentPageNo)){ //jsp传来的参数判断非空
currPageNo = Integer.parseInt(currentPageNo);
}
BillService billService = new BillServiceImpl();
//获取数据总数
int count = billService.queryBillCount();
PageObj pageObj = new PageObj();
pageObj.setPageSize(5);
pageObj.setTotalCount(count);
pageObj.setCurrentPageNo(currPageNo);
List <Bill> billList = billService.queryBillinit(pageObj.getCurrentPageNo(),pageObj.getPageSize());
req.setAttribute("billList",billList);
//把当前页面数传给jsp
req.setAttribute("currentPageNo",pageObj.getCurrentPageNo());
//把总数传给jsp
req.setAttribute("totalPageCount",pageObj.getTotalPageCount());
req.getRequestDispatcher("billList.jsp").forward(req,resp);
}
}
九,创建jsp,创建欢迎页
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<body>
<p>
<a href="${pageContext.request.contextPath}/bill">查看商品列表</a>
</p>
</body>
</html>
在页面中实现点击分页效果
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<div>
<table border="1" style="border: solid black">
<tr>
<td>商品编号</td>
<td>商品名称</td>
<td>商品单位</td>
<td>总价</td>
</tr>
<c:forEach items="${billList}" var="bill">
<tr>
<td>${bill.id}</td>
<td>${bill.productName}</td>
<td>${bill.productUnit}</td>
<td>${bill.totalPrice}</td>
</tr>
</c:forEach>
</table>
</div>
<div>
<a href="${pageContext.request.contextPath}/bill?currentPageNo=1">首页</a>
<c:if test="${currentPageNo<=1}">
<a href="${pageContext.request.contextPath}/bill?currentPageNo=1">上一页</a>
</c:if>
<c:if test="${currentPageNo>1}">
<a href="${pageContext.request.contextPath}/bill?currentPageNo=${currentPageNo-1}">上一页</a>
</c:if>
<c:if test="${currentPageNo>=totalPageCount}">
<a href="${pageContext.request.contextPath}/bill?currentPageNo=${totalPageCount}">下一页</a>
</c:if>
<c:if test="${currentPageNo<totalPageCount}">
<a href="${pageContext.request.contextPath}/bill?currentPageNo=${currentPageNo+1}">下一页</a>
</c:if>
<a href="${pageContext.request.contextPath}/bill?currentPageNo=${totalPageCount}">尾页</a>
</div>
</body>
</html>
十,配置jndi和tomcat实现运行
在tomcat包中找到conf下的
配置
这里的和上面web.xml文件中配置的要保持一致