第9章 前端系统的展示

首页后台开发

Dao层的实现

  • 新建一个HeadLineDao接口
public interface HeadLineDao {
    /**
     * 根据传入的条件查询
     * @param headLineCondition
     * @return
     */
    List queryHeadLine(@Param("headLineCondition") HeadLine headLineCondition);
}
  • mapper




    

  • 测试
    发现数据哭着少了一列
ALTER TABLE tb_head_line ADD COLUMN line_img varchar(1000) DEFAULT NULL;

测试单元

    @Test
    public void testQueryArea() {
        HeadLine headLineCondition = new HeadLine();
        List headLineList = headLineDao.queryHeadLine(headLineCondition);
        System.out.println(headLineList.size());
    }
第9章 前端系统的展示_第1张图片
image.png

ShopCategoryDao的改造

  • 支持 parent_Id为空的情况,返回一级level

    
  • 测试
    @Test
    public void testBQueryShopCategory() throws Exception {
        List shopCategoryList = shopCategoryDao.queryShopCategory(null);
        System.out.println(shopCategoryList.size());
    }
  • 测试结果


    第9章 前端系统的展示_第2张图片
    image.png

Service层实现

  • 接口
public interface HeadLineService {
    /**
     * 根据传入的条件返回指定的头条列表
     * @param headLineCondition
     * @return
     * @throws IOException
     */
    List getHeadLineList(HeadLine headLineCondition) throws IOException;
}
  • 实现类
@Service
public class getHeadLineList implements HeadLineService{
    @Autowired
    private HeadLineDao headLineDao;
    
    @Override
    public List getHeadLineList(HeadLine headLineCondition) throws IOException {
        List  headLineList = headLineDao.queryHeadLine(headLineCondition);
        return  headLineList;
    }
}

Controller层的实现

  • 新建在web下frontend包,新建MainPageController类
@Controller
@RequestMapping("/frontend")
public class MainPageController {
    @Autowired
    private ShopCategoryService shopCategoryService;

    @Autowired
    private HeadLineService headLineService;

    /**
     * 初始化前端展示的系统的主页信息 包括获取一级店铺类别列表一级头条
     * @return
     */
    @RequestMapping(value = "/listmainpageinfo", method = RequestMethod.GET)
    @ResponseBody
    private Map listMainPageInfo() {
        Map modelMap = new HashMap<>();
        List shopCategoryList = new ArrayList<>();

        try {
            // 获取一级店铺列表(即parentId为空的ShopCategory)
            shopCategoryList = shopCategoryService.getShopCategoryList(null);
            modelMap.put("shopCategoryList", shopCategoryList);
        } catch (Exception e) {
            modelMap.put("success", false);
            modelMap.put("errMsg", e.getMessage());
            return modelMap;
        }

        List headLineList = new ArrayList<>();

        try {
            // 获取状态为1(可用的头条列表)
            HeadLine headLineCondition = new HeadLine();
            headLineCondition.setEnableStatus(1);
            headLineList = headLineService.getHeadLineList(headLineCondition);
            modelMap.put("headLineList", headLineList);

        } catch (Exception e) {
            modelMap.put("success", false);
            modelMap.put("errMsg", e.getMessage());
            return modelMap;
        }

        modelMap.put("success", true);
        return modelMap;
    }

}
  • 测试


    第9章 前端系统的展示_第3张图片
    image.png

前端页面展示

  • 新建FrontendAdminControlle控制类
@Controller
@RequestMapping("/frontend")
public class FrontendAdminController {

    @RequestMapping(value = "index", method = RequestMethod.GET)
    private String index() {
        return "frontend/index";
    }
}
  • 测试
    注意静态文件的路径
    如果图片不放在静态资源里的话,也可以通过tomcat解析路径
    https://www.cnblogs.com/magic101/p/7756402.html
    第9章 前端系统的展示_第4张图片
    image.png

店铺列表

Dao层的实现

  • 增加通过parent_id查询
    

同理count中也要增加

            
               AND s.shop_category_id IN (
                SELECT
                shop_category_id
                FROM
                tb_shop_category
                WHERE
                parent_id = #{shopCondition.shopCategory.parent.shopCategoryId}
                )
            

Controller层的实现

  • ShopListController
@Controller
@RequestMapping(value ="/frontend")
public class ShopListController {
    @Autowired
    ShopCategoryService shopCategoryService;

    @Autowired
    AreaService areaService;

    @Autowired
    ShopService shopService;

