码迷,mamicode.com
首页 > 数据库 > 详细

关于分页SQL的小总结

时间:2017-04-12 23:48:33      阅读:542      评论:0      收藏:0      [点我收藏+]

标签:attribute   bar   创建时间   eid   页面传值   stat   arc   min   icons   

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";
    }

 

关于分页SQL的小总结

标签:attribute   bar   创建时间   eid   页面传值   stat   arc   min   icons   

原文地址:http://www.cnblogs.com/xieCong/p/6701489.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!