- onecan
- 等级:
- 性别:
- 文章: 100
- 积分: 200
- 来自: 广州
|
接上:
在struts的Action中:
java 代码
- private ActionForward findAllRole(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
- String curPageNO = request.getParameter("curPageNO");
- String myaction;
- String search=request.getParameter("search");
-
- if(search==null){
- search="";
- myaction="findAllRole.do";
- }else{
- myaction="findAllRole.do?search="+search;
- }
- removeRequestAttribute(mapping,request);
- try {
-
- CriteriaQuery cq=new CriteriaQuery(Role.class,curPageNO,myaction);
- cq.setPageSize(Constants.PAGE_SIZE);
- if(!AppUtils.isBlank(search)){
- cq.like("name","%"+search+"%");
- cq.add();
- }
- IState state=new StateImpl();
- PageSupport ps= (PageSupport)getBiz().findAllRole(cq, state);
- request.setAttribute("search",search);
- request.setAttribute("curPageNO",new Integer(ps.getCurPageNO()));
- request.setAttribute("offset", new Integer(ps.getOffset()+1));
- request.setAttribute("list", ps.getResultList());
- if(!AppUtils.isBlank(ps.getResultList()))
- request.setAttribute("toolBar", ps.getToolBar());
- }catch (Exception e){
- return handleException(mapping, request, e, "RoleAction's findAllRole");
- }
- return mapping.findForward("view");
- }
采用QBC对单表的分页很简单,只需要改变参数,并且在action和jsp之间传递。如果要分页的内容涉及到几张表,可以将业务逻辑封装为一个视图,并且做好视图相应的实体类,便可以象操纵单表一样。
<!---->1、 <!---->另外可以使用HQL来实现分页,在dao中:
java 代码
- public PageSupport find(final HqlQuery hqlQuery,final boolean needParameter)
- {
-
- return (PageSupport)getHibernateTemplate().execute(
- new HibernateCallback() {
- public Object doInHibernate(Session session) throws HibernateException, SQLException {
- Query query=session.createQuery(hqlQuery.getQueryString());
- if(needParameter)
- query.setParameters(hqlQuery.getParam(), hqlQuery.getTypes());
- int allCounts=query.list().size();
- int curPageNO = PagerUtil.getCurPageNO(hqlQuery.getCurPage());
- int offset = PagerUtil.getOffset(allCounts, curPageNO, hqlQuery.getPageSize());
- String toolBar = PagerUtil.getBar(hqlQuery.getMyaction(),allCounts,curPageNO,hqlQuery.getPageSize());
- query.setFirstResult(offset);
- query.setMaxResults(hqlQuery.getPageSize());
- return new PageSupport(query.list(),toolBar,offset,curPageNO);
- }
- }
- ,true);
- }
HqlQuery 代码如下:
java 代码
- public class HqlQuery implements Serializable{
-
- private String curPage=null ;
- private int pageSize=10;
- private String myaction;
- private String myform;
- private String queryString;
- private Object[] param;
- private Type[] types;
- public HqlQuery(String queryString, Object[] param) {
- this.queryString = queryString;
- this.param = param;
- }
-
- public HqlQuery(String myaction) {
- this.myaction = myaction;
- }
- public Object[] getParam() {
- return param;
- }
-
- public HqlQuery(String myaction, String queryString, Object[] param,Type[] types) {
- this.myaction = myaction;
- this.queryString = queryString;
- this.param = param;
- this.types=types;
- }
- public void setParam(Object[] param) {
- this.param = param;
- }
- public String getCurPage() {
- return curPage;
- }
- public void setCurPage(String curPage) {
- this.curPage = curPage;
- }
- public String getMyaction() {
- return myaction;
- }
- public void setMyaction(String myaction) {
- this.myaction = myaction;
- }
- public String getMyform() {
- return myform;
- }
- public void setMyform(String myform) {
- this.myform = myform;
- }
- public int getPageSize() {
- return pageSize;
- }
- public void setPageSize(int pageSize) {
- this.pageSize = pageSize;
- }
- public String getQueryString() {
- return queryString;
- }
- public void setQueryString(String queryString) {
- this.queryString = queryString;
- }
- public Type[] getTypes() {
- return types;
- }
- public void setTypes(Type[] types) {
- this.types = types;
- }
-
- }
如此的单元测试:
java 代码
- public final void testfindOtherFunctionByHql() {
- logger.debug("testfindFunctionByRole");
- long l1=System.currentTimeMillis();
- String queryString="from Function where id=?";
- HqlQuery hqlQuery=new HqlQuery("myaction");
- hqlQuery.setCurPage("1");
-
-
- PageSupport functions=BeanFactory.getInstance().getRightDelegate().findOtherFunctionByHql(hqlQuery,"1", state);
- long l2=System.currentTimeMillis();
- AppUtils.printCollection(functions.getResultList());
- List list=functions.getResultList();
- for(int i=0;i
- Function f=(Function)list.get(i);
- System.out.println("getId = "+f.getId());
- }
- System.out.println("一共用时为 : "+(l2-l1));
- }
总结:将分页的功能封装起来,每次请求构造参数不同的CriteriaQuery 和HqlQuery,后台的操作都是重复不变的,变得是查询的条件。如使用CriteriaQuery可以这样构造条件,Or或者and的条件可以用面向对象的方式来构造:
java 代码
- public final void findAllRole() {
- logger.debug("findAllRole");
- long l1=System.currentTimeMillis();
- String curPage = "2";
- String myaction="xxx.do?";
- String myform="forms[0]";
- CriteriaQuery cq=new CriteriaQuery(Role.class,curPage,myaction,myform);
- cq.setPageSize(2);
- cq.setCurPage(curPage);
- cq.addOrder("asc","id");
- cq.like("name", "ROLE_SUPERVISOR");
- cq.eq("enabled", "1");
- cq.eq("roleType", "ROLE_SUPERVISOR");
-
- cq.add(cq.or(cq.and(cq, 0, 1), cq, 2));
-
-
- PageSupport ps=BeanFactory.getInstance().getRightDelegate().findAllRole(cq,state);
- long l2=System.currentTimeMillis();
- System.out.println("list size: "+ps.getResultList().size());
- System.out.println("一共用时为 : "+(l2-l1));
- for(int i=0;i
- Role role=(Role)ps.getResultList().get(i);
- System.out.println("id = "+ role.getId());
-
-
- System.out.println("-----------------");
- }
- AppUtils.printCollection(ps.getResultList());
- System.out.println(ps.getToolBar());
- }
看cq.add(cq.or(cq.and(cq, 0, 1), cq, 2)),这句是为CriteriaQuery增加条件。意思是条件(0 and 1) or 2。以下是日志打出来的结果:<o:p></o:p>
java 代码
- DEBUG [14:50:53] (POJODelegate.java:execute:82) - POJODelegate executing
- INFO [14:50:53] (SequenceProcessor.java:doActivities:23) - SequenceProcessor 流程开始 <-- FindAllRoleProcessor
- INFO [14:50:53] (SequenceProcessor.java:doActivities:32) - 活动 : FindAllRole
- Hibernate: select count(*) as y0_ from t_role this_ where ((this_.name like ? and this_.enabled=?) or this_.role_type=?) order by this_.id asc
-
- Hibernate: select this_.id as id0_, this_.name as name3_0_, this_.role_type as role3_3_0_, this_.enabled as enabled3_0_, this_.note as note3_0_ from t_role this_ where ((this_.name like ? and this_.enabled=?) or this_.role_type=?) order by this_.id asc limit ?
- INFO [14:50:53] (SequenceProcessor.java:doActivities:69) - SequenceProcessor 流程结束 -->
- list size: 1
- 一共用时为 : 531
- id = 1
- -----------------
- 首页 上一页 下一页 尾页 共1条记录 转到"location='xxx.do?&curPageNO='+this.options[this.selectedIndex].value">1' selected>第1页
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|