mysql 较为高效的分页
Posted liguangsunls
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql 较为高效的分页相关的知识,希望对你有一定的参考价值。
直接上代码
DaoImpl:
/**
* 开发转让页面展示 ,查询搜索数据,而且分页展示
* @param zrdp 搜索条件封装对象
* @return
*/
@SuppressWarnings("unchecked")
public List<Zrdpxx> showAllZrdpxxPageindex(Zrdpxx zrdp,int pageindex,String keys) {
String
sql ="select * from zrdpxx where dpxx_htaiztai = 2";
if(zrdp!=null){
if(zrdp.getDpxxId()!=null&&!zrdp.getDpxxId().equals("")){
sql+=" and dpxx_id =" +zrdp.getDpxxId();
}
if(zrdp.getDpxxQyu()!=null&&!zrdp.getDpxxQyu().equals("-选择区域-")){
sql+=" and dpxx_qyu = ‘"+zrdp.getDpxxQyu()+"‘";
}
if(zrdp.getDpxxPyetai()!=null&&!zrdp.getDpxxPyetai().equals("")){
sql+=" and dpxx_pyetai = ‘"+zrdp.getDpxxPyetai()+"‘";
}
if(zrdp.getDpxxZyetai()!=null&&!zrdp.getDpxxZyetai().equals("0")&&!zrdp.getDpxxZyetai().equals("null")){
sql+=" and dpxx_zyetai = ‘"+zrdp.getDpxxZyetai()+"‘";
}
if(zrdp.getBigmji()!=0&&zrdp.getSmlmj()!=-1){
sql += " and dpxx_mji between "+zrdp.getSmlmj()+" and " +zrdp.getBigmji();
}
//開始时间和结束时间均有值
if(zrdp.getBeginTime()!=null&&!zrdp.getBeginTime().equals("")&&zrdp.getEndTimes()!=null&&!zrdp.getEndTimes().equals("null")){
sql +=" and dpxx_fbusjian between ‘"+zrdp.getBeginTime()+"‘ and ‘"+zrdp.getEndTimes()+"‘";
}
//仅仅有開始时间
if(zrdp.getBeginTime()!=null&&!zrdp.getBeginTime().equals("")&&zrdp.getEndTimes()==null){
sql +=" and dpxx_fbusjian between ‘"+zrdp.getBeginTime()+"‘ and now()";
}
if(!("标题 地址 电话").equals(keys)){
sql +=" and (dpxx_bti like ‘%"+keys+"%‘ or tel like ‘%"+keys+"%‘ or dpxx_jtidzhi like ‘%"+keys+"%‘)";
}
sql +=" order by dpxx_fbusjian desc";
}
return getSession().createSQLQuery(sql).addEntity(Zrdpxx.class).setFirstResult((pageindex-1)*20).setMaxResults(20).list();
}
// public int getallzrdpcounts() {
// Object oo = getSession().createSQLQuery("select count(dpxx_id) from zrdpxx where dpxx_htaiztai =2").uniqueResult();
// return Integer.parseInt(oo.toString());
// }
//
/**
* 开发转让页面展示 ,查询满足条件的开发转让店铺信息
* @param zrdp 搜索条件封装对象
* @return
*/
@SuppressWarnings("unchecked")
public int showAllZrdpxx(Zrdpxx zrdp,String keys) {
String
sql ="select count(dpxx_id) from zrdpxx where dpxx_htaiztai = 2";
if(zrdp!=null){
if(zrdp.getDpxxId()!=null&&!zrdp.getDpxxId().equals("")){
sql+=" and dpxx_id =" +zrdp.getDpxxId();
}
if(zrdp.getDpxxQyu()!=null&&!zrdp.getDpxxQyu().equals("-选择区域-")){
sql+=" and dpxx_qyu = ‘"+zrdp.getDpxxQyu()+"‘";
}
if(zrdp.getDpxxPyetai()!=null&&!zrdp.getDpxxPyetai().equals("")){
sql+=" and dpxx_pyetai = ‘"+zrdp.getDpxxPyetai()+"‘";
}
if(zrdp.getDpxxZyetai()!=null&&!zrdp.getDpxxZyetai().equals("0")&&!zrdp.getDpxxZyetai().equals("null")){
sql+=" and dpxx_zyetai = ‘"+zrdp.getDpxxZyetai()+"‘";
}
if(zrdp.getBigmji()!=0&&zrdp.getSmlmj()!=-1){
sql += " and dpxx_mji between "+zrdp.getSmlmj()+" and " +zrdp.getBigmji();
}
//開始时间和结束时间均有值
if(zrdp.getBeginTime()!=null&&!zrdp.getBeginTime().equals("")&&zrdp.getEndTimes()!=null&&!zrdp.getEndTimes().equals("null")){
sql +=" and dpxx_fbusjian between ‘"+zrdp.getBeginTime()+"‘ and ‘"+zrdp.getEndTimes()+"‘";
}
//仅仅有開始时间
if(zrdp.getBeginTime()!=null&&!zrdp.getBeginTime().equals("")&&zrdp.getEndTimes()==null){
sql +=" and dpxx_fbusjian between ‘"+zrdp.getBeginTime()+"‘ and now()";
}
if(!("标题 地址 电话").equals(keys)){
sql +=" and (dpxx_bti like ‘%"+keys+"%‘ or tel like ‘%"+keys+"%‘ or dpxx_jtidzhi like ‘%"+keys+"%‘)";
}
}
Object oo = getSession().createSQLQuery(sql).uniqueResult();
return Integer.parseInt(oo.toString());
}
Action:
/**
* 开发转让展示 ,追加对象搜索
* @return 集合
*/
public String getZrdpxxByzr(){
if(pid==1){
allcounts = zrDao.showAllZrdpxx(zrdpxx,keys);
}
forkaifazrlist = zrDao.showAllZrdpxxPageindex(zrdpxx, pid, keys);
kfmap.put("allkaifazr",allcounts);
kfmap.put("allkaifazrPageindex", forkaifazrlist);
if(forkaifazrlist!=null){
return "showallsuc";
}
return "fail";
}
js:
function showws(){
zrshopids =$.trim($("#txt_infoid").val());
var isnums = isNaN(zrshopids);
var yanzhengs = /^\s*$/;
var isspaces =yanzhengs.test(zrshopids);
if(isnums){
zrshopids=-1;
}
if(isspaces){
zrshopids = "";
}
if($("#txt_infoid").val().indexOf(".")!=-1){
alert("亲。请输入正确的店铺ID!");
}
else{
$("#showtable tr:not(:first)").remove();
$("#bottompage").html("");
$.post("zrAction!getallDpxx.action",{"pid":pageindex,"zrdpxx.dpxxPyetai":$("#ddl_yetaicate").val().substring(3,7),"zrdpxx.dpxxHtaiztai":$("#ddl_after").val(),"zrdpxx.dpxxZyetai":$("#ddl_yetai").val(),"zrdpxx.dpxxQtaiztai":$("#ddl_before").val(),"zrdpxx.dpxxHtaiztai":$("#ddl_after").val(),"zrdpxx.dpxxSfouzdin":$("#istopno").val(),"tel":$.trim($("#txt_key").val()),"dpidsss":zrshopids,"showwhotel":$("#showwhotel").val()},
function(data){
if(data["zrdxpxxPageindexCount"].length<=0){
alert("没有符合当前条件的数据!");
}
else{
$.each(data["zrdxpxxPageindexCount"],function(i,list){
$tr=$("<tr></tr>");
$td12 =$(" <td align=‘center‘ valign=‘top‘ style=‘border-right:0;‘><input type=‘checkbox‘ name =‘row_checkbox‘ id =‘checkDo‘ value=‘"+list.dpxxId+"‘/></td>");
$td1=$("<td align=‘center‘ valign=‘top‘><a href=‘TransferManager24.1.jsp?dpidsss="+list.dpxxId+"‘ target=‘_blank‘>"+list.dpxxId+"</a></td>");
$td2 =$("<td align=‘center‘ valign=‘top‘>"+list.dpxxFbusjian.substring(0,10)+"</td>");
switch(list.dpxxXxilyuan){
case 1:
$td3 =$("<td align=‘center‘ valign=‘top‘>客户</td>");
break;
case 4:
$td3 =$("<td align=‘center‘ valign=‘top‘>外网</td>");
break;
case 2:
$td3 =$("<td align=‘center‘ valign=‘top‘>报纸</td>");
break;
case 3:
$td3 =$("<td align=‘center‘ valign=‘top‘>扫街</td>");
break;
case 5:
$td3 =$("<td align=‘center‘ valign=‘top‘>其它</td>");
break;
}
$td4 =$("<td align=‘center‘ valign=‘top‘>"+list.dpxxZyetai+"</td>");
$td5 =$("<td align=‘center‘ valign=‘top‘>"+list.dpxxQyu+"</td>");
$td7 =$("<td align=‘center‘ valign=‘top‘>"+list.dpxxBti+"</td>");
$td8 =$("<td align=‘center‘ valign=‘top‘>"+list.dpxxJtidzhi+"</td>");
$td13=$("<td align=‘center‘ valign=‘top‘>"+list.mytel+"</td>");
if(list.dpxxSfouzdin==1){
$td9 =$("<td align=‘center‘ valign=‘top‘>是</td>");
}
if(list.dpxxSfouzdin==2){
$td9 =$("<td align=‘center‘ valign=‘top‘>否</td>");
}
if(list.dpxxQtaiztai==1){
$td10 =$("<td align=‘center‘ valign=‘top‘>显示</td>");
}
if(list.dpxxQtaiztai==2){
$td10 =$("<td align=‘center‘ valign=‘top‘>不显示</td>");
}
switch(list.dpxxHtaiztai){
case 1:
$td11 =$("<td align=‘center‘ valign=‘top‘>屏蔽</td>");
break;
case 2:
$td11 =$("<td align=‘center‘ valign=‘top‘ style=‘color:green‘>开发</td>");
break;
case 3:
$td11 =$("<td align=‘center‘ valign=‘top‘ style=‘color:blue‘>重点</td>");
break;
case 4:
$td11 =$("<td align=‘center‘ valign=‘top‘ style =‘color:red‘>售后</td>");
break;
case 5:
$td11 =$("<td align=‘center‘ valign=‘top‘ style=‘color:#A757A8‘>案例</td>");
break;
}
$tr.append($td1).append($td2).append($td3).append($td4).append($td5).append($td7).append($td8).append($td13).append($td9).append($td10).append($td11).append($td12);
$("#showtable").append($tr);
});
pagesize = data["zrdxpxxAllCount"]%maxresult==0?
parseInt(data["zrdxpxxAllCount"]/maxresult):parseInt(data["zrdxpxxAllCount"]/maxresult)+1;
var $uppage = $("<a href=‘javascript:go(true)‘><<上一页    </a>");
var $span =$("<span style=‘font-size:12px‘>共"+data["zrdxpxxAllCount"]+"条  第"+pageindex+"页  总"+pagesize+"页</span>");
var $downpage =$("<a href=‘javascript:go(false)‘>    下一页>>    </a>");
var $select = $("<select onchange=‘javascript:gotos()‘ id=‘gotosid‘></select>");
for(var i = 1;i<=pagesize;i++){
$select.append("<option value="+i+">第"+i+"页</option>");
}
$("#bottompage").append($uppage).append($span).append($downpage).append($select);
$("#gotosid").find("option[value=‘"+pageindex+"‘]").attr("selected","selected");
}
});
}
}
以上是关于mysql 较为高效的分页的主要内容,如果未能解决你的问题,请参考以下文章