1. 程式人生 > >shop--11.前端展示系統--店鋪列表(後端)

shop--11.前端展示系統--店鋪列表(後端)

ima mod col lmap eat time false ans rom

按照頁面原型設計

  • 點擊全部商店後加載一級商鋪列表,加載對應的數據

  • 點擊特定的一級商鋪列表,加載對應商鋪列表下的數據

  • 區域顯示全部區域

  • 店鋪列表頁面需要支持分頁功能,使用無極滾動的樣式

  • 店鋪列表頁面需要支持多條件排列組合查詢店鋪信息

技術分享圖片

    <select id="queryShopList" resultMap="shopMap">
        SELECT
        s.shop_id,
        s.shop_name,
        s.shop_desc,
        s.shop_addr,
        s.phone,
        s.shop_img,
        s.priority,
        s.create_time,
        s.last_edit_time,
        s.enable_status,
        s.advice,
        a.area_id,
        a.area_name,
        sc.shop_category_id,
        sc.shop_category_name
        FROM
        tb_shop s,
        tb_area a,
        tb_shop_category sc
        
<where> <if test="shopCondition.shopCategory!=null and shopCondition.shopCategory.shopCategoryId!=null"> and s.shop_category_id = #{shopCondition.shopCategory.shopCategoryId} </if> <
if test="shopCondition.shopCategory != null and shopCondition.shopCategory.parent!=null and shopCondition.shopCategory.parent.shopCategoryId!=null"> and s.shop_category_id in (select shop_category_id from tb_shop_category WHERE parent_id = #{shopCondition.shopCategory.parent.shopCategoryId})
</if> <if test="shopCondition.area!=null and shopCondition.shopCategory.areaId!=null"> and shop_area_id = #{shopCondition.area.areaId} </if> <if test="shopCondition.shopName!=null"> and s.shop_name like ‘%${shopCondition.shopName}%‘ </if> <if test="shopCondition.enableStatus!=null"> and s.enable_status = #{shopCondition.enableStatus} </if> <if test="shopCondition.owner!=null and shopCondition.owner.userId!=null"> and s.owner_id = #{shopCondition.owner.userId} </if> AND s.area_id = a.area_id AND s.shop_category_id = sc.shop_category_id </where> ORDER BY s.priority DESC LIMIT #{rowIndex},#{pageSize}; </select> <select id="queryShopCount" resultType="int"> SELECT count(1) FROM tb_shop s, tb_area a, tb_shop_category sc <where> <if test="shopCondition.shopCategory!=null and shopCondition.shopCategory.shopCategoryId!=null"> and s.shop_category_id = #{shopCondition.shopCategory.shopCategoryId} </if> <if test="shopCondition.shopCategory != null and shopCondition.shopCategory.parent!=null and shopCondition.shopCategory.parent.shopCategoryId!=null"> and s.shop_category_id in (select shop_category_id from tb_shop_category WHERE parent_id = #{shopCondition.shopCategory.parent.shopCategoryId}) </if> <if test="shopCondition.area!=null and shopCondition.shopCategory.areaId!=null"> and shop_area_id = #{shopCondition.area.areaId} </if> <if test="shopCondition.shopName!=null"> and s.shop_name like ‘%${shopCondition.shopName}%‘ </if> <if test="shopCondition.enableStatus!=null"> and s.enable_status = #{shopCondition.enableStatus} </if> <if test="shopCondition.owner!=null and shopCondition.owner.userId!=null"> and s.owner_id = #{shopCondition.owner.userId} </if> AND s.area_id = a.area_id AND s.shop_category_id = sc.shop_category_id </where> </select>

為了找出二級目錄

            <if
                test="shopCondition.shopCategory != null
                and shopCondition.shopCategory.parent!=null 
                and shopCondition.shopCategory.parent.shopCategoryId!=null">
                and s.shop_category_id in (select shop_category_id from
                tb_shop_category
                WHERE parent_id =
                #{shopCondition.shopCategory.parent.shopCategotyId})
            </if>

shopListController.java

  1 package com.ryanxu.o2o.web.frontend;
  2 
  3 import java.util.ArrayList;
  4 import java.util.HashMap;
  5 import java.util.List;
  6 import java.util.Map;
  7 
  8 import javax.servlet.http.HttpServletRequest;
  9 
 10 import org.springframework.beans.factory.annotation.Autowired;
 11 import org.springframework.stereotype.Controller;
 12 import org.springframework.web.bind.annotation.RequestMapping;
 13 import org.springframework.web.bind.annotation.RequestMethod;
 14 import org.springframework.web.bind.annotation.ResponseBody;
 15 
 16 import com.ryanxu.o2o.dto.ShopExecution;
 17 import com.ryanxu.o2o.entity.Area;
 18 import com.ryanxu.o2o.entity.Shop;
 19 import com.ryanxu.o2o.entity.ShopCategory;
 20 import com.ryanxu.o2o.service.AreaService;
 21 import com.ryanxu.o2o.service.ShopCategoryService;
 22 import com.ryanxu.o2o.service.ShopService;
 23 import com.ryanxu.o2o.util.HttpServletRequestUtil;
 24 
 25 @Controller
 26 @RequestMapping(value="/frontend")
 27 public class ShopListController {
 28     @Autowired
 29     private AreaService areaService;
 30     @Autowired
 31     private ShopCategoryService shopCategoryService;
 32     @Autowired
 33     private ShopService shopService;
 34     
 35     /**
 36      * 返回商品列表頁裏的ShopCategory列表(二級或一級),以及區域信息列表
 37      * 
 38      * @param request
 39      * @return
 40      */
 41     @RequestMapping(value = "/listshopspageinfo",method=RequestMethod.GET)
 42     @ResponseBody
 43     private Map<String, Object> listShopsPageInfo(HttpServletRequest request){
 44         Map<String, Object> modelMap = new HashMap<String,Object>();
 45         //試著從前端請求中獲取parentId
 46         long parentId = HttpServletRequestUtil.getLong(request, "parentId");
 47         List<ShopCategory> shopCategoryList = new ArrayList<ShopCategory>();
 48         
 49         if(parentId != -1) {
 50             //如果存在parentId,則取出該一級ShopCategory下的二級ShopCategory列表
 51             try {
 52                 ShopCategory shopCategoryCondition = new ShopCategory();
 53                 ShopCategory parent = new ShopCategory();
 54                 parent.setShopCategoryId(parentId);
 55                 shopCategoryCondition.setParent(parent);
 56                 shopCategoryList = shopCategoryService.getShopCategoryList(shopCategoryCondition);
 57             }catch (Exception e) {
 58                 modelMap.put("success", false);
 59                 modelMap.put("errMsg", e.getMessage());
 60             }
 61         }else {
 62             try {
 63                 //如果parentId不存在,則取出所有一級ShopCategory(用戶在首頁選擇的是全部商品列表)
 64                 shopCategoryList = shopCategoryService.getShopCategoryList(null);
 65             }catch (Exception e) {
 66                 modelMap.put("success", false);
 67                 modelMap.put("errMsg", e.getMessage());
 68             }
 69         }
 70         modelMap.put("shopCategoryList", shopCategoryList);
 71         List<Area> areaList = null;
 72         try {
 73             //獲取區域列表信息
 74             areaList = areaService.getAreaList();
 75             modelMap.put("areaList", areaList);
 76             modelMap.put("success", true);
 77             return modelMap;
 78         }catch (Exception e) {
 79             modelMap.put("success", false);
 80             modelMap.put("errMsg", e.getMessage());
 81         }
 82         return modelMap;
 83     }
 84     
 85     /**
 86      * 獲取指定查詢條件下的店鋪列表
 87      * 
 88      * @param request
 89      * @return
 90      */
 91     @RequestMapping(value="/listshops",method = RequestMethod.GET)
 92     @ResponseBody
 93     private Map<String, Object> listShops(HttpServletRequest request){
 94         Map<String, Object> modelMap = new HashMap<String,Object>();
 95         //獲取頁碼
 96         int pageIndex = HttpServletRequestUtil.getInt(request, "pageIndex");
 97         //獲取一頁所需顯示的數據條數
 98         int pageSize = HttpServletRequestUtil.getInt(request, "pageSize");
 99         //非空判斷
100         if((pageIndex > -1)&&(pageSize > -1)) {
101             //試著獲取一級類別Id
102             long parentId = HttpServletRequestUtil.getLong(request, "parentId");
103             //試著獲取特定二級類別Id
104             long shopCategoryId = HttpServletRequestUtil.getLong(request, "shopCategoryId");
105             //試著獲取區域Id
106             int areaId = HttpServletRequestUtil.getInt(request, "areaId");
107             //試著獲取模糊查詢的名字
108             String shopName = HttpServletRequestUtil.getString(request, "shopName");
109             //獲取組合之後的查詢條件
110             Shop shopCondition = compactShopCondition4Search(parentId,shopCategoryId,areaId,shopName);
111             //根據查詢條件和分頁信息獲取店鋪列表,並返回總數
112             ShopExecution se = shopService.getShopList(shopCondition, pageIndex, pageSize);
113             modelMap.put("shopList", se.getShopList());
114             modelMap.put("count", se.getCount());
115             modelMap.put("success", true);
116         }else {
117             modelMap.put("success", false);
118             modelMap.put("errMsg", "empty pageSize or pageIndex");
119         }
120         return modelMap;
121     }
122 
123     /**
124      * 組合查詢條件,並將條件封裝到ShopCondition對象裏返回
125      * 
126      * @param parentId
127      * @param shopCategoryId
128      * @param areaId
129      * @param shopName
130      * @return
131      */
132     private Shop compactShopCondition4Search(long parentId, long shopCategoryId, int areaId, String shopName) {
133         Shop shopCondition = new Shop();
134         if(parentId != -1L) {
135             //查詢某一個一級ShopCategory下面的所有二級ShopCategory裏面的店鋪列表
136             ShopCategory childCategory = new ShopCategory();
137             ShopCategory parentCategory = new ShopCategory();
138             parentCategory.setShopCategoryId(parentId);
139             childCategory.setParent(parentCategory);
140             shopCondition.setShopCategory(childCategory);
141         }
142         if(shopCategoryId != -1L) {
143             //查詢某個二級ShopCategory下面的店鋪列表
144             ShopCategory shopCategory = new ShopCategory();
145             shopCategory.setShopCategoryId(shopCategoryId);
146             shopCondition.setShopCategory(shopCategory);
147         }
148         if(areaId != -1L) {
149             //查詢位於某個區域Id下的店鋪列表
150             Area area = new Area();
151             area.setAreaId(areaId);
152             shopCondition.setArea(area);
153         }
154         if(shopName != null ) {
155             //查詢名字裏包含shopName的店鋪列表
156             shopCondition.setShopName(shopName);
157         }
158         //前端展示的店鋪都是審核通過的店鋪
159         shopCondition.setEnableStatus(1);
160         return shopCondition;
161     }
162 }

第二個if相當於這樣 shop表裏的

技術分享圖片

shop--11.前端展示系統--店鋪列表(後端)