    @RequestMapping(value = "/listshoppageinfo", method = RequestMethod.GET)
    @ResponseBody
    private Map listShopPageInfo(HttpServletRequest request) {
        Map modelMap = new HashMap<>();
        // 试着从前端获取parentId
        long parentId = HttpServletRequestUtil.getLong(request, "parent");
        List shopCategoryList = null;

        if (parentId != -1) {
            try {
                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  areaList = null;

        try {
            // 获取区域列表信息
            areaList = areaService.getAreaList();
            modelMap.put("areaList", areaList);
            modelMap.put("success", true);
        } catch (Exception e) {
            modelMap.put("success", false);
            modelMap.put("errMsg", e.getMessage());
        }

        return modelMap;
    }

    @RequestMapping(value = "/listshops", method = RequestMethod.GET)
    @ResponseBody
    private Map listShops(HttpServletRequest request) {
        Map modelMap = new HashMap();
        int pageIndex = HttpServletRequestUtil.getInt(request, "pageIndex");
        int pageSize = HttpServletRequestUtil.getInt(request, "pageSize");
        if ((pageIndex > -1) && (pageSize > -1)) {
            long parentId = HttpServletRequestUtil.getLong(request, "parentId");
            long shopCategoryId = HttpServletRequestUtil.getLong(request,
                    "shopCategoryId");
            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;
    }

    private Shop compactShopCondition4Search(long parentId,
                                             long shopCategoryId, int areaId, String shopName) {
        Shop shopCondition = new Shop();
        if (parentId != -1L) {
            ShopCategory parentCategory = new ShopCategory();
            parentCategory.setShopCategoryId(parentId);
            // TODO
            // shopCondition.setParentCategory(parentCategory);
        }
        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;
    }
}

-测试


第9章 前端系统的展示_第5张图片
image.png

返回页面

  • 在FrontendAdminController中添加一个返回的页面
    @RequestMapping(value = "shoplist", method = RequestMethod.GET)
    private String shopList() {
    return "frontend/shoplist";
    }
  • 测试


    第9章 前端系统的展示_第6张图片
    image.png

店铺详情页面

Controller层

  • 返回shopdetail页面
    @RequestMapping(value = "shopdetail", method = RequestMethod.GET)
    private String shopDetail() {
        return "frontend/shopdetail";
    }
  • 新建一个ShopDetailController类
@Controller
@RequestMapping("/frontend")
public class ShopDetailController {
    @Autowired
    private ShopService shopService;

    @Autowired
    private ProductService productService;

    @Autowired
    private ProductCategoryService productCategoryService;

    /**
     * 获取店铺信息以及该店铺下的商品列别列表
     * @param request
     * @return
     */
    @RequestMapping(value = "/listshopdetailpageinfo", method = RequestMethod.GET)
    @ResponseBody
    private Map listShopDetailPageInfo(HttpServletRequest request) {

        Map modelMap = new HashMap<>();

        // 获取前台传过来的shopId
        long shopId = HttpServletRequestUtil.getLong(request, "shopId");
        Shop shop = null;
        List productCategoryList = null;

        if (shopId != -1) {
            // 获取店铺Id为shopId的店铺
            shop = shopService.getByShopId(shopId);
            // 获取店铺下面的商品类别列表
            productCategoryList = productCategoryService.getProductCategroyList(shopId);
            modelMap.put("shop", shop);
            modelMap.put("productCategoryList", productCategoryList);
            modelMap.put("success", true);
        } else {
            modelMap.put("success", false);
            modelMap.put("errMsg", "empty shopId");
        }
        return modelMap;
    }

    @RequestMapping(value = "/listproductsbyshop", method = RequestMethod.GET)
    @ResponseBody
    private Map listProductByShop(HttpServletRequest request) {
        Map modelMap = new HashMap<>();

        // 获取页码
        int pageIndex = HttpServletRequestUtil.getInt(request, "pageIndex");
        // 获取一页需要的显示条数
        int pageSize = HttpServletRequestUtil.getInt(request, "pageSize");
        // 获取店铺Id
        long shopId = HttpServletRequestUtil.getLong(request, "shopId");
        // 空值判断
        if ((pageSize > -1) && (pageIndex > -1) && (shopId > -1)) {
            long productCategoryId = HttpServletRequestUtil.getLong(request,
                    "productCategoryId");
            String productName = HttpServletRequestUtil.getString(request,
                    "productName");
            Product productCondition = compactProductCondition4Search(shopId,
                    productCategoryId, productName);
            ProductExecution pe = productService.getProductList(
                    productCondition, pageIndex, pageSize);
            modelMap.put("productList", pe.getProductList());
            modelMap.put("count", pe.getCount());
            modelMap.put("success", true);
        } else {
            modelMap.put("success", false);
            modelMap.put("errMsg", "empty pageSize or pageIndex or shopId");
        }
        return modelMap;
    }

    private Product compactProductCondition4Search(long shopId,
                                                   long productCategoryId, String productName) {
        Product productCondition = new Product();
        Shop shop = new Shop();
        shop.setShopId(shopId);
        productCondition.setShop(shop);
        if (productCategoryId != -1L) {
            ProductCategory productCategory = new ProductCategory();
            productCategory.setProductCategoryId(productCategoryId);
            productCondition.setProductCategory(productCategory);
        }
        if (productName != null) {
            productCondition.setProductName(productName);
        }
        productCondition.setEnableStatus(1);
        return productCondition;
    }    
}
第9章 前端系统的展示_第7张图片
image.png

第9章 前端系统的展示_第8张图片
image.png

你可能感兴趣的:(第9章 前端系统的展示)