SSH分页查询了解其运行原理

SSH框架 (Spring、Status、Hibernate)
运行流程 :
前台页面发送请求到status.xml根据请求的前缀(email) 例如:请求路劲emailselectFirstComment
找到所对应的action的class的例如:EmailAction
action中返回的字符串和result的name值比对 跳转页面或者重定向到某个方法上

<action name="email*" class="EmailAction" method="{1}">
			<result name="recipients">/E_write.jsp</result>
			<result name="add">/E_write.jsp</result>
			<result name="input" type="redirectAction">emailrecipients</result>
			<result name="selectFirstPage">/E_details.jsp</result>
			<result name="selectNextPage">/E_details.jsp</result>
			<result name="delete" type="redirectAction">emailselectFirstPage</result>
			<result name="spam">/E_delete.jsp</result>
			<result name="find">/E_view.jsp</result>
			<result name="reduction" type="redirectAction">emailspam</result>
			<result name="deleteReal" type="redirectAction">emailspam</result>
			<result name="error">/error.jsp</result>
</action>

,在action控制层中找到除去前缀(email)的方法名 selectFirstComment 和mvc一样调用service和dao层方法

 /**
     * 分页查询
     * @return
     */
    public String selectFirstComment(){
        HttpServletRequest request1 = ServletActionContext.getRequest();
        Map<String,Object> request = (Map<String, Object>)ActionContext.getContext().get("request");
        int pageSize = 5;
        Integer page = 1;
        if(request1.getParameter("page")!=null){
            page = Integer.parseInt(request1.getParameter("page"));
        }
        try{
            List list = commentBiz.selectByPage(page, pageSize);
            int totalPage = commentBiz.totalPage();
            int zcount = totalPage%pageSize==0 ? totalPage/pageSize : totalPage/pageSize+1;
            request.put("commentList", list);
            request.put("totalPage", zcount);
            request.put("currentPage", page);
        }catch(Exception e){
            e.printStackTrace();
        }
        return "selectFirstComment";
    }

在dao层执行sql语句

    /**
	 * 分页查询,获取每页的记录
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Email> selectByPage(String className, int pageNo, int pageSize,String userName) {
		Query query = this.getSession().createQuery("from "+ className + " where RECIPIENTS =:userName and isDeleted =1")
				.setString("userName", userName);
		query.setFirstResult((pageNo - 1) * pageSize);//每页显示的第一条记录
		query.setMaxResults(pageSize);//每页显示的记录数
		return query.list();
	}
	/**
	 * 分页查询邮件,获取总页数
	 */
	@Override
	public int totalPage(String className,String userName) {
		String hql = "select count(*) from "+ className + " where RECIPIENTS =:userName and isDeleted =1";
		Query query = this.getSession().createQuery(hql).setString("userName", userName);
		int total = Integer.parseInt(query.list().get(0).toString());
		return total;
	}

注意的时在SSH中要配置bean 需要在applicationContext.xml中配置

<!-- Email dao -->
	<bean id="EmailDaoImpl" class="com.accp.dao.impl.EmailDaoImpl">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<!-- biz -->
	<bean id="EmailBizImpl" class="com.accp.biz.impl.EmailBizImpl">
		<property name="emailDao" ref="EmailDaoImpl" />
	</bean>

	<!-- action -->
	<bean id="EmailAction" class="com.accp.action.EmailAction" scope="prototype">
		<property name="emailBiz" ref="EmailBizImpl" />
	</bean>




<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
		<property name="configLocation" value="classpath:hibernate.cfg.xml" />

		<property name="mappingResources">
			<list>
				<value>com/accp/pojo/Email.hbm.xml</value>
			</list>
		</property>
</bean>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值