DRP系统分页查询实现

一、UML流程分析


二、代码实现:

ServletItemServlet:

package com.bjpowernode.drp.basedata.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bjpowernode.drp.basedata.manager.ItemManager;
import com.bjpowernode.drp.basedata.manager.ItemManagerImpl;
import com.bjpowernode.drp.util.PageModel;

public class SearchItemServlet extends HttpServlet {

//	@Override
//	public void init() throws ServletException {
//		this.getServletConfig()
//	}

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
//		int pageNo = 0;
//		//第几页
//		String pageNoString = request.getParameter("pageNo");
//		if (pageNoString == null) {
//			pageNo = 1;
//		}else {
//			pageNo = Integer.parseInt(pageNoString);
//		}
		int pageNo = 1;
		//int pageSize = 2;
		
		//取得Servlet初始化参数page-size
		//int pageSize = Integer.parseInt(this.getServletConfig().getInitParameter("page-size"));
		
		//从application范围内取得page-size,application指的是ServletContext对象
		int pageSize = Integer.parseInt(this.getServletContext().getInitParameter("page-size"));
		
		String pageNoString = request.getParameter("pageNo");
		if (pageNoString != null && !"".equals(pageNoString)) {
			pageNo = Integer.parseInt(pageNoString);
		}
		//取得条件
		String itemNoOrName = request.getParameter("itemNoOrName");
		
		ItemManager itemManager = new ItemManagerImpl();
		PageModel pageModel = itemManager.findItemList(pageNo, pageSize, itemNoOrName);
		
		request.setAttribute("pageModel", pageModel);
		
		request.getRequestDispatcher("/basedata/item_maint.jsp").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
ItemManagerImpl(实现ItemManager):

public PageModel findItemList(int pageNo, int pageSize, String condation) {
		Connection conn=null;
		try{
			conn=DbUtil.getConnection();
			return itemDao.findItemList(conn,pageNo,pageSize,condation);
		}finally{
			DbUtil.close(conn);
		}
	
	}
ItemDao4OracleImple(实现ItemDao):

public PageModel findItemList(Connection conn, int pageNo, int pageSize, String condation) {
		StringBuffer sbSql = new StringBuffer();

		//第一中方法
//		sbSql.append("select a.item_no, a.item_name, a.spec, a.pattern, a.item_category_id, ")
//		     .append("b.name as item_category_name, a.item_unit_id, c.name as item_unit_name ")
//		     .append("from t_items a, t_data_dict b, t_data_dict c ")
//		     .append("where a.item_category_id=b.id and a.item_unit_id=c.id and a.item_no=?");

//        //第二中方法 		
//		sbSql.append("select a.item_no, a.item_name, a.spec, a.pattern, a.category as category_id, ")
//			 .append("(select b.name from t_data_dict b where a.category=b.id) as category_name, ")
//			 .append("a.unit as unit_id, ")
//			 .append("(select c.name from t_data_dict c where a.unit=c.id) as unit_name ")
//			 .append("from t_items a where a.item_no=?");
		
		sbSql.append("select * ")
			.append("from (")
				.append("select i.*, rownum rn from (")
				.append("select a.item_no, a.item_name, a.spec, a.pattern, a.item_category_id, ")
				.append("b.name as item_category_name, a.item_unit_id, c.name as item_unit_name ")
				.append("from t_items a, t_data_dict b, t_data_dict c ")
				.append("where a.item_category_id=b.id and a.item_unit_id=c.id  ");
				if (condation != null && !"".equals(condation)) {
					sbSql.append(" and (a.item_no like '" + condation + "%' or a.item_name like '" + condation + "%') ");
				}
				sbSql.append(" order by a.item_no")
				.append(") i where rownum<=? ")
				.append(") ")
				.append("where rn >? ");
		System.out.println("sql=" + sbSql.toString());
				
		//通常采用日志组件记录,如log4j, 级别:info,debug,error...
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		PageModel pageModel = null;
		try {
			pstmt = conn.prepareStatement(sbSql.toString());
			pstmt.setInt(1, pageNo * pageSize);
			pstmt.setInt(2, (pageNo - 1) * pageSize);
			rs = pstmt.executeQuery();
			List itemList = new ArrayList();
			while (rs.next()) {
				Item item = new Item();
				item.setItemNo(rs.getString("item_no"));
				item.setItemName(rs.getString("item_name"));
				item.setSpec(rs.getString("spec"));
				item.setPattern(rs.getString("pattern"));
				//构造ItemCategory
				ItemCategory ic = new ItemCategory();
				ic.setId(rs.getString("item_category_id"));
				ic.setName(rs.getString("item_category_name"));
				item.setItemCategory(ic);
				
				//构造ItemUnit
				ItemUnit iu = new ItemUnit();
				iu.setId(rs.getString("item_unit_id"));
				iu.setName(rs.getString("item_unit_name"));
				item.setItemUnit(iu);
				
				itemList.add(item);
			}
			pageModel = new PageModel();
			pageModel.setPageNo(pageNo);
			pageModel.setPageSize(pageSize);
			pageModel.setList(itemList);
			//根据条件取得记录数
			int totalRecords = getTotalRecords(conn, condation);
			pageModel.setTotalRecords(totalRecords);
		}catch(SQLException e) {
			e.printStackTrace();
			//记录到日志文件 error
			throw new ApplicationException("分页查询失败");
		}finally {
			DbUtil.close(rs);
			DbUtil.close(pstmt);
		}
		return pageModel;
	}

	/**
	 * 根据条件取得记录数
	 * @param conn
	 * @param queryStr
	 * @return
	 */
	private int getTotalRecords(Connection conn, String condation) 
	throws SQLException {
		String sql = "select count(*) from t_items ";
		if (condation != null && !"".equals(condation)) {
			sql+="where item_no like '" + condation + "%' or item_name like '" + condation + "%' ";
		}
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		int temp = 0;
		try {
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			rs.next();
			temp = rs.getInt(1);
		}finally {
			DbUtil.close(rs);
			DbUtil.close(pstmt);
		}
		return temp;
	}	




你可能感兴趣的:(DRP系统分页查询实现)