程序员社区

【校园商铺SSM-31】店铺列表页面--Dao+Service+Controller层的实现

文章目录

      • 1. ShopDao接口
      • 2. ShopDao.xml实现类
      • 3. 修改与添加数据库中的数据信息
      • 4. ShopDaoTest测试类
      • 5. Controller层的实现

1. ShopDao接口

前面已经写过:

    /**
     * 分页查询店铺,可输入的条件有:店铺名(模糊),店铺状态,店铺类别,区域id,owner
     * @param shopCondition
     * @param rowIndex 从第几行开始取数据
     * @param pageSize 返回的条数
     * @return
     */
    List<Shop> queryShopList(
            @Param("shopCondition")Shop shopCondition,
            @Param("rowIndex")int rowIndex,
            @Param("pageSize")int pageSize);
    /**
     * 返回queryShopList总数
     * @param shopCondition
     * @return
     */
    int queryShopCount(@Param("shopCondition") Shop shopCondition);

2. ShopDao.xml实现类

在ShopDao.xml中添加:
满足当用户点击某个一级商铺类别的时候,加载该商铺类别下全部的商铺:

<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>

在这里插入图片描述

3. 修改与添加数据库中的数据信息

在这里插入图片描述
在这里插入图片描述

4. ShopDaoTest测试类

    @Test
    public void testQueryShopListAndCount(){
        Shop shopCondition = new Shop();

        ShopCategory childCategory = new ShopCategory();
        ShopCategory parentCategory = new ShopCategory();
        parentCategory.setShopCategoryId(1L);
        childCategory.setParent(parentCategory);
        shopCondition.setShopCategory(childCategory);

        List<Shop> shopList =
                shopDao.queryShopList(shopCondition,0,5);
        System.out.println("店铺列表大小:"+shopList.size());
        int count = shopDao.queryShopCount(shopCondition);
        System.out.println("店铺总数:"+count);
    }

5. Controller层的实现

在这里插入图片描述

@Controller
@RequestMapping("/frontend")
public class ShopListController {
    @Autowired
    private AreaService areaService;

    @Autowired
    private ShopCategoryService shopCategoryService;

    @Autowired
    private ShopService shopService;

    @RequestMapping(value = "/listshoppageinfo")
    @ResponseBody
    private Map<String,Object> listShopPageInfo(HttpServletRequest request){
        Map<String,Object> modelMap = new HashMap<>();
        //试着从前端请求中获取parentId
        long parentId = HttpServletRequestUtil.getLong(request, "parentId");
        List<ShopCategory> shopCategoryList =null;
        if(parentId!=-1){
            try {
                //如果parentId存在就取出以及Category目录下面的二级ShopCategory列表
                ShopCategory shopCategoryCondition = new ShopCategory();
                ShopCategory parent = new ShopCategory();
                parent.setShopCategoryId(parentId);
                shopCategoryCondition.setParent(parent);
                shopCategoryList = shopCategoryService.getShopCategoryList(shopCategoryCondition);
            } catch (Exception e) {
                modelMap.put("success",false);
                modelMap.put("errMsg",e.getMessage());
            }
        }else{
            try {
                //如果parentId不存在,就取出所有的一级ShopCategory(用户在首页选择的是全部商店列表)
                shopCategoryList = shopCategoryService.getShopCategoryList(null);
            } catch (Exception e) {
                modelMap.put("success",false);
                modelMap.put("errMsg",e.getMessage());
            }
        }
        modelMap.put("shopCategoryList",shopCategoryList);
        List<Area> areaList = null;
        try{
            areaList = areaService.getAreaList();
            modelMap.put("areaList",areaList);
            modelMap.put("success",true);
            return modelMap;
        }catch (Exception e){
            modelMap.put("success",false);
            modelMap.put("errMsg",e.getMessage());
        }
        return modelMap;
    }
}

在这里插入图片描述
在这里插入图片描述

@RequestMapping(value = "/listshops",method = RequestMethod.GET)
@ResponseBody
private Map<String,Object> listShops(HttpServletRequest request) {
   Map<String, Object> modelMap = new HashMap<>();
   //获取页码
   int pageIndex = HttpServletRequestUtil.getInt(request,"pageIndex");
   //获取一页需要显示的数据条数
   int pageSize = HttpServletRequestUtil.getInt(request,"pageSize");
   //非空判断
   if(pageIndex>-1 && pageSize>-1){
       //试着获取以及类别id
       long parentId = HttpServletRequestUtil.getLong(request,"parentId");
       //试着获取二级类别Id
       long shopCategoryId = HttpServletRequestUtil.getLong(request,"shopCategoryId");
       //获取区域Id
       int areaId = HttpServletRequestUtil.getInt(request,"areaId");
       String shopName = HttpServletRequestUtil.getString(request,"shopName");
       Shop shopCondition = compactShopCondition4Search(parentId,shopCategoryId,areaId,shopName);
       ShopExecution se = shopService.getShopList(shopCondition,pageIndex,pageSize);
       modelMap.put("shopList",se.getShopList());
       modelMap.put("count",se.getCount());
       modelMap.put("success",true);
   }else{
       modelMap.put("success",false);
       modelMap.put("errMsg","empty pageSize or pageIndex");
   }
   return modelMap;
}

/**
* 组合查询条件
* @param parentId
* @param shopCategoryId
* @param areaId
* @param shopName
* @return
*/
private Shop compactShopCondition4Search(long parentId, long shopCategoryId, int areaId, String shopName) {
   Shop shopCondition = new Shop();
   if(parentId!=-1L){
       ShopCategory childCategory = new ShopCategory();
       ShopCategory parentCategory = new ShopCategory();
       parentCategory.setShopCategoryId(parentId);
       childCategory.setParent(parentCategory);
       shopCondition.setShopCategory(childCategory);
   }
   if(shopCategoryId!=-1L){
       ShopCategory shopCategory = new ShopCategory();
       shopCategory.setShopCategoryId(shopCategoryId);
       shopCondition.setShopCategory(shopCategory);
   }
   if(areaId!=-1L){
       Area area = new Area();
       area.setAreaId(areaId);
       shopCondition.setArea(area);
   }

   if(shopName!=null){
       shopCondition.setShopName(shopName);
   }

   shopCondition.setEnableStatus(1);
   return shopCondition;
}

在这里插入图片描述
在这里插入图片描述

赞(0) 打赏
未经允许不得转载:IDEA激活码 » 【校园商铺SSM-31】店铺列表页面--Dao+Service+Controller层的实现

相关推荐

  • 暂无文章

一个分享Java & Python知识的社区