shop--7.店鋪編輯和列表--店鋪列表 分頁查詢,模糊查詢--dao層實現
在service層中, 有一個問題,就是:
dao層中的分頁查詢傳參,傳入的是rowIndex(從第幾行開始查詢), pageSize(查詢多少條記錄)
而在service層中分頁查詢傳的是pageIndex(第幾頁),pageSize
所以在第一個參數要有一個轉換才可以
所以編寫一個工具類,將pageIndex轉為rowIndex
public class PageCalculator { /** * 將pageIndex(第幾頁)轉換為查詢結果中的第幾行rowIndex * @param pageIndex * @param pageSize * @return */ public static int calculateRowIndex(int pageIndex, int pageSize){ return (pageIndex > 0) ? (pageIndex - 1) * pageSize : 0; } }
service接口
/** * 實現分頁查詢店鋪,通過條件組合,來篩選出條件范圍內的店鋪列表 * @param shopCondition * @param pageIndex 第幾頁 * @param pageSize 一頁中數據的數量 * @return ShopExecution 其中包含shopList和count */ public ShopExecution getShopList(Shop shopCondition, int pageIndex, int pageSize);
serviceImpl
public ShopExecution getShopList(Shop shopCondition, int pageIndex, int pageSize) { int rowIndex = PageCalculator.calculateRowIndex(pageIndex, pageSize); List<Shop> shopList = shopDao.queryShopList( shopCondition, rowIndex, pageSize); int count = shopDao.queryShopCount( shopCondition ); ShopExecution shopExecution = new ShopExecution( ); if(shopList != null){ shopExecution.setShopList( shopList ); shopExecution.setCount( count ); }else{ shopExecution.setState(ShopStateEnum.INNER_ERROR.getState()); } return shopExecution; }
controller層實現
/** * 實現分頁查詢店鋪,通過條件組合,來篩選出條件范圍內的店鋪列表 * @param request * @return */ @RequestMapping(value="/getshoplist", method=RequestMethod.GET) @ResponseBody public Map<String, Object> getShopList(HttpServletRequest request){ Map<String, Object> modelMap = new HashMap<>(); PersonInfo user = new PersonInfo(); user.setUserId( 1L ); ShopExecution shopExecution = null; try{ Shop shopCondition = new Shop(); shopCondition.setOwner( user ); shopCondition.setShopName("f"); Area area = new Area(); area.setAreaId( 2 ); shopCondition.setArea(area); shopExecution = shopService.getShopList( shopCondition, 1, 100 ); modelMap.put( "shopList", shopExecution.getShopList() ); modelMap.put( "user", user); modelMap.put( "success", true ); } catch(Exception e){ modelMap.put( "success", false ); modelMap.put( "errMsg", e.toString() ); } return modelMap; }
這個不知道是做什么的
/** *管理session相關的 * @param request * @return */ @RequestMapping(value="/getshopmanagementinfo", method=RequestMethod.GET) @ResponseBody public Map<String, Object> getShopManagementInfo(HttpServletRequest request){ Map<String, Object> modelMap = new HashMap<>(); long shopId = (long) request.getSession().getAttribute("shopId"); //如果shopId不存在 if(shopId <= 0){ //判斷當前有沒有登錄 Object currentShopObj = request.getSession().getAttribute("currentShop"); //如果當前既沒有shopId傳入,也沒有店鋪登錄的話,就重定向到店鋪列表頁面 if(currentShopObj == null){ modelMap.put( "redirect", true ); modelMap.put( "url", "/shop/shoplist" ); }else{ //如果當前有店鋪登錄的話 Shop currentShop = (Shop)currentShopObj; modelMap.put( "redirect", false ); modelMap.put( "shopId", currentShop.getShopId()); } }else{ //如果當前有shopId Shop currentShop = new Shop(); currentShop.setShopId(shopId); request.getSession().setAttribute("currentShop", currentShop); modelMap.put( "redirect", false ); } return modelMap; }