自己写的分页查询的步骤代码

第一步:前台页面(核心代码)

 主要是当前的页数,因为是第一次嘛,肯定是从第一页开始的。查询出来的页面我用的是iframe方式呈现。其他地方的代码不用啰嗦,该怎么写怎么写。

<form name="advertiseForm" id="advertiseForm" action="modules/advertisement/AdvertiseManageAction.do?action=searchAdvertise" method="POST" target="advertiseList">
    <input type="hidden" name="currentPage" id="currentPage" value="1" />
<!-- 此处省略 -->
 <iframe style="padding-top: 0px;" name="advertiseList" id="advertiseList"
			src="" frameBorder="0" width="100%"
			οnlοad="javascript:this.height=advertiseList.document.body.scrollHeight"
			scrolling="no"></iframe>

 

第二步:actionForm代码

 actionForm里面的字段名称和前台页面要保存一直,学过struts的同学应该明白自动填充的含义。

package com.feinar.b2c.holiday.advertise.form;

import org.apache.struts.action.ActionForm;

/**
 * 查询广告管理用到的formBean
 * @author Administrator
 *
 */
public class AdvertiseSearchForm extends ActionForm{
	public String advertiseName;//广告名称
	public int advertiseState;//广告状态
	public int advertiseType;//发布区域
	public String currentPage;//当前第几页
	public String getAdvertiseName() {
		return advertiseName;
	}

	public void setAdvertiseName(String advertiseName) {
		this.advertiseName = advertiseName;
	}

	public int getAdvertiseState() {
		return advertiseState;
	}

	public void setAdvertiseState(int advertiseState) {
		this.advertiseState = advertiseState;
	}

	public int getAdvertiseType() {
		return advertiseType;
	}

	public void setAdvertiseType(int advertiseType) {
		this.advertiseType = advertiseType;
	}

	public String getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(String currentPage) {
		this.currentPage = currentPage;
	}
}

 

第三步:action代码

 

/**
	 * 查询广告
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward search(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		AdvertiseSearchForm advertiseSearchForm = (AdvertiseSearchForm) actionForm;
		String action  = request.getParameter("action");
		int pageSize = 5;//默认每页显示的数量为10条
		System.out.println(action);
		List list = advertiseManageService.getAdvertiseList(advertiseSearchForm,pageSize);//获取对应的数据列表
		List list2 = advertiseManageService.getTotalCount(advertiseSearchForm,pageSize);//获取关于多少页,每页几条数据,一共多少页
		request.setAttribute("list", list);
		request.setAttribute("list2", list2);
		request.setAttribute("advertiseSearchForm", advertiseSearchForm);
		return mapping.findForward("list");
	}

 第四步:service业务逻辑层核心代码

/**
	 * 获取广告列表
	 */
	public List getAdvertiseList(AdvertiseSearchForm advertiseSearchForm,int pageSize)
			throws Exception {
		// TODO Auto-generated method stub
		List list = null;
		int currentPage = Integer.parseInt(advertiseSearchForm.getCurrentPage());
		int index = (currentPage-1)*pageSize;
		int end = currentPage*pageSize;
		try{
			String sql = getSql(advertiseSearchForm,2,index,end);
			list = advertiseManageDao.getAdvertiseList(sql);
		}catch (Exception e) {
			throw new Exception(e.getMessage().toString());
		}
		return list;
	}
	
	/**
	 * 得到查询的数据库sql语句
	 * @param advertiseSearchForm
	 * @param type
	 * @param index
	 * @param end
	 * @return
	 */
	public String getSql(AdvertiseSearchForm advertiseSearchForm,int type,int index,int end){
		String sqlwhere = " where 1=1 ";
		String sql = "";
		if(type==1){
			//获取评论总条数
			sql= "select count(*) from HOLIDAY_ADVERTISEMENT";
		}
		else if(type==2){
			//获取评论信息内容
			sql = "select * from (select ADVERTISEID,ADVERTISENAME,PIC,LINKURL,HOTELADDNUM,HOLIDAYADDNUM,HOLIDAYTYPEADDNUM,PUBLISHSTATE,rownum n " +
					"from HOLIDAY_ADVERTISEMENT ";
		}
		if(advertiseSearchForm.getAdvertiseName()!=null && !"".equals(advertiseSearchForm.getAdvertiseName().trim())){
			sqlwhere += " and ADVERTISENAME = '"+advertiseSearchForm.getAdvertiseName().trim()+"'";
		}
		if(advertiseSearchForm.getAdvertiseType()==1){
			sqlwhere += " and HOTELADDNUM is not null ";
		}
		if(advertiseSearchForm.getAdvertiseType()==2){
			sqlwhere += " and HOLIDAYADDNUM is not null ";
		}
		if(advertiseSearchForm.getAdvertiseType()==3){
			sqlwhere += " and HOLIDAYTYPEADDNUM is not null ";
		}
		if(advertiseSearchForm.getAdvertiseState()==0){
			sqlwhere += " and PUBLISHSTATE = 0 ";
		}
		if(advertiseSearchForm.getAdvertiseState()==1){
			sqlwhere += " and PUBLISHSTATE = 1 ";
		}
		if(advertiseSearchForm.getAdvertiseState()==2){
			sqlwhere += " and PUBLISHSTATE = 2 ";
		}
		if(type==2){
			sql += sqlwhere+" and rownum<="+end +" order by ADVERTISEID asc) where n>"+index;
		}else {
			sql += sqlwhere;
		}
		return sql;
	}
	
	/**
	 * 获取总共的条数,和页数,保存到一个list里面
	 */
	public List getTotalCount(AdvertiseSearchForm advertiseSearchForm,int pageSize) throws Exception {
		// TODO Auto-generated method stub
		int count = 0;
		int currentPage = Integer.parseInt(advertiseSearchForm.getCurrentPage());
		int pageCount = 0;//总共的页数
		List list = new ArrayList();
		try{
			String sql = getSql(advertiseSearchForm,1,0,0);
			count = advertiseManageDao.getTotalCount(sql);
			if (count % pageSize == 0) {
				pageCount = count / pageSize;
			} else {
				pageCount = count / pageSize + 1;
			}
			list.add(count);
			list.add(pageCount);
			list.add(advertiseSearchForm.getCurrentPage());
		}catch (Exception e) {
			throw new Exception(e.getMessage().toString());
		}
		return list;
	}

 

