基于简单MVC模式实现从数据库获取数据并分页实现

发布时间:

MVC模式实现对数据库查询的数据分页浏览

先展示一下最后的效果(至少实现功能,并没有去优化)

分页实现

使用的是MYSQL数据库
基本的文件结构如下图

文件结构

基于简单MVC模式实现从数据库获取数据并分页实现

在使用idea创建基本的web框架之后,在默认的index.jsp中添加一段跳转代码

<meta http-equiv="refresh" content="0;url=showServlet"/>

使其跳转至showServlet

showservlet传递数据

package com;

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 java.io.IOException;
import java.sql.*;

//index.jsp跳转位置
@WebServlet("/showServlet")
public class showServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ShowBean showBean = new ShowBean();
//定义一个初始页面位置,初始为空
String pageNo=request.getParameter("pageNo");
//定义一个初始页面记录条数,初始为空
String tiaoshu=request.getParameter("tiaoshu");

//如果页面位置为空则将其设置为首页
 if(pageNo==null||pageNo=="")
pageNo="1";
//如果初始页面记录条数为空则将其设置为2
if(tiaoshu==null||tiaoshu=="")
tiaoshu="2";

//queryBillPageSize方法获取总记录条数
int count = showBean.queryBillPageSize();
//ts为转int型之后的每个页面最大记录条数
int ts=Integer.parseInt(tiaoshu);

//如果总记录条数能够是ts的整数倍
if(count%ts==0){
//此时,count为总页面数
count=count/ts;
}
//否则count+1才是总页面数
else{
count=count/ts+1;
}
//判断当前页面位置是否超过总页数
if(Integer.parseInt(pageNo)>=count){
//超过总页数将其设置为最大页数
pageNo= String.valueOf(count);
}
//同理
if(Integer.parseInt(pageNo)<=1){
pageNo= "1";
}

//传输数据,调用queryBillPage方法将从数据库中获取的数据传递
//参数pageNo为当前所在页面,tiaoshu为当前页面最大记录数
request.setAttribute("YHXX", showBean.queryBillPage(pageNo,tiaoshu));
//传输数据,调用queryBillPageSize方法传递总记录条数
request.setAttribute("YHYS", showBean.queryBillPageSize());
//传递页面总数
request.setAttribute("count", count);
//传递页面最大记录条数
request.setAttribute("ts", tiaoshu);
//传递当前页面位置
request.getSession().setAttribute("pageNo", pageNo);
//跳转到login.jsp
request.getRequestDispatcher("login.jsp").forward(request,response);
}
}

在跳转至login.jsp后,login.jsp中

实现分页显示

<%@ page import="com.ShowBean" %>
<%@ page import="java.util.List" %>
<%@ page import="com.Users" %>
<%@ page import="java.util.Objects" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
//获取servlet中传输的页面记录最大条数
String ts = (String)request.getAttribute("ts");
//从servlet中获取的全部记录,存储在list中
//此时list中的数据为根据每页最大记录条数以及当前页码获取到的数据
List<Users>list= (List<Users>)request.getAttribute("YHXX");
for循环打印数据
for(Users users:list)
{
%><%=users.getUserName()%><br/><%
}
%>
//此处form表单是为了在选择每页最大条数之后,刷新页面所用
<form id="bd" name="bd">
//下拉框选择每页最大显示条数,οnchange="document.bd.submit()即可实现刷新
<select id="tiaoshu" name="tiaoshu" onchange="document.bd.submit();">
<%
//如果页面最大显示条数ts为2,设置默认选项为2
if(Objects.equals(ts, "2"))
{
%>
<option selected="selected">2</option>
<%
}
else 
{
%>
<option>2</option>
<%
}
%>
<%
//如果页面最大显示条数ts为4,设置默认选项为4
if(Objects.equals(ts, "4"))
{
%>
<option selected="selected">4</option>
<%
}
else
{
%>
<option>4</option>
<%
}
%>
<%
//如果页面最大显示条数ts为5,设置默认选项为5
if(ts.equals("5"))
{
%>
<option selected="selected">5</option>
<%
}
else 
{
%>
<option>5</option>
<%
}
%>
<%
//如果页面最大显示条数ts为100,设置默认选项为100
if(ts.equals("100"))
{
%>
<option selected="selected">100</option>
<%
}
else 
{
%>
<option>100</option>
<%
}
%>
</select>
</form>
//YHYS为servlet中获取的总记录数,pageNo为当前页号,count为总页数
共${YHYS}条记录,${pageNo}/${count}页
// /showServlet为从新转向showServlet,?pageNo=1&tiaoshu=<%=ts%>pageNo,tiaoshu为传递的两个参数
<a href="${pageContext.request.contextPath }/showServlet?pageNo=1&tiaoshu=<%=ts%>">首页</a>
<a href="${pageContext.request.contextPath }/showServlet?pageNo=${pageNo-1}&tiaoshu=<%=ts%>">上一页</a>
<a href="${pageContext.request.contextPath }/showServlet?pageNo=${pageNo+1 }&tiaoshu=<%=ts%>">下一页</a>
<a href="${pageContext.request.contextPath }/showServlet?pageNo=${count }&tiaoshu=<%=ts%>">末页</a>
</body>
</html>

到此大致的内容就结束了,剩下的即为从MySQL数据库中获取数据。

获取数据

package com;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class ShowBean {
public List<Users> queryBillPage(String pageNo,String tiaoshu) {
List<Users> list = new ArrayList<>();
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//每一页的第一条数据的序号
int pageIndex=(Integer.parseInt(pageNo) -1)*Integer.parseInt(tiaoshu);
String url="jdbc:mysql://localhost/你的数据库名称";
String uname="登录名";
String upwd="密码";
try {
Connection conn= DriverManager.getConnection(url,uname,upwd);
//该语句为从获取从pageIndex开始往后tiaoshu条数据
String sql="select * from 你要获取数据的表 limit "+pageIndex+","+tiaoshu;
PreparedStatement ps=conn.prepareStatement(sql);
ResultSet rs=ps.executeQuery();
while (rs.next()){
//System.out.println(1);
Users users = new Users();
users.setUserName(rs.getString(3));
list.add(users);
}
rs.close();
ps.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
public int queryBillPageSize() {
//flag为总记录条数
int flag=0;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// System.out.println(1);
String url="jdbc:mysql://localhost/数据库名称";
String uname="登录名";
String upwd="密码";
try {
Connection conn= DriverManager.getConnection(url,uname,upwd);
String sql="select * from 表名";
PreparedStatement ps=conn.prepareStatement(sql);
ResultSet rs=ps.executeQuery();
while (rs.next()){
flag++;
}
rs.close();
ps.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return flag;
}
}

Users类(getset方法)

package com;
public class Users {
private String userName;
public String getUserName() { return userName; }
public void setUserName(String userName) {
this.userName = userName;
}
}

到此分页功能基本实现!