日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java Web实现分页查询

發布時間:2025/5/22 java 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java Web实现分页查询 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

      • 使用工具:
      • 使用Jar包:
      • 項目下載連接:
          • 數據庫表
        • 實體類:
          • 用戶實體類:
          • 分頁實例化:
        • 在Dao接口中定義2個方法:
            • 接口:
        • 實現Dao接口方法解析:
        • service層:
            • 接口:
            • 接口實現:
        • 工具類:
        • Servlet:
        • web.xml:
        • c3p0-config.xml
        • 信息展示頁面:
        • 效果展示:

使用工具:

JavaJDK1.8
Tomcat 8.5.33
IDEA
MySQL5.6

使用Jar包:

  • c3p0-0.9.1.2.jar
  • commons-dbutils-1.4.jar
  • javax.annotation.jar
  • javax.ejb.jar
  • javax.jms.jar
  • javax.persistence.jar
  • javax.resource.jar
  • javax.servlet.jar
  • javax.servlet.jsp.jar
  • javax.transaction.jar
  • jstl-1.2.jar
  • mysql-connector-java-8.0.13.jar
  • junit-4.12.jar + hamcrest-core-1.3.jar 這個兩個缺一不可 (junit-4.8.jar以上可以代替這兩個jar包)

項目下載連接:

微云鏈接:https://share.weiyun.com/WA6AmSix

數據庫表
CREATE TABLE `user` (`id` int(11) NOT NULL AUTO_INCREMENT,`name` varchar(255) DEFAULT NULL,`age` int(11) DEFAULT NULL,PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=101 DEFAULT CHARSET=utf8;

實體類:

用戶實體類:
public class User {private Integer id;private String name;private Integer age;public User() {}public User(Integer id, String name, Integer age) {this.id = id;this.name = name;this.age = age;}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 Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}@Overridepublic String toString() {return "User{" +"id=" + id +", name='" + name + '\'' +", age=" + age +'}';} }
分頁實例化:

定義5個屬性:
Integer pageNum :用來記錄當前頁面的頁碼
Integer pageSize :可以自定義當前頁面最大顯示多少條數據
Long totalSize :記錄數據庫存儲的數據總共多少條
Integer pageCount : 將數據庫的 總數據量 除以 當前頁面最大顯示數據量 得到 數據可以分多少頁
List<T> data:存放每一頁要顯示的數據

this.pageCount = (int)(this.totalSize%this.pageSize == 0 ?this.totalSize / this.pageSize:this.totalSize/this.pageSize + 1);

當總數據量 除以 當前頁面顯示的數據量 剛好等于 0時數據頁面不再 + 1,
當總數據量 除以 當前頁面顯示的數據量 有余數時,數據頁面 + 1(額外加一頁顯示剩余的數據)。

例如:有21條數據,每頁顯示10條數據,需要3頁才能把全部的數據展示出來。
如果剛好20條數據,每頁顯示10條數據,2頁剛好展示全部的數據。

import java.util.List;/*** @Author ??VVcat??* @Date 2020/6/15 12:20 星期一* @PackageName io.vvcat.domain* @ClassName PageBean* @Email: 206647497@qq.com* @CSDN: https://blog.csdn.net/qq_44989881* @Blog: vvcat.io**/public class PageBean<T> {private Integer pageNum; // 第幾頁private Integer pageSize; // 每頁幾條數據private Long totalSize; // 總共多少條數據private Integer pageCount; // 總共能分幾頁private List<T> data; // 每頁存儲的數據public PageBean(Integer pageNum, Integer pageSize, Long totalSize, List<T> data){this.pageNum = pageNum;this.pageSize = pageSize;this.totalSize = totalSize;this.data = data;this.pageCount = (int)(this.totalSize%this.pageSize == 0 ?this.totalSize / this.pageSize:this.totalSize/this.pageSize + 1);}public Integer getPageNum() {return pageNum;}public void setPageNum(Integer pageNum) {this.pageNum = pageNum;}public Integer getPageSize() {return pageSize;}public void setPageSize(Integer pageSize) {this.pageSize = pageSize;}public Long getTotalSize() {return totalSize;}public void setTotalSize(Long totalSize) {this.totalSize = totalSize;}public Integer getPageCount() {return pageCount;}public void setPageCount(Integer pageCount) {this.pageCount = pageCount;}public List<T> getData() {return data;}public void setData(List<T> data) {this.data = data;} }@Overridepublic String toString() {return "PageBean{" +"pageNum=" + pageNum +", pageSize=" + pageSize +", totalSize=" + totalSize +", pageCount=" + pageCount +", data=" + data +'}';}

在Dao接口中定義2個方法:

接口:

findByPage方法:當前頁面的頁碼 和 頁面能顯示的最大數據量 作為參數返回通過mysql處理 顯示當前頁面對應的數據。
getCount方法:統計數據庫數據的總量。

import domain.User; import java.util.List;public interface UserDao {// 返回指定頁面的數據List<User> findByPage(Integer pagNum, Integer pageSize);// 返回總的數據量long getCount(); }

實現Dao接口方法解析:

例如:
目前數據庫數據:

調用findByPage方法

例如:
pageNum = 0; 從第幾頁開始顯示
pageSize = 10; 顯示10條數據

SELECT * FROM USER ORDER BY id // 正序排列 LIMIT 0,10 // 數據庫是從下標0 開始查找數據

參數:(pageNum-1) * pageSize
剛進入看到的頁面為第一頁,數據庫LIMIT 是下標第0頁開始查詢,所以減去1。

getCount方法

統計數據庫數據的總量。

例如:

import dao.UserDao; import domain.User; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanListHandler; import org.apache.commons.dbutils.handlers.ScalarHandler; import utils.DruidSourceUtils;import java.sql.SQLException; import java.util.List;/*** @Author ??VVcat??* @Date 2020/6/17 12:20 星期五* @PackageName io.vvcat.dao.impl* @ClassName UserDaoImpl * @Email: 206647497@qq.com* @CSDN: https://blog.csdn.net/qq_44989881* @Blog: vvcat.io**/public class UserDaoImpl implements UserDao {@Overridepublic List<User> findByPage(Integer pageNum, Integer pageSize) {QueryRunner qr = new QueryRunner(DruidSourceUtils.getDataSource());try {return qr.query("select * from user order by id limit ?,?",new BeanListHandler<User>(User.class), (pageNum-1)*pageSize, pageSize);} catch (SQLException e) {e.printStackTrace();}return null;}@Overridepublic long getCount() {QueryRunner qr = new QueryRunner(DruidSourceUtils.getDataSource());try {return qr.query("select count(*) from user", new ScalarHandler<>());} catch (SQLException e) {e.printStackTrace();}return 0;}}

service層:

接口:

PageBean findByPage(Integer pageNum, Integer pageSize)方法:
將pageNum; pageSize; totalSize; pageCount; data; 這些參數通過 PageBean 返回給jsp頁面。

public interface UserService {PageBean<User> findByPage(Integer pageNum, Integer pageSize); }
接口實現:
public class UserServiceImpl implements UserService {@Overridepublic PageBean<User> findByPage(Integer pageNum, Integer pageSize) {UserDao studentDao = new UserDaoImpl();List<User> data = studentDao.findByPage(pageNum, pageSize); // 根據條件獲取數據庫對應數據long totalSize = 0;totalSize = studentDao.getCount(); // 獲取數據庫可分多少頁數據PageBean<User> pageBean = new PageBean<>(pageNum, pageSize, totalSize, data); // 將以上2條方法封裝在一起,返回給Servlet;if (pageNum > pageBean.getPageCount()){pageBean.setPageNum(pageBean.getPageCount());}return pageBean;}

工具類:

import com.mchange.v2.c3p0.ComboPooledDataSource; import javax.sql.DataSource; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement;public class JDBCUtil {static ComboPooledDataSource dataSource = null;static{dataSource = new ComboPooledDataSource();}public static DataSource getDataSouce() {return dataSource;}/*** 獲取連接對象* @return* @throws SQLException*/public static Connection getConn() throws SQLException{return dataSource.getConnection();}/*** 釋放資源* @param conn* @param st* @param rs*/public static void release(Connection conn , Statement st , ResultSet rs){closeRs(rs);closeSt(st);closeConn(conn);}public static void release(Connection conn , Statement st){closeSt(st);closeConn(conn);}private static void closeRs(ResultSet rs){try {if(rs != null){rs.close();}} catch (SQLException e) {e.printStackTrace();}finally{rs = null;}}private static void closeSt(Statement st){try {if(st != null){st.close();}} catch (SQLException e) {e.printStackTrace();}finally{st = null;}}private static void closeConn(Connection conn){try {if(conn != null){conn.close();}} catch (SQLException e) {e.printStackTrace();}finally{conn = null;}} }

Servlet:

import io.vvcat.been.PageBean; import io.vvcat.been.User; import io.vvcat.service.UserService; import io.vvcat.service.impl.UserServiceImpl; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException;/*** @Author ??VVcat??* @Date 2020/6/19 18:02 星期五* @ProjectName PageSelect* @PackageName io.vvcat.Servlet* @ClassName ServletListUser* @Email: 206647497@qq.com* @Blog: vvcat.io* @CSDN: https://blog.csdn.net/qq_44989881* @Version 1.0**/ public class ServletListUser extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String pageNum = request.getParameter("pageNum"); // 獲取前端頁面傳過來的頁碼String pageSize = request.getParameter("pageSize"); // 獲取前端頁面顯示的數據int pageN = 0;int pageS = 0;if (pageNum==null||pageNum.trim().length()==0){ // 當第一次打開頁面時是沒有頁碼的,數據為null需要為頁碼賦值pageN = 1;}else {pageN=Integer.parseInt(pageNum); if (pageN<1){ // 如果請求的頁面小于 1 那么就將 pageN 賦值為 1 ,限制上一頁請求傳遞過來的值是一個負數pageN=1;}}if (pageSize==null||pageSize.trim().length()==0){ // 為頁面顯示的數據進行初始化pageS=10; // 如果此處修改為 8,那么每頁最多只會顯示 8 條數據,設置為 5 ,那么每頁最多只會顯示 5 條數據}else {pageS=Integer.parseInt(pageSize); }UserService userService = new UserServiceImpl(); // 調用UserService 服務 PageBean<User> pageBean = userService.findByPage(pageN, pageS); // 獲取頁面獲取的值傳遞到數據庫,獲取對應的數據。request.setAttribute("pageBean", pageBean);request.getRequestDispatcher("/user.jsp").forward(request, response); // 處理完成后返回user.jsp頁面進行數據渲染。}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request, response);} }

web.xml:

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"><display-name>user</display-name><welcome-file-list><welcome-file>index.html</welcome-file><!--<welcome-file>index.htm</welcome-file>--><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><!--<welcome-file>default.htm</welcome-file>--><welcome-file>default.jsp</welcome-file></welcome-file-list><servlet><description></description><display-name>ServletListUser</display-name><servlet-name>ServletListUser</servlet-name><servlet-class>io.vvcat.Servlet.ServletListUser</servlet-class></servlet><servlet-mapping><servlet-name>ServletListUser</servlet-name><url-pattern>/ServletListUser</url-pattern></servlet-mapping></web-app>

c3p0-config.xml

要對以下三條進行修改和確認

<property name="jdbcUrl">jdbc:mysql://localhost/studnets?serverTimezone=GMT%2B8</property> <property name="user">root</property> <property name="password">123456</property>
  • 設置 數據庫連接地址 jdbcUrl
  • 設置 數據庫的用戶名 user
  • 設置 數據庫密碼 password

注: serverTimezone=GMT%2B8 防止在使用IDEA對高版本數據庫連接時出現時區報錯問題。

<?xml version="1.0" encoding="UTF-8"?> <c3p0-config><!-- default-config 默認的配置, --><default-config><property name="driverClass">com.mysql.jdbc.Driver</property><property name="jdbcUrl">jdbc:mysql://localhost/db?serverTimezone=GMT%2B8</property><property name="user">root</property><property name="password">123456</property><property name="initialPoolSize">10</property><property name="maxIdleTime">30</property><property name="maxPoolSize">100</property><property name="minPoolSize">10</property><property name="maxStatements">200</property></default-config><!-- This app is massive! --><named-config name="oracle"> <property name="acquireIncrement">50</property><property name="initialPoolSize">100</property><property name="minPoolSize">50</property><property name="maxPoolSize">1000</property><!-- intergalactoApp adopts a different approach to configuring statement caching --><property name="maxStatements">0</property> <property name="maxStatementsPerConnection">5</property><!-- he's important, but there's only one of him --><user-overrides user="master-of-the-universe"> <property name="acquireIncrement">1</property><property name="initialPoolSize">1</property><property name="minPoolSize">1</property><property name="maxPoolSize">5</property><property name="maxStatementsPerConnection">50</property></user-overrides></named-config></c3p0-config>

信息展示頁面:

首頁:第一頁對應頁碼設置為1,每次點擊首頁,將1傳遞到后臺,進行查詢數據。
上一頁:點擊上一頁,會獲取當前頁碼 - 1,傳遞到后臺獲取,上一頁的數據。
下一頁:點擊下一頁,會獲取當前頁碼 + 1,傳遞到后臺獲取,下一頁的數據。
尾頁:將總共可分的頁數(即最后一頁),傳遞到后臺,顯示最后一頁的數據。

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>Title</title> </head> <body><table border="1" cellspacing="0" cellpadding="10" width="500" height="250" align="center"><tr><td>學號</td><td>姓名</td><td>年齡</td></tr><c:forEach items="${pageBean.data}" var="user"><tr><td>${user.id}</td><td>${user.name}</td><td>${user.age}</td></tr></c:forEach></table><div align="center"><a href="${pageContext.request.contextPath}/ServletListUser?pageNum=1&pageSize=${pageBean.pageSize}">首頁</a><a href="${pageContext.request.contextPath}/ServletListUser?pageNum=${pageBean.pageNum-1}&pageSize=${pageBean.pageSize}">上一頁</a><a href="${pageContext.request.contextPath}/ServletListUser?pageNum=${pageBean.pageNum+1}&pageSize=${pageBean.pageSize}">下一頁</a><a href="${pageContext.request.contextPath}/ServletListUser?pageNum=${pageBean.pageCount}&pageSize=${pageBean.pageSize}">尾頁</a></div> </body> </html>

效果展示:



總結

以上是生活随笔為你收集整理的Java Web实现分页查询的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 一本一本久久a久久精品综合麻豆 | 福利小视频 | 黑人精品一区二区三区不 | 色网站免费在线观看 | 精品亚洲在线 | 91精品久久久久久综合五月天 | 刘亦菲毛片一区二区三区 | 欧美视频一二三区 | 日本涩涩网站 | 国产女人视频 | 午夜影视剧场 | 日韩高清影院 | 99视频免费看 | 色欲av无码精品一区 | 欧美一级欧美三级 | 日本在线一区 | 色偷偷综合网 | 精品欧美久久 | 精品国产97 | 欧美老熟妇一区二区三区 | 国产亚洲毛片 | 国产亚洲欧美精品久久久www | 午夜电影你懂的 | 成人国产精品蜜柚视频 | 国产国语videosex另类 | 国产一区在线观看视频 | 日本色中色 | 激情欧美日韩 | wwwav在线播放 | 午夜免费福利视频 | 亚洲一区二区国产 | 中文字幕国产在线观看 | 欧美性xxxxxx | 日韩成人免费在线观看 | 综合视频一区二区 | 全黄性性激高免费视频 | 手机看片欧美日韩 | 中国国语农村大片 | 亚州视频在线 | 久久婷婷一区 | 黄色av影院 | 久久国产成人 | 韩国三级av | 久久精品69 | 成人免费看类便视频 | 与子敌伦刺激对白播放的优点 | 欧美日韩亚洲一区 | 日日干日日草 | 久久精品视频18 | 天天撸在线视频 | 香蕉视频911 | 欧美三级不卡 | av在线一区二区三区 | 理论在线视频 | 亚洲伊人久久综合 | 黄页在线播放 | 理论片午夜 | 国产精品嫩 | 欧美乱大交xxxxx潮喷 | 久久久激情视频 | 少妇真人直播免费视频 | 国产精品性爱在线 | 森林影视官网在线观看 | 中文字幕2区 | 成人视屏在线观看 | 日韩一二区 | 日韩欧美区 | 欧美区一区二 | 日剧再来一次第十集 | 性猛交富婆╳xxx乱大交麻豆 | 男人天堂综合网 | 神马午夜我不卡 | 日本女人一区二区三区 | 婷婷去俺也去 | 亚洲色偷偷综合亚洲av伊人 | 五月天黄色网 | 台湾av在线 | 99视频免费看 | 久久亚洲aⅴ无码精品 | 日本视频在线免费观看 | 福利一区二区在线 | 看黄色小视频 | 六十路息与子猛烈交尾 | 国产综合亚洲精品一区二 | 久久九九久久九九 | 人妻无码中文字幕 | 人妻精品久久久久中文字幕69 | 日本伊人影院 | 成人区人妻精品一区二区不卡视频 | 国产v片| 萌白酱在线观看 | 18男女无套免费视频 | 欧美乱码精品一区二区三区 | 日韩av免费在线播放 | 久久久久久久久艹 | 色偷偷一区二区三区 | 日本免费一二区 | 国产在线免费 | 欧美一区二区网站 |