第五步:dao层组件代码

/**
	 * 获取某广告信息列表
	 */
	public List getAdvertiseList(String sql) throws Exception {
		// TODO Auto-generated method stub
		List list = new ArrayList();
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		try{
			connection = this.getJdbcTemplate().getDataSource().getConnection();
			preparedStatement = connection.prepareStatement(sql);
			resultSet = preparedStatement.executeQuery();
			while(resultSet.next()){
				Advertisement advertisement = new Advertisement();
				advertisement.setAdvertiseid(resultSet.getLong("ADVERTISEID"));
				advertisement.setAdvertisename(resultSet.getString("ADVERTISENAME"));
				advertisement.setPic(resultSet.getBlob("PIC"));
				advertisement.setLinkurl(resultSet.getString("LINKURL"));
				advertisement.setHoteladdnum(resultSet.getLong("HOTELADDNUM"));
				advertisement.setHolidayaddnum(resultSet.getLong("HOLIDAYADDNUM"));
				advertisement.setHolidaytypeaddnum(resultSet.getLong("HOLIDAYTYPEADDNUM"));
				advertisement.setPublishstate(resultSet.getLong("PUBLISHSTATE"));
				list.add(advertisement);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(resultSet!=null){
				resultSet.close();
			}
			if(preparedStatement!=null){
				preparedStatement.close();
			}
			if(connection!=null){
				connection.close();
			}
		}
		return list;
	}

	/**
	 * 获取满足要求的广告总记录数(用于查询分页)
	 */
	public int getTotalCount(String sql) throws Exception {
		// TODO Auto-generated method stub
		int totalCount = 0;
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		try{
			connection = this.getJdbcTemplate().getDataSource().getConnection();
			preparedStatement = connection.prepareStatement(sql);
			resultSet = preparedStatement.executeQuery();
			while(resultSet.next()){
				totalCount = resultSet.getInt(1);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(resultSet!=null){
				resultSet.close();
			}
			if(preparedStatement!=null){
				preparedStatement.close();
			}
			if(connection!=null){
				connection.close();
			}
		}
		return totalCount;
	}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值