网站建设知识
MySQL---数据库从入门走向大神系列(十六)-JavaWeb分页技术实例演示1
2025-07-22 10:00  点击:0

分页,是一种将所有数据分段展示给用户的技术.用户每次看到的不 是全部数据,而是其中的一部分,如果在其中没有找到自己想要的内容,用户可以通过指定页码或是点上/下一页的方式进行翻页。

本例演示静态分页,也就是先设置好每页显示10行,再根据总行数,来算出总页数,将所有页数的页号都显示出来。

相关算法(技术):

总行数(num): select count(1) from stud;

每页显示的行数(n): 固定值---已知的一个常量

页数: pageSize= num/n +( (num%n==0)?0:1 )

当前页号: currentPage

当前要显示的页面数据的起始行号和终止行号

startN: (currentPage-1)*pageSize

如何显示从startN开始的pageSize条记录

select * from stud limit startN, pageSize;

像这样:

点击哪一页就显示哪一页的内容。

数据库数据:

数据库的表和数据在这一篇博客中已经准备好了:

blog.csdn.net/qq_26525215/article/details/52212571

create table person(

id varchar(30) primary key,

name varchar(30),

address varchar(30),

age int

);

DAO层:

接口:

package cn.hncu.dao;

import java.sql.SQLException;

import java.util.Map;

public interface IPageDAO {

public Map query(Integer pageNo) throws NumberFormatException, SQLException;

}

实现类:

package cn.hncu.dao;

import java.sql.SQLException;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import org.apachemons.dbutils.QueryRunner;

import org.apachemons.dbutils.handlers.MapListHandler;

import org.apachemons.dbutils.handlers.ScalarHandler;

import org.junit.Test;

import cn.hncu.pubs.C3p0Pool;

public class PageJdbc implements IPageDAO {

private final int pageSize = 10;

@Override

public Map query(Integer pageNo) throws NumberFormatException, SQLException {

Map result = new HashMap();

String sql = "select count(1) from person";

QueryRunner run = new QueryRunner(C3p0Pool.getDataSource());

int rows =Integer.parseInt(""+run.query(sql, new ScalarHandler()));

int pageCount = rows/pageSize + ((rows%pageSize==0)?0:1);

result.put("pageCount", pageCount);

int startN = (pageNo-1)*pageSize;

sql = "select * from person limit "+startN+" , "+pageSize;

List> datas = run.query(sql, new MapListHandler());

result.put("datas", datas);//封装到result

return result;

}

@Test

public void test() {

try {

Map map = query(5);

System.out.println(map);

} catch (NumberFormatException e) {

e.printStackTrace();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

C3p0配置文件c3p0-config.xml:

com.mysql.jdbc.Driver

root

1234

2

30

10

2

50

com.mysql.jdbc.Driver

root

1234

5

100

50

1000

0

5

C3p0数据库连接池:

package cn.hncu.pubs;

import java.sql.Connection;

import java.sql.SQLException;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class C3p0Pool {

private static DataSource pool;

private static ThreadLocal t = new ThreadLocal();

static{

pool = new ComboPooledDataSource();

}

public static DataSource getDataSource(){

return pool;

}

public static Connection getConnection() throws SQLException{

Connection con=t.get();

if(con==null){

con = pool.getConnection();

t.set(con);

}

return con;

}

}

service层:

接口:

package cn.hncu.service;

import java.sql.SQLException;

import java.util.Map;

public interface IPageService {

public Map query(Integer pageNo) throws NumberFormatException, SQLException;

}

实现类

package cn.hncu.service;

import java.sql.SQLException;

import java.util.Map;

import cn.hncu.dao.IPageDAO;

import cn.hncu.dao.PageJdbc;

public class PageServiceImpl implements IPageService{

IPageDAO dao = new PageJdbc();

@Override

public Map query(Integer pageNo) throws NumberFormatException, SQLException {

return dao.query(pageNo);

}

}

index.jsp: