《锋迷商城》系列项目
链接: 《一》 项目搭建
链接: 《二》数据库的创建
链接: 《三》业务流程设计
链接: 《四》业务流程实现:用户管理
链接: 《五》逆向工程
链接: 《六》用户认证
十一、首页-商品分类推荐
按照商品的分类(一级分类)推荐销量最高的6个商品
11.1 实现流程分析
加载分类商品推荐的两种实现方案:
方案一:当加载首页面时不加载分类推荐商品,监听进度条滚动事件,当进度条触底(滚动指定的距离)就触发商品的加载每次只加载一个分类的商品
方案二: 一次性加载所有分类的推荐商品,整体进行初始化
11.2 接口开发
11.2.1 数据库和DAO层实现
-
SQL
-- 查询一级分类 select category_id, category_name, category_level, parent_id, category_icon, category_slogan, category_pic, category_bg_color FROM category where category_level =1 -- 查询每个分类下销量前六的商品 select product_id, product_name, category_id, root_category_id, sold_num, product_status, content, create_time, update_time from product where root_category_id=2 ORDER BY sold_num desc LIMIT 0,6 -- 查询每个商品的图片 select * FROM product_img where item_id=1
-
在CategoryVO中添加
//实现首页的分类商品推荐 private List<ProductVO> products;
-
DAO层接口
-
CategoryMapper 接口 添加
/** * 查询一级类别 * @return */ List<CategoryVO> selectFirstLevelCategories();
-
ProductMapper 接口添加
/** * 查询分类下销量最高的6个产品 * @param cid * @return */ List<ProductVO> selectTop6ByCategory(int cid);
-
-
Mapper.xml映射文件
-
ProductMapper .xml添加
<select id="selectTop6ByCategory" resultMap="ProductVOMap"> select product_id, product_name, category_id, root_category_id, sold_num, product_status, content, create_time, update_time from product where root_category_id=#{cid} ORDER BY sold_num desc LIMIT 0,6 </select>
-
CategoryMapper .xml 添加
<resultMap id="CategoryVOMap3" type="com.sjtest.fmmall.entity.CategoryVO"> <id column="category_id" jdbcType="INTEGER" property="categoryId"/> <result column="category_name" jdbcType="VARCHAR" property="categoryName"/> <result column="category_level" jdbcType="INTEGER" property="categoryLevel"/> <result column="parent_id" jdbcType="INTEGER" property="parentId"/> <result column="category_icon" jdbcType="VARCHAR" property="categoryIcon"/> <result column="category_slogan" jdbcType="VARCHAR" property="categorySlogan"/> <result column="category_pic" jdbcType="VARCHAR" property="categoryPic"/> <result column="category_bg_color" jdbcType="VARCHAR" property="categoryBgColor"/> <collection property="products" column="category_id" select="com.sjtest.fmmall.dao.ProductMapper.selectTop6ByCategory"/> </resultMap> <select id="selectFirstLevelCategories" resultMap="CategoryVOMap3"> select category_id, category_name, category_level, parent_id, category_icon, category_slogan, category_pic, category_bg_color FROM category where category_level =1 </select>
-
11.2.2 业务层实现
/**
* 查询一级分类类别 同时查询当前分类下销量最高的六个商品
* @return
*/
ResultVO selectFirstLevelCategories();
-----------------------------------------------
@Override
public ResultVO selectFirstLevelCategories() {
List<CategoryVO> categoryVOS = categoryMapper.selectFirstLevelCategories();
ResultVO resultVO = new ResultVO(ResStatus.OK, "success", categoryVOS);
return resultVO;
}
11.2.3 控制层实现
在indexController中添加
@GetMapping("/category-recommends")
@ApiOperation("分类推荐查询接口")
public ResultVO listRecommendProductsByCategory(){
return categoryService.selectFirstLevelCategories();
}