sql拼接实现自由多条件查询及内容分页总结

上周接受了一个新任务,实现cms新内容管理模块的多条件查询及内容分页

首先,我先实现了html页面的隐藏与显示条件输入框。

网上一般的方法就是用js显示或隐藏层实现

htmlz中:<input type="button" id="searchMore" value="更多搜索条件" οnclick="moreCondition()"> 

JS代码:

function moreCondition(){
    var showMore = document.getElementById("hiddenMore");
    if(showMore.style.display=="none"){
   showMore.style.display="block";
    }
    else{
   showMore.style.display="none";
    }    
 
}

然后页面用struts2标签+JSP+EL表达式实现

后台最重要的其实就是查询和分页的sql语句:

代码如下:

@Override
public List getSearchArticle(int pageNo, int pageSize, String nodeId,String childNodeId, String title, String editor, String author,
String beginTime, String endTime) throws AppException {
String sql = "select top "+pageSize+" * from (select b.*,a.nodeId,a.nodeIdLevel3,a.nodeIdLevel2, ROW_NUMBER() OVER(ORDER BY b.insertedTime desc) AS ROWNUMBER ";
sql+="from cms_FK_nodeArticle as a join cms_article as b on a.articleId = b.id where a.mainFlag = '"+Constance.MAIN_FLAG+"' and b.status!= '"+Constance.DATA_STATUS_STOP+"'";
    
   if(childNodeId != null && !childNodeId.equals("")){
sql += " and (nodeIdLevel4='"+childNodeId+"')  ";
}else{
if( nodeId != null && nodeId.trim().length() > 0 )
sql += " and (nodeIdLevel3='"+nodeId+"')  ";
   }

if(title != null && title.trim().length() > 0){
sql += " and title like '%"+title+"%' ";
}

if(editor != null && editor.trim().length() > 0){
sql += " and editor ='"+editor+"'";
}

if(author != null && author.trim().length() > 0){
  sql += " and author = '"+author+"'";
}

if(beginTime != null && beginTime.trim().length() > 0 && endTime != null && endTime.trim().length() > 0){
  sql += " and insertedTime between '"+beginTime+"' and '"+endTime+" 23:59:59'";
}

sql+=") F where F.rownumber >"+(pageNo-1)*pageSize;
   JdbcHelper jdbcUtil=(JdbcHelper)SpringBeanUtil.getBean("jdbcBaseTransaction");
return jdbcUtil.queryForList(sql);

}


查询出分页结果,在前端读取,其中最麻烦的是每当点下一页后的参数传递问题。

网上有两种方法,一种是提交action后面参数拼接法 为  Parametername="value" &param....等

另一种方法是用js控制form提交,就是在form里隐藏写一个存储页码的隐藏域,然后用js控制随表单提交,我是受别人网上说的,在页面里用session存储传递的参数

但是session有其局限性,一是session有一定的时效,过一段时间session会失效,另外session由于浏览器不关会一直存在的,所以有时候会需要清除缓存

反正我做的最后查询的时候,有时候会出乱子,希望朋友们给我指点个更好的方法实现多条件自由分页查询。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值