关于分页SQL的小总结

findPage 和findPageTotal条件分页中的条件 较为复杂点的关联查询 有取别名的

<select id="findPage" resultMap="MinOrderInfo" parameterType="map">
         SELECT o.*,w.name buyName,w.MOBILE buyMobile,aa.name sellName,aa.MOBILE sellMobile,rs.CAR_BRAND_NAME carBrandName1,rs.TRANS_PRICE,scn.price,scs.CAR_BRAND_NAME carBrandName3,scs.EXPECT_TRANS_PRICE FROM  order_info  o
       INNER JOIN web_user w ON  w.id=o.SEARCH_USER_ID
       LEFT JOIN (SELECT o.SOURCE_USER_ID ,w.name,w.MOBILE mobile,o.Id  FROM  order_info  o INNER JOIN web_user w ON  w.id=o.SOURCE_USER_ID) aa ON aa.Id=o.Id
       LEFT JOIN release_source rs ON o.source_id = rs.id
       LEFT JOIN search_car_normal scn ON o.SEARCH_normal_ID = scn.id
       LEFT JOIN search_car_special scs ON o.search_special_ID = scs.id
       WHERE 1= 1

        <if test="id != null and id != ‘‘">and o.Id = #{id}</if>
        <if test="sourceId != null and sourceId != ‘‘">and o.SOURCE_ID = #{sourceId}</if>
        <if test="sourceUserId != null and sourceUserId != ‘‘">and o.SOURCE_USER_ID = #{sourceUserId}</if>
        <if test="searchId != null and searchId != ‘‘">and o.SEARCH_ID = #{searchId}</if>
        <if test="searshUserId != null and searshUserId != ‘‘">and o.SEARCH__USER_ID = #{searshUserId}</if>
        <if test="num != null and num != ‘‘">and o.NUM = #{num}</if>
        <if test="depositPrice != null and depositPrice != ‘‘">and o.DEPOSIT_PRICE = #{depositPrice}</if>
        <if test="registratAddress != null and registratAddress != ‘‘">and o.REGISTRAT_ADDRESS = #{registratAddress}</if>
        <if test="pullCarTime != null and pullCarTime != ‘‘">and o.PULL_CAR_TIME = #{pullCarTime}</if>
        <if test="remark != null and remark != ‘‘">and o.REMARK = #{remark}</if>
        <if test="createTime != null and createTime != ‘‘">and o.CREATE_TIME = #{createTime}</if>
        <if test="payTime != null and payTime != ‘‘">and o.PAY_TIME = #{payTime}</if>
        <if test="state != null and state != ‘‘">and o.STATE = #{state}</if>
        <if test="orderAmt != null and orderAmt != ‘‘">and o.ORDER_AMT = #{orderAmt}</if>
        <if test="orderType != null and orderType != ‘‘">and o.ORDER_TYPE= #{orderType}</if>
        <if test="buyName != null and buyName != ‘‘">and w.NAME like CONCAT(‘%‘,#{buyName},‘%‘)</if>
        <if test="buyMobile != null and buyMobile != ‘‘">and w.MOBILE like CONCAT(‘%‘,#{buyMobile},‘%‘)</if>
        <if test="startTime !=null and startTime !=‘‘">and o.CREATE_TIME &gt;#{startTime}</if>
        <if test="endTime !=null and endTime !=‘‘">and o.CREATE_TIME &lt;#{endTime}</if>
        <if test="orderNumber != null and orderNumber != ‘‘">and o.ORDER_NUMBER like CONCAT(‘%‘,#{orderNumber},‘%‘)</if>
        ORDER BY o.CREATE_TIME DESC
        limit #{startRow},#{pageSize}
    </select>
<select id="findPageTotal" resultType="int" parameterType="map">

        SELECT COUNT(1) FROM(SELECT o.*,w.name buyName,w.MOBILE buyMobile,aa.name sellName,aa.MOBILE sellMobile,rs.TRANS_PRICE,scn.price,scs.EXPECT_TRANS_PRICE FROM  order_info  o
       INNER JOIN web_user w ON  w.id=o.SEARCH_USER_ID
       LEFT JOIN (SELECT o.SOURCE_USER_ID ,w.name,w.MOBILE mobile,o.Id  FROM  order_info  o INNER JOIN web_user w ON  w.id=o.SOURCE_USER_ID) aa ON aa.Id=o.Id
       LEFT JOIN release_source rs ON o.source_id = rs.id
       LEFT JOIN search_car_normal scn ON o.SEARCH_normal_ID = scn.id
       LEFT JOIN search_car_special scs ON o.search_special_ID = scs.id
       WHERE 1= 1)qq WHERE 1= 1
        <if test="id != null and id != ‘‘">and Id = #{id}</if>
        <if test="sourceId != null and sourceId != ‘‘">and SOURCE_ID = #{sourceId}</if>
        <if test="sourceUserId != null and sourceUserId != ‘‘">and SOURCE_USER_ID = #{sourceUserId}</if>
        <if test="searchId != null and searchId != ‘‘">and SEARCH_ID = #{searchId}</if>
        <if test="searshUserId != null and searshUserId != ‘‘">and SEARCH__USER_ID = #{searshUserId}</if>
        <if test="num != null and num != ‘‘">and NUM = #{num}</if>
        <if test="depositPrice != null and depositPrice != ‘‘">and DEPOSIT_PRICE = #{depositPrice}</if>
        <if test="registratAddress != null and registratAddress != ‘‘">and REGISTRAT_ADDRESS = #{registratAddress}</if>
        <if test="pullCarTime != null and pullCarTime != ‘‘">and PULL_CAR_TIME = #{pullCarTime}</if>
        <if test="remark != null and remark != ‘‘">and REMARK = #{remark}</if>
        <if test="createTime != null and createTime != ‘‘">and CREATE_TIME = #{createTime}</if>
        <if test="payTime != null and payTime != ‘‘">and PAY_TIME = #{payTime}</if>
        <if test="buyName != null and buyName != ‘‘">and buyName like CONCAT(‘%‘,#{buyName},‘%‘)</if>
        <if test="buyMobile != null and buyMobile != ‘‘">and buyMobile like CONCAT(‘%‘,#{buyMobile},‘%‘)</if>
        <if test="startTime !=null and startTime !=‘‘">and CREATE_TIME &gt;#{startTime}</if>
        <if test="endTime !=null and endTime !=‘‘">and CREATE_TIME &lt;#{endTime}</if>
        <if test="state != null and state != ‘‘">and STATE = #{state}</if>
        <if test="orderAmt != null and orderAmt != ‘‘">and ORDER_AMT = #{orderAmt}</if>
        <if test="orderType != null and orderType != ‘‘">and ORDER_TYPE=#{orderType}</if>
        <if test="orderNumber != null and orderNumber != ‘‘">and ORDER_NUMBER like CONCAT(‘%‘,#{orderNumber},‘%‘)</if>
    </select>

<if test="buyName != null and buyName != ‘‘">and buyName like CONCAT(‘%‘,#{buyName},‘%‘)</if> 注意此处需用别名 不能直接用name 作为条件 否则当页面传值查询时候会报SQL语句查不到相应字段!

页面jsp代码

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib uri="/WEB-INF/tld/fmt.tld" prefix="fmt"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<!-- ubo -->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>订单管理</title>
<meta
    content=‘width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no‘
    name=‘viewport‘>
<link href="${ctx}/resources/css/css.css" rel="stylesheet"
    type="text/css" />
<!-- Bootstrap 3.3.4 -->
<link href="${ctx}/resources/frame/bootstrap/css/bootstrap.min.css"
    rel="stylesheet" type="text/css" />
<!-- Font Awesome Icons -->
<link href="${ctx}/resources/frame/dist/css/font-awesome.min.css"
    rel="stylesheet" type="text/css" />
<!-- Ionicons -->
<link href="${ctx}/resources/frame/dist/css/ionicons.min.css"
    rel="stylesheet" type="text/css" />
<!-- Theme style -->
<link href="${ctx}/resources/frame/dist/css/AdminLTE.min.css"
    rel="stylesheet" type="text/css" />
<!-- AdminLTE Skins. Choose a skin from the css/skins
         folder instead of downloading all of them to reduce the load. -->
<link href="${ctx}/resources/frame/dist/css/skins/_all-skins.min.css"
    rel="stylesheet" type="text/css" />

<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn‘t work if you view the page via file:// -->
<!--[if lt IE 9]>
        <script src="${ctx}/resources/frame/dist/js/html5shiv.min.js"></script>
        <script src="${ctx}/resources/frame/dist/js/respond.min.js"></script>
    <![endif]-->
    </head>
    <body class="skin-blue sidebar-mini">
        <div class="wrapper">
            <%@ include file="../../main_header.jsp"%>

            <%@ include file="../../main_sidebar.jsp"%>

            <!-- Content Wrapper. Contains page content -->
            <div class="content-wrapper">
                <!-- Content Header (Page header) -->
                <section class="content-header">
                    <h1>
                        优博车平台 <small>订单管理</small>
                    </h1>
                </section>

                <!-- Main content -->
                <section class="content">
                    <div class="row">
                        <div class="col-xs-12">
                            <div class="box">
                                <div class="box-header">
                                    <form method="post" id="queryForm"
                                        action="${ctx }/orderinfo/list" name="form1">
                                        <div class="box-tools">
                                            <input type="hidden" name="currentPage" id="currentPage"
                                                value="${page.currentPage}">
                                        </div>
                                        <div class="box-header">
                                            <div class="">
                                                <input type="text" class="form-control input-sm pull-left" style="width: 150px;" placeholder="订单号" name="orderNumber" value="${orderNumber}" maxlength="30">
                                                <input type="text" class="form-control input-sm pull-left"  placeholder="买方用户查找" name="buyName" value="${buyName}" style="width: 100px; margin-left:15px;"  maxlength="20">
                                                <input type="text" class="form-control input-sm pull-left"  placeholder="买方手机号查找" name="buyMobile" value="${buyMobile}" style="width: 130px; margin-left:15px;"  maxlength="20">
                                                <select
                                                    class= "form-control input-sm pull-left" name="orderType" id="orderType"
                                                    style="width:150px;height:30px; margin-left:20px;">
                                                    <option <c:if test="${orderType == ‘0‘}">selected="selected"</c:if>
                                                        value="">类型</option>
                                                    <option <c:if test="${orderType == ‘1‘}">selected="selected"</c:if>
                                                        value="1">销车订单</option>
                                                    <option <c:if test="${orderType == ‘2‘}">selected="selected"</c:if>
                                                        value="2">普通寻车订单</option>
                                                    <option <c:if test="${orderType == ‘3‘}">selected="selected"</c:if>
                                                        value="3">竞价寻车订单</option>
                                                </select>
                                                <input type="text" class="form-control input-sm pull-left" style="width: 100px; margin-left:20px;" placeholder="创建起始日期" name="startTime" value="${startTime}" readonly="true" onclick="WdatePicker({dateFmt:‘yyyy-MM-dd‘})">
                                                  <input type="text" class="form-control input-sm pull-left" style="width: 100px; margin-left:20px;" placeholder="创建结束日期" name="endTime" value="${endTime}" readonly="true" onclick="WdatePicker({dateFmt:‘yyyy-MM-dd‘})">
                                                <button class="btn btn-sm btn-default" id="queryBtn" style=" margin-left:20px;">查询</button>
                                            </div>
                                        </div>
                                    </form>
                                </div>
                                <!-- /.box-header -->
                                <div class="box-body table-responsive no-padding">
                                    <table class="table table-hover">
                                        <tr>
                                            <th class="col-md-1" style="width: 10%">订单号</th>
                                            <th class="col-md-1" style="width: 6%">买家姓名</th>
                                            <th class="col-md-1" style="width: 8%">买家手机号</th>
                                            <th class="col-md-1" style="width: 6%">卖家姓名</th>
                                            <th class="col-md-1" style="width: 10%">创建时间</th>
                                            <th class="col-md-1" style="width: 8%">订单金额</th>
                                            <th class="col-md-1" style="width: 8%">类型</th>
                                            <th class="col-md-1" style="width:10%">提车时间</th>
                                            <th class="col-md-1" style="width: 8%">状态</th>
                                            <th class="col-md-1" style="width: 15%">操作</th>
                                            <c:if test="${page.items != null}">
                                                <c:forEach items="${page.items}" var="orderinfo">
                                                    <tr>
                                                        <td>${orderinfo.orderNumber }</td>
                                                        <td>${orderinfo.buyName }</td>
                                                        <td>${orderinfo.buyMobile }</td>
                                                        <td>${orderinfo.sellName }</td>
                                                        <td><fmt:formatDate value="${orderinfo.createTime}"
                                                                pattern="yyyy.MM.dd HH:mm:ss" /></td>
                                                        <td>${orderinfo.orderAmt }万元</td>
                                                        <td><c:if test="${orderinfo.orderType==‘1‘}">销车订单
                                                          </c:if> <c:if
                                                                test="${orderinfo.orderType==‘2‘}">普通寻车订单
                                                          </c:if> <c:if
                                                                test="${orderinfo.orderType==‘3‘}">竞价寻车订单
                                                          </c:if></td>
                                                        <td><fmt:formatDate value="${orderinfo.pullCarTime}"
                                                                pattern="yyyy.MM.dd HH:mm:ss" /></td>

                                                        <td><c:if test="${orderinfo.state==‘1‘}">待支付
                                                        </c:if> <c:if
                                                                test="${orderinfo.state==‘2‘}">已冻结订金
                                                        </c:if> <c:if
                                                                test="${orderinfo.state==‘3‘}">已支付订金
                                                        </c:if> <c:if
                                                                test="${orderinfo.state==‘4‘}">已完成
                                                        </c:if> <c:if
                                                                test="${orderinfo.state==‘5‘}">已取消
                                                        </c:if> <c:if
                                                                test="${orderinfo.state==‘6‘}">已删除
                                                        </c:if> <c:if
                                                                test="${orderinfo.state==‘7‘}"><b style="color:red;">纠纷处理中</b>
                                                        </c:if>
                                                         <c:if
                                                                test="${orderinfo.state==‘8‘}"><b style="color:red;">纠纷处理成功</b>
                                                        </c:if></td>
                                                        <td><a
                                                            href="${ctx}/orderinfo/detail?id=${orderinfo.id }"
                                                            class="btn btn-sm btn-primary detailBtn">详细</a>
                                                            <c:if test="${orderinfo.state==‘7‘}"><b style="color:red;"><a
                                                            href="${ctx}/orderinfo/edit?id=${orderinfo.id }"
                                                            class="btn btn-sm btn-primary detailBtn">违约处理</a></b>
                                                        </c:if>
                                                            </td>
                                                    </tr>
                                                </c:forEach>
                                            </c:if>
                                            <c:if test="${empty page.items}">
                                                <tr>
                                                    <td colspan="10"><p class="text-center text-danger">暂无数据</p></td>
                                                </tr>
                                            </c:if>
                                    </table>
                                </div>
                                <!-- /.box-body -->
                                <c:if test="${not empty page.items}">
                                    <div id="pager" class="box-footer clearfix"></div>
                                </c:if>
                            </div>
                            <!-- /.box -->
                        </div>
                    </div>
                </section>
                <!-- /.content -->
            </div>
            <!-- /.content-wrapper -->

            <!-- Modal -->
            <%@ include file="../../main_footer.jsp"%>
        </div>
        <!-- ./wrapper -->

    <!-- jQuery 2.1.4 -->
    <script src="${ctx}/resources/frame/plugins/jQuery/jQuery-2.1.4.min.js"></script>
    <!-- Bootstrap 3.3.2 JS -->
    <script src="${ctx}/resources/frame/bootstrap/js/bootstrap.min.js"
        type="text/javascript"></script>
    <!-- Slimscroll -->
    <script
        src="${ctx}/resources/frame/plugins/slimScroll/jquery.slimscroll.min.js"
        type="text/javascript"></script>
    <!-- FastClick -->
    <script src=‘${ctx}/resources/frame/plugins/fastclick/fastclick.min.js‘></script>
    <!-- AdminLTE App -->
    <script src="${ctx}/resources/frame/dist/js/app.min.js"
        type="text/javascript"></script>
    <!-- AdminLTE for demo purposes -->
    <script src="${ctx}/resources/frame/dist/js/demo.js"
        type="text/javascript"></script>
    <script src="${ctx}/resources/scripts/jquery.pager2.js"
        type="text/javascript"></script>
    <script type="text/javascript"src="${ctx}/resources/scripts/DatePicker/WdatePicker.js"></script>
    <script>
         String.prototype.Trim = function() {
            return this.replace(/(^\s*)|(\s*$)/g, "");
        }
        $(function() {
            PageClick = function(pageclickednumber) {
                $("#pager").pager({
                    pagenumber : pageclickednumber,
                    pagecount : ${page.totalPage},
                    buttonClickCallback : PageClick
                });
                $("#currentPage").val(pageclickednumber);
                $("#queryForm").submit();
            }

            $("#pager").pager({
                pagenumber : ${page.currentPage},
                pagecount : ${page.totalPage},
                buttonClickCallback : PageClick
            });
            //用于查询
            $("#queryBtn").bind("click", function() {
                $("#currentPage").val("1");//给当前页赋值
                $("#queryForm").submit();//提交表单
            });

        });
    </script>
</body>
</html>

ssm框架下的控制层代码

@RequestMapping("list")
    public String list(OrderInfo orderInfo,String startTime,String endTime,
            Model model, Page page,HttpServletRequest request) {
        Map<String, Object> params = new HashMap<String, Object>();
        page.setPageSize(pagesize);
        String orderNumber=request.getParameter("orderNumber");
        String orderType=request.getParameter("orderType");
        params.put("orderNumber", orderNumber);
        params.put("orderType", orderType);
        params.put("endTime", endTime);
        params.put("startTime", startTime);//时间区间查询
        params.put("buyName", orderInfo.getBuyName()); //(用于用户名查询)
        params.put("buyMobile", orderInfo.getBuyMobile()); //(用于手机查询)
        String num = request.getParameter("currentPage");
        if (num != null && !("").equals(num)) {
            page.setCurrentPage(Integer.parseInt(num));
        }
        page = orderInfoService.findByMap(params, page);
        model.addAttribute("page", page);// 查到的值传给页面
        model.addAttribute("orderNumber", orderNumber);
        model.addAttribute("orderType", orderType);
        model.addAttribute("buyName", orderInfo.getBuyName());
        model.addAttribute("buyMobile", orderInfo.getBuyMobile());
        model.addAttribute("endTime", endTime);
        model.addAttribute("startTime", startTime);
        return PATH + "list";
    }
时间: 2024-10-27 02:59:21

关于分页SQL的小总结的相关文章

创建sql自定义的函数及商品分页sql存储过程

--商品筛选时判断品牌ID是否存在 --select dbo.isValite(94,94)create function isValite(@brandId int,@bId int)returns intas begin Declare @rNumber int if @brandId = @bId set @rNumber = 1 else set @rNumber = 0 if @bId = 0 set @rNumber = 1 return @rNumberendgo --判断商品筛选

sql server 小技巧(8) visual studio 2013里使用Sql server compact 4.0

1. 安装 Microsoft SQL Server Compact 4.0  https://www.microsoft.com/zh-cn/download/confirmation.aspx?id=17876   2. 在vs2013里安装 SQL Server Compact & SQLite Toolbox 3.   sql server 小技巧 集锦

IbLpJnJErT分页查询的小例子

锓锩苘院 IbLpJnJErT分页查询的小例子

atitit。mssql sql server 转换mysql 及 分页sql ast的搭建

atitit.mssql sql server 转换mysql  及 分页sql ast的搭建 1. 主要的的转换::函数的转换,分页的转换 1 2. 思路::mssql sql >>ast>>mysql 1 3. sql主要的如下::: 1 4. Mssql2MysqlConvertor (proj:wechatAdm4propt 2 1. 主要的的转换::函数的转换,分页的转换 2. 思路::mssql sql >>ast>>mysql 3. sql主要

Oracle、DB2、SQLSERVER、Mysql、Access分页SQL语句

最近把平时在项目中常用到的数据库分页sql总结了下.大家可以贴出分页更高效的sql语句.sqlserver分页  第一种分页方法 需用到的参数:  pageSize 每页显示多少条数据  pageNumber 页数 从客户端传来  totalRecouds 表中的总记录数 select count (*) from 表名  totalPages 总页数  totalPages=totalRecouds%pageSize==0?totalRecouds/pageSize:totalRecouds/

ORACLE分页SQL

1,使用rownum SELECT * FROM ( SELECT A.*, ROWNUM RN FROM (SELECT * FROM TABLE_NAME) A WHERE ROWNUM <= 40 ) WHERE RN >= 21 2,使用between SELECT * FROM ( SELECT A.*, ROWNUM RN FROM (SELECT * FROM TABLE_NAME) A ) WHERE RN BETWEEN 21 AND 40 ORACLE分页SQL,布布扣,b

SQL SERVER 小技巧

SQL SERVER 小技巧(不用exec实现in()的功能) declare @x varchar(20) SET @x='1,2,3' SELECT @x select * from data0001 where charindex(','+ltrim(rkey)+',',','[email protected]+',')>0 --====================== SQL SERVER 小技巧(多行数据转字符串a,b,c...的写法) declare @Colstr varcha

报表性能优化方案之单数据集分页SQL实现层式报表

1.概述 我们知道,行式引擎按页取数只适用于Oracle,mysql,hsql和sqlserver2008及以上数据库,其他数据库,如access,sqlserver2005,sqlite等必须编写分页SQL. 下面以Access数据库为例介绍需要写分页SQL的数据库怎样利用行式的引擎实现层式报表. 解决方案提供工具:报表开发工具FineReport 2.解决思路 对于mysql这类可以直接使用行式的引擎实现层式报表的数据库来说,如果勾选了行式引擎,程序会自动生成分页sql,如,我新建了一个数据

Oracle数据库和DB2数据库分页SQL的区别举例

--------------------------ORACLE数据库分页SQL举例------------------------------------------------ SELECT * FROM ( SELECT TEMP.*, ROWNUM AS NUM FROM ( SELECT EB.EHR_ID,EB.PERSON_NAME,CL.SVC_ID a,BILL.SVC_ID b,BILL.ITEM_NAME FROM PHIS.EHR_BASE EB, PHIS.SVC_CL