用ssh和bootstrap制作分页功能

(1)Hibernate插入、查询、删除操作 HQL :http://blog.sina.com.cn/s/blog_69fe52e00100zxyd.html
(2)hibernateTemplate 怎样执行hql呢?:https://wenda.so.com/q/1363616729066709
(3)hibernate延迟加载的传说级错误org.hibernate.LazyInitializationException: could not initialize proxy - no Session:http://blog.csdn.net/elfenliedef/article/details/6011892

applicationContext.xml中:
<!-- 指定Spring配置文件的Schema信息 (XMLSchema-instance、beans、context、aop、p、tx)-->
<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                        http://www.springframework.org/schema/aop    
                          http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
                         http://www.springframework.org/schema/tx    
                          http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
                          http://www.springframework.org/schema/context
                          http://www.springframework.org/schema/context/spring-context-3.0.xsd"

>
<!-- 配置(第三方依赖包中(使用连接池技术 )的实现类包的)数据源 -->
<bean id ="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">...</bean>
<!-- 定义了Hibernate的SessionFactory -->
<bean id="sessionFactory"  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">...</bean>
<!-- 定义hibernateTemplate -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">...</bean>
<!-- 业务操作层(ServiceImpl) -->
<bean id="postService" class="com.serviceImpl.PostServiceImpl"/>
<bean id="studentService" class="com.serviceImpl.StudentServiceImpl"/>
<!-- 开启Spring的Annotation注解处理器 -->
<context:annotation-config />

<!-- 开启Spring的bean自动扫描机制来查找和管理bean实例     -->
<context:component-scan base-package="com"/>
</beans>
IPostService中:
public interface IPostService {
    // 按用户ID进行分页查找
    public List<Post> pagePostsBySid(int sid,int pageNo,int pageSize);
}
PostServiceImpl中:
import org.hibernate.Session;
import org.hibernate.SessionFactory;
public class PostServiceImpl implements IPostService{
    @Resource(name="hibernateTemplate")
    //必须只创建一个HibernateTemplate,是为了让所有操作都在同一个HibernateTemplate的管理下。
    HibernateTemplate template;
    //通过配置文件得到SessionFactory
    SessionFactory sf=template.getSessionFactory();
    //通过SessionFactory 得到一个Session
    Session session=sf.openSession();

    @Override
    public List<Post> pagePostsBySid(int sid, int pageNo, int pageSize) {
        final int pNo=pageNo;
        final int pSize=pageSize;
        final String hql = "from Post as p where p.student.id = ? order by p.id desc";
        List list=template.executeFind(new HibernateCallback<Object>(){
            @Override
            public Object doInHibernate(Session sn) throws HibernateException, SQLException {
                Query query=sn.createQuery(hql);
                query.setString(0, sid);
                query.setMaxResults(pSize);
                query.setFirstResult((pNo-1)*pSize);
                List result=query.list();
                if(!Hibernate.isInitialized(result)){
                    Hibernate.initialize(result);
                }
                return result;
            }

        });
        return result;
    }
}
action中:
import java.util.List;
import com.orm.Student;
import com.opensymphony.xwork2.ActionSupport;
import com.service.IPostService;
import com.service.IStudentService;
public class StudentAction extends ActionSupport{
   @Resource(name="postService")
   IPostService postService;
   @Resource(name="studentService")
   IStudentService studentService;

   private List<Post> postsBySid; //根据学生id搜索的帖子
   private int nowPage;    //当前页
   private int endPage;    //尾页
   private int lastPage;   //上一页
   private int nextPage;   //下一页
   //set...get...
   @Override
    public String execute() throws Exception {
       try{
           pageMyPosts();
          return "MyHome";
       }catch(Exception e){
           e.printStackTrace();
           return ERROR;
       }
    }

    //分页
    private void pageMyPosts() throws Exception{
          Student s=(Student)ServletActionContext.getRequest().getSession().getAttribute("student");
          setStudent(s);
          int pageNo=Integer.valueOf(new String(ServletActionContext.getRequest().getParameter("pageNo").getBytes("iso-8859-1"),"utf-8"));
          setPostsBySid(postService.pagePostsBySid(s.getId(), pageNo, 10)); //调用自定义的pagePostsBySid(int sid, int pageNo, int pageSize)数据分页方法
          setNowPage(pageNo);   //设置
          setLastPage(nowPage-1);  //设置
          setNextPage(nowPage+1);  //设置
          int totalPostCount=postService.getPostCountBySid(s.getId()); //总数据条数
          int totalPage=totalPostCount/10+((totalPostCount%10>0)?1:0);  //每10条数据成一页
          setEndPage(totalPage); //设置
   }
}
struts.xml中:
<!-- 学生管理 -->
 <action name="student" class="com.action.StudentAction">
      <result name="MyHome">/MyHome.jsp</result>
 </action>
jsp中:
<!-- 分页 -->
            <ul class="pagination">
                <s:if test="nowPage!=1">
                   <li><a href="<%=request.getContextPath() %>/student.action?pageNo=1">首页</a></li>
                   <li><a href="<%=request.getContextPath() %>/student.action?pageNo=<s:property value="lastPage"/>">上一页</a></li>
                </s:if>
                <!-- 判断当前页在小于7的情况下 -->
                <s:if test="nowPage <7">
                   <!-- 判断尾页小于10时 -->
                   <s:if test="endPage <10">
                      <s:iterator var="i" begin="1" end="endPage">
                        <li><a href="<%=request.getContextPath() %>/student.action?pageNo=<s:property value="#i"/>"><s:property value="#i"/></a></li>
                      </s:iterator>
                   </s:if>
                  <!--  判断尾页大于等于10时 -->
                   <s:else>
                      <s:iterator var="i" begin="1" end="10">
                        <li><a href="<%=request.getContextPath() %>/student.action?pageNo=<s:property value="#i"/>"><s:property value="#i"/></a></li>
                      </s:iterator>
                   </s:else>
                </s:if>
                <!-- 判断当前页在大于等于7的情况下 -->
                <s:else>
                   <!-- 如果尾页大于当前页加4 -->
                   <s:if test="endPage > nowPage+4">
                      <s:iterator var="i" begin="nowPage-5" end="nowPage+4">
                          <li><a href="<%=request.getContextPath() %>/student.action?pageNo=<s:property value="#i"/>"><s:property value="#i"/></a></li>
                      </s:iterator>
                   </s:if>
                   <!-- 如果尾页大于当前页,并且小于等于当前页加4 -->
                   <s:else>
                      <s:iterator var="i" begin="nowPage-5" end="endPage">
                          <li><a href="<%=request.getContextPath() %>/student.action?pageNo=<s:property value="#i"/>"><s:property value="#i"/></a></li>
                      </s:iterator>
                   </s:else>
                </s:else>

                <s:if test="nowPage != endPage">
                   <li><a href="<%=request.getContextPath() %>/student.action?pageNo=<s:property value="nextPage"/>">下一页</a></li>
                   <li><a href="<%=request.getContextPath() %>/student.action?pageNo=<s:property value="endPage"/>">尾页</a></li>
                </s:if>
        </ul>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值