个人认为开发WEB分页程序比较困难,主要是因为思路不够清晰,故结合自身实践,总结出开发分页程序的步骤。
步骤一:封装实体类 PageBean, 封装下列数据: 当前页 currentPage、 当前页记录个数 currentCount、 总页数 totalPage、 记录总个数 totalCount
、当前页的数据(多条记录放入集合中)
import java.util.List;
public class PageBean {
private int currentPage; //当前页
private int currentCount; //当前页显示条数
private int totalCount; //总条数
private int totalPage; //总页数
private List list; //当前页的数据(集合)
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getCurrentCount() {
return currentCount;
}
public void setCurrentCount(int currentCount) {
this.currentCount = currentCount;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
@Override
public String toString() {
return "PageBean [currentPage=" + currentPage + ", currentCount=" + currentCount + ", totalCount=" + totalCount
+ ", totalPage=" + totalPage + ", list=" + list + "]";
}
}
步骤二:完成表示层,表示层有两个:1、向Servlet传递参数(当前页currentPage,分类显示数据(以分类显示不同商品为例),则应该向Servlet传递商品id)。2、接收Servlet返回的数据,即PageBean中封装的数据,用以显示页面。
表示层分页部分的代码:
步骤三:完成控制层,即servlet部分的代码。控制层的任务是:获得 pageBean, 转发到显示层。
public class ProductListByCidServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/**
* 分页:控制层参数列表: 商品编号cid, 当前页currentPage, 每页的信息个数currentCount
* 任务:获得 pageBean, 转发到显示层
*/
String cid = request.getParameter("cid");
String currPage = request.getParameter("currentPage");
if(currPage == null)
currPage = "1";
int currentPage = Integer.parseInt(currPage); //当前页
int currentCount = 12; //当前页的条数(固定)
IProductService service = ServiceFactory.getProductServiceImpl();
PageBean pageBean = service.findProductListByCid(cid, currentPage, currentCount);
request.setAttribute("pageBean", pageBean);
request.setAttribute("cid", cid);
request.getRequestDispatcher("/product_list.jsp").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
步骤四:完成业务层,业务层的任务是封装 pageBean 实体类。
业务层接口:
public interface IProductService {
public PageBean findProductListByCid(String cid, int currentPage, int currentCount);
}
业务层实现类
public class ProductServiceImpl implements IProductService{
/**
* 业务层:封装 pageBean
* 当前页 currentPage
* 当前页记录个数 currentCount
* 总页数 totalPage
* 记录总个数 totalCount
* 当前页的数据集合 list
*/
@Override
public PageBean findProductListByCid(String cid, int currentPage, int currentCount) {
PageBean pageBean = new PageBean();
pageBean.setCurrentPage(currentPage); // 封装当前页
pageBean.setCurrentCount(currentCount); // 封装当前页条数
IProductDao dao = DaoFactory.getProductDao();
int totalCount = dao.getProductCount(cid);
pageBean.setTotalCount(totalCount); //封装总条数
int totalPage = (int) Math.ceil(1.0*totalCount/currentCount); //封装总页数
pageBean.setTotalPage(totalPage);
// 封装当前页的数据
int index = (currentPage - 1) * currentCount; //计算起始索引
List list = dao.findProductByPage(cid, index, currentCount); //商品种类编号 起始索引 当前页显示条数
pageBean.setList(list);
return pageBean;
}
}
步骤五:实现数据层,查询数据库。
数据层接口:
public interface IProductDao {
public int getProductCount(String cid);
public List findProductByPage(String cid, int index, int currentCount);
}
数据层实现类:
public class ProductDaoImpl implements IProductDao{
@Override
public int getProductCount(String cid) {
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select count(*) from Product where cid=?";
Long count = 0L;
try {
count = (Long) runner.query(sql, new ScalarHandler(), cid);
} catch (SQLException e) {
e.printStackTrace();
}
return count.intValue();
}
@Override
public List findProductByPage(String cid, int index, int currentCount) {
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from product where cid=? limit ?,?";
List list = null;
try {
list = runner.query(sql, new BeanListHandler(Product.class), cid, index, currentCount);
} catch (SQLException e) {
e.printStackTrace();
}
// for(Product pro : list) {
// System.out.println(pro.toString());
// }
return list;
}
}
步骤六:相关工具类。
自定义通用工具类:
public class CommonsUtils {
// 生成uuid的方法
public static String getUUID() {
return UUID.randomUUID().toString();
}
public static void stringToDate() {
// 自己指定一个类型转换器(将String转成Date) //了解即可
ConvertUtils.register(new Converter() {
@Override
public Object convert(Class clazz, Object value) {
// 将string转成 date
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date parse = null;
try {
parse = format.parse(value.toString());
} catch (ParseException e) {
e.printStackTrace();
}
return parse;
}
}, Date.class);
}
}
工厂类:
public class DaoFactory {
public static IProductDao getProductDao() {
return new ProductDaoImpl();
}
}
工厂类:
public class ServiceFactory {
public static IProductService getProductServiceImpl() {
return new ProductServiceImpl();
}
}
连接池工具类:
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class DataSourceUtils {
private static DataSource dataSource = new ComboPooledDataSource();
private static ThreadLocal tl = new ThreadLocal();
// 直接可以获取一个连接池
public static DataSource getDataSource() {
return dataSource;
}
// 获取连接对象
public static Connection getConnection() throws SQLException {
Connection con = tl.get();
if (con == null) {
con = dataSource.getConnection();
tl.set(con);
}
return con;
}
// 开启事务
public static void startTransaction() throws SQLException {
Connection con = getConnection();
if (con != null) {
con.setAutoCommit(false);
}
}
// 事务回滚
public static void rollback() throws SQLException {
Connection con = getConnection();
if (con != null) {
con.rollback();
}
}
// 提交并且 关闭资源及从ThreadLocall中释放
public static void commitAndRelease() throws SQLException {
Connection con = getConnection();
if (con != null) {
con.commit(); // 事务提交
con.close();// 关闭资源
tl.remove();// 从线程绑定中移除
}
}
// 关闭资源方法
public static void closeConnection() throws SQLException {
Connection con = getConnection();
if (con != null) {
con.close();
}
}
public static void closeStatement(Statement st) throws SQLException {
if (st != null) {
st.close();
}
}
public static void closeResultSet(ResultSet rs) throws SQLException {
if (rs != null) {
rs.close();
}
}
}
注:以上代码由项目中截取,运行代码需要导入相应的外部架包。