1.了解分页原因 和分页技术的好处有哪些:
准确来说分页一般都是数据库分页
1.分页技术是把数据全部查询出来,然后再进行分页
2.分页技术可以,降低带宽使用,提高访问速度
分页的原因如下:
1、提高性能,一次查20个,比一次查20000个性能肯定更好;另外如果数据量很大,一次性将内容都查询出来,查询出来的结果是放在内存里面的,内存没有这么大
2、不需要这么多数据,如新闻,一般人可能只看最近前20条;如果我们将后面的也都查询出来了,就是浪费
3、展现层面的考虑:如果一次展现太多的数据,不管是排版,还是美观上都不好
4.查询效率快,因为只显示每页的条数,而不是把所有的数据加载出来,另外页面好看,比如几十万的数据,你不分页会把页面撑爆
分页三要素:
page 页码 视图层传递过来
rows 页大小 视图层传递过来
total 总记录数 后台查出来
pagination 是否分页 视图层传递过来
通用分页的核心思想
将上一次查询请求再发一次,改变页码
在写分页之前我们需要一个连接数据库的类
/**
* 提供了一组获得或关闭数据库对象的方法
*
*/
public class DBAccess {
private static String driver;
private static String url;
private static String user;
private static String password;
static {// 静态块执行一次,加载 驱动一次
try {
//读取流
InputStream is = DBAccess.class.getResourceAsStream("config.properties");//这是一个config.properties的类里面定义了帮助类的数据库名 密码和 用户名
//封装类
Properties properties = new Properties();
//加载
properties.load(is);
//获得driver
driver = properties.getProperty("driver");
url = properties.getProperty("url");
user = properties.getProperty("user");
password = properties.getProperty("pwd");
Class.forName(driver);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
* 获得数据连接对象
*
* @return
*/
//加载驱动
public static Connection getConnection() {
try {
Connection con = DriverManager.getConnection(url, user, password);
return con;
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
//关闭连接
public static void close(ResultSet rs) {
if (null != rs) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
public static void close(PreparedStatement pst) {
if (null != pst) {
try {
pst.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
public static void close(Connection con) {
if (null != con) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
public static void close(Connection con, PreparedStatement pst, ResultSet rs) {
close(rs);
close(pst);
close(con);
}
//连接数据库Oracle
public