分页的作用(优势)
1.分页技术可以,降低带宽使用,提高访问速度
2.增加访客浏览的路径,在访客翻页的过程中,不可避免的会注意到网站的广告,这样就提升了点击的几率。
3.影响用户的体验,对于如今的“快餐时代”,很多访客是不愿意也没有耐心去点击下一页按钮,而是直接选择跳出页面,不再浏览。
PageBean
分页三要素:
page 页码 视图层传递过来
rows 页大小 视图层传递过来
total 总记录数 后台查出来
pagination 是否分页 视图层传递过来
package com.ylt.util;
/**
* 分页工具类
*/
public class PageBean {
private int page = 1;// 页码
private int rows = 10;// 页大小
private int total = 0;// 总记录数
private boolean pagination = true;// 是否分页
public PageBean() {
super();
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getRows() {
return rows;
}
public void setRows(int rows) {
this.rows = rows;
}
public int getTotal() {
return total;
}
public boolean isPagination() {
return pagination;
}
public void setPagination(boolean pagination) {
this.pagination = pagination;
}
public void setTotal(int total) {
this.total = total;
}
public void setTotal(String total) {
this.total = Integer.parseInt(total);
}
/**
* 获得起始记录的下标
* @return
*/
public int getStartIndex() {
return (this.page - 1) * this.rows;
}
@Override
public String toString() {
return "PageBean [page=" + page + ", rows=" + rows + ", total=" + total + ", pagination=" + 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");
Properties properties = new Properties();
properties.load(is);
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 conn = DriverManager.getConnection(url, user, password);
return conn;
} 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(Connection conn) {
if (null != conn) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
public static void close(Statement stmt) {
if (null != stmt) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
public static void close(Connection conn, Statement stmt, ResultSet rs) {
close(rs);
close(stmt);
close(conn);
}
public static boolean isOracle() {
return "oracle.jdbc.driver.OracleDriver".equals(driver);
}
public static boolean isSQLServer() {
return "com.microsoft.sqlserver.jdbc.SQLServerDriver".equals(driver);
}
public static boolean isMysql() {
return "com.mysql.jdbc.Driver".equals(driver);
}
public static void main(String[] args) {
Connection conn = DBAccess.getConnection();
DBAccess.close(conn);
System.out.println("isOracle:" + isOracle());
System.out.println("isSQLServer:" + isSQLServer());
System.out.println("isMysql:" + isMysql());
System.out.println("数据库连接(关闭)成功");
}
}
所有类都可以用的一个父类来进行操作所以再写一个BaseDao:
public class BaseDao<T> {
/**
*
* @param sql 决定查询哪个表的数据
* @param clz 查询出来的数据封装到哪个实体类中
* @param p 决定是否分页
* @return
* @throws SQLException
* @throws InstantiationException
* @throws IllegalAccessException
*/
public List<T> executeQuery(String sql,Class clz,PageBean p) throws SQLException, InstantiationException, IllegalAccessException{
List<T> list = new ArrayList<>();
Connection con = DBAccess.getConnection();
PreparedStatement pst = null;
ResultSet rs = null;
try {
//判断是否进行分页
if (p != null && p.isPagination()) {
String countsql=getCountSql(sql);
pst = con.prepareStatement(countsql);
rs = pst.executeQuery();
if(rs.next()) {
p.setTotal(rs.getLong(1)+"");
}
//查询数据
String pageSql=getPageSql(sql,p);
pst = con.prepareStatement(pageSql);
rs = pst.executeQuery();
} else {
//查询所有并全部展示,不分页
pst = con.prepareStatement(sql);
rs = pst.executeQuery();
}
while (rs.next()) {
T t = (T) clz.newInstance();
Field[] fields = clz.getDeclaredFields();
for (Field f : fields) {
f.setAccessible(true);
f.set(t, rs.getObject(f.getName()));
}
list.add(t);
}
} finally {
DBAccess.close(con, pst, rs);
}
return list;
}
/**
* 将原生sql拼接出符合条件的某一页的数据查询sql
* @return
*/
private String getPageSql(String sql,PageBean p) {
// TODO Auto-generated method stub
return sql+" limit " + p.getStartIndex()+","+p.getRows();
}
/**
* 用原生sql拼接出查询符合条件的记录数
* @return
*/
private String getCountSql(String sql) {
return "select count(1) from ("+sql+") t";
}
}
再实例化一个book:
public class Book {
private int bid;
private String bname;
private float price;
public int getBid() {
return bid;
}
public void setBid(int bid) {
this.bid = bid;
}
public String getBname() {
return bname;
}
public void setBname(String bname) {
this.bname = bname;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public Book() {}
public Book(int bid, String bname, float price) {
this.bid = bid;
this.bname = bname;
this.price = price;
}
@Override
public String toString() {
return "Book [bid=" + bid + ", bname=" + bname + ", price=" + price + "]";
}
}
再使BookDao继承BaseDao:
- 操作数据库中的t_mvc_book表
- 1.加载驱动
- 2.建立连接
- 3.获取预定义处理对象
- 4.执行sql语句
- 5.处理结果集
- 6.关闭连接
public List<Book> Ls(Book b,PageBean pageBean) throws SQLException, InstantiationException, IllegalAccessException{
List<Book> ls= new ArrayList<>();
String sql = "select * from t_mvc_book where 1=1";
if(StringUtils.isNotBlank(b.getBname()) && !"null".equals(b.getBname())) {
sql +=" and bname like'%"+b.getBname()+"%'";
}
return super.executeQuery(sql, Book.class, pageBean);
}
最后则在main方法里执行从而得出分页结论。
总结:
1、将原有的查询向上抽取
2、让返回值变成泛型
3、使用回调函数处理resultset
4、利用反射处理回调函数
5、获取总记录数(页面展示,计算总页数)
6、拼接分页sql语句,获取对应的结果集