通用分页2

通用分页

实体类

package com.zhoujun.zj.entity;

import java.io.Serializable;
/**
 * 实体类
 * @author 周俊
 *
 */
public class Book implements Serializable {
	private static final long serialVersionUID = -1784556434207788044L;
	
	private Integer bId;
	private String bName;
	private Float bPrice;
	
	public Integer getbId() {
		return bId;
	}
	public void setbId(Integer bId) {
		this.bId = bId;
	}
	public String getbName() {
		return bName;
	}
	public void setbName(String bName) {
		this.bName = bName;
	}
	public Float getbPrice() {
		return bPrice;
	}
	public void setbPrice(Float bPrice) {
		this.bPrice = bPrice;
	}
	
	public Book() {}
	public Book(Integer bId, String bName, Float bPrice) {
		this.bId = bId;
		this.bName = bName;
		this.bPrice = bPrice;
	}
	
	@Override
	public String toString() {
		return "Book [bId=" + bId + ", bName=" + bName + ", bPrice=" + bPrice + "]";
	}
	
}

dao方法

package com.zhoujun.zj.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zhoujun.zj.base.BaseDao;
import com.zhoujun.zj.entity.Book;
import com.zhoujun.zj.util.DBHelper;
import com.zhoujun.zj.util.PageBean;
import com.zhoujun.zj.util.StringUtils;
/**
 * 数据访问层实现类
 * @author 周俊
 *
 */


//增加
public class BookDao extends BaseDao<Book> implements IBookDao {
	@Override
	public void add(Book book) {
		// TODO Auto-generated method stub
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "INSERT INTO tb_book(b_name,b_price) VALUES(?,?)";
		con = DBHelper.getConnection();
		try {
			ps = con.prepareStatement(sql);
			ps.setString(1, book.getbName());
			ps.setFloat(2, book.getbPrice());
			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		} finally{
			DBHelper.close(con, ps, rs);
		}
	}
	/**
	 *  不分页查询
	 */
	@Override
	public List<Book> Search(Book book) {
		// TODO Auto-generated method stub
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "SELECT b_id,b_name,b_price FROM tb_book WHERE 1=1";
		if(StringUtils.isNotBlank(book.getbName())) {
			sql += " AND b_name LIKE '%"+book.getbName()+"%'";
		}
		List<Book> bookList = new ArrayList<Book>();
		con = DBHelper.getConnection();
		try {
			ps = con.prepareStatement(sql);
			rs = ps.executeQuery();
			Book z = null;
			while(rs.next()) {
				z = new Book();
				z.setbId(rs.getInt("b_id"));
				z.setbName(rs.getString("b_name"));
				z.setbPrice(rs.getFloat("b_price"));
				bookList.add(z);
			}
			return bookList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		} finally {
			DBHelper.close(con, ps, rs);
		}
	}
	/**
	 * 分页查询
	 */
	@Override
	public List<Book> Search(Book book, PageBean pageBean) {
		// TODO Auto-generated method stub
		String sql = "SELECT b_id,b_name,b_price FROM tb_book WHERE 1=1";
		if(StringUtils.isNotBlank(book.getbName())) {
			sql += " AND b_name LIKE '%"+book.getbName()+"%'";
		}
		return this.executeQuery(sql, pageBean, new CallBack<Book>() {
			@Override
			public List<Book> forEachRs(ResultSet rs) throws SQLException {
				// TODO Auto-generated method stub
				List<Book> bookList = new ArrayList<Book>();
				Book z = null;
				while(rs.next()) {
					z = new Book();
					z.setbId(rs.getInt("b_id"));
					z.setbName(rs.getString("b_name"));
					z.setbPrice(rs.getFloat("b_price"));
					bookList.add(z);
				}
				return bookList;
			}
		}
		);
	}
	
}

与数据库连接

这里是引用

定义tid文件


<tag>
		<!-- 标签名 -->
		<name>page</name>
		<!-- 标签工具类 -->
		<tag-class>com.cengke.paging.tag.PageTag</tag-class>
		<!-- 标签的内容类型:empty表示空标签,jsp表示可以为任何合法的JSP元素 -->
		<body-content>empty</body-content>
		<!-- 自定义标签的属性定义,请注意一定要在标签类中提供对应的get/set方法 -->
		<attribute>
			<!-- 自定义标签的属性名称 -->
			<name>pageBean</name>
			<!-- true表示必填 -->
			<required>true</required>
			<!-- true支持动态值,可以向值里面填jsp表达式、EL表达式,false则不支持 -->
			<rtexprvalue>true</rtexprvalue>
		</attribute>
	</tag>

jsp界面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="zz" uri="/zhoujun" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>书店首页</title>
</head>
<body>
	<h1>首页</h1>
	<form action="search.action" method="post">
		<span>姓名:</span>
		<input name="key" />
		<input type="submit" value="查询" />
	</form>
	<table border="1px" width="100%">
		<tr>
			<td>序号</td>
			<td>书名</td>
			<td>价格</td>
		</tr>
	<c:forEach items="${bookList }" var="book" varStatus="vs">
			<tr>
				<td>${vs.index + 1 }</td>
				<td>${book.bName }</td>
				<td>${book.bPrice }</td>
				<td>${book.bPrice }</td>
			</tr>
		</c:forEach> 
	</table>
	<!-- 使用自定义标签 -->
 <zz:page pageBean="${pageBean }"/> 
</body>
</html>

核心代码

package com.zhoujun.zj.base;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.zhoujun.zj.util.DBHelper;
import com.zhoujun.zj.util.PageBean;

public class BaseDao<C> {
	/**
	 *  内部接口,用来遍历rs结果集
	 * @author 周俊
	 *
	 * @param 
	 */
	public static interface CallBack<K>{
		public List<K> forEachRs(ResultSet rs) throws SQLException;
	}
	
	@SuppressWarnings("unchecked")
	public List<C> executeQuery(String sql, PageBean pageBean, @SuppressWarnings("rawtypes") CallBack callBack){
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		con = DBHelper.getConnection();
		try {
			/**
			 * 查询符合条件的总记录数
			 */
			//判断是否分页
			if(null != pageBean && pageBean.isPagination()) {
				String countSql = this.getCountSql(sql);
				ps = con.prepareStatement(countSql);
				rs = ps.executeQuery();
				if(rs.next()) {
					Object obj = rs.getInt(1);
					pageBean.setTotal(obj.toString());
				}
				DBHelper.close(null, ps, rs);
			}
			/**
			 * 查询符合条件的记录
			 */
			//判断是否分页
			if(null != pageBean && pageBean.isPagination()) {
				sql = this.getPageSql(sql, pageBean);
			}
			ps = con.prepareStatement(sql);
			rs = ps.executeQuery();
			return callBack.forEachRs(rs);
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			DBHelper.close(con, ps, rs);
		}
	}
	/**
	 * 得到查询总记录数的sql语句
	 * @param sql
	 * @return
	 */
	private String getCountSql(String sql) {
		String countSql = "SELECT COUNT(*) FROM (" + sql + ")t1";
		return countSql;
	}
	/**
	 * 得到查询满足条件的记录的sql
	 * @param sql
	 * @param pageBean
	 * @return
	 */
	private String getPageSql(String sql, PageBean pageBean) {
		String pageSql = sql + " LIMIT " + pageBean.getStartIndex() + "," +pageBean.getRows();
		return pageSql;
	}
	
}

总结:下次见

你可能感兴趣的:(通用分页2)