1.流程分析
首页 ——————ajax——————> 首页商品推荐
index.html <——销量最高的三个商品——— 接口
推荐规则:1.根据当前用户的最近搜索进行推荐
2.查询销量最高的商品进行推荐
3.根据平台管理员设置的需要推荐的商品
4.查询最新上架的商品进行推荐
2.数据库实现
商品推荐算法:推荐最新上架的商品
说明:商品推荐算法是根据多个维度进行权重计算,计算出一个匹配值
(1)数据表分析及数据准备
(2)sql
#连接查询: #商品推荐:查询最新上架的商品 #通过创建时间排序查找product表,只查询前三条 SELECT * FROM product p INNER JOIN product_img pimg ON p.product_id = pimg.item_id ORDER BY p.create_time DESC LIMIT 0,3 #子查询: #商品推荐:查询最新上架的商品 SELECT * FROM product ORDER BY create_time DESC LIMIT 0,3; #子查询:根据商品id查询商品图片 SELECT * FROM product_img WHERE item_id=2;
(3)在beans子工程entity包创建ProductVO,相比较Product新增了List<ProductImg> imgs,用于存储商品的图片。
public class ProductVO { /** * 商品主键id */ @Id @Column(name = "product_id") private String productId; /** * 商品名称 商品名称 */ @Column(name = "product_name") private String productName; /** * 分类外键id 分类id */ @Column(name = "category_id") private Integer categoryId; /** * 一级分类外键id 一级分类id,用于优化查询 */ @Column(name = "root_category_id") private Integer rootCategoryId; /** * 销量 累计销售 */ @Column(name = "sold_num") private Integer soldNum; /** * 默认是1,表示正常状态, -1表示删除, 0下架 默认是1,表示正常状态, -1表示删除, 0下架 */ @Column(name = "product_status") private Integer productStatus; /** * 创建时间 */ @Column(name = "create_time") private Date createTime; /** * 更新时间 */ @Column(name = "update_time") private Date updateTime; private List<ProductImg> imgs; public List<ProductImg> getImgs() { return imgs; } public void setImgs(List<ProductImg> imgs) { this.imgs = imgs; } /** * 商品内容 商品内容 */ private String content; /** * 获取商品主键id * * @return product_id - 商品主键id */ public String getProductId() { return productId; } /** * 设置商品主键id * * @param productId 商品主键id */ public void setProductId(String productId) { this.productId = productId; } /** * 获取商品名称 商品名称 * * @return product_name - 商品名称 商品名称 */ public String getProductName() { return productName; } /** * 设置商品名称 商品名称 * * @param productName 商品名称 商品名称 */ public void setProductName(String productName) { this.productName = productName; } /** * 获取分类外键id 分类id * * @return category_id - 分类外键id 分类id */ public Integer getCategoryId() { return categoryId; } /** * 设置分类外键id 分类id * * @param categoryId 分类外键id 分类id */ public void setCategoryId(Integer categoryId) { this.categoryId = categoryId; } /** * 获取一级分类外键id 一级分类id,用于优化查询 * * @return root_category_id - 一级分类外键id 一级分类id,用于优化查询 */ public Integer getRootCategoryId() { return rootCategoryId; } /** * 设置一级分类外键id 一级分类id,用于优化查询 * * @param rootCategoryId 一级分类外键id 一级分类id,用于优化查询 */ public void setRootCategoryId(Integer rootCategoryId) { this.rootCategoryId = rootCategoryId; } /** * 获取销量 累计销售 * * @return sold_num - 销量 累计销售 */ public Integer getSoldNum() { return soldNum; }
(4)mapper接口定义操作方法
ProductMapper :
public interface ProductMapper extends GeneralDAO<Product> { public List<ProductVO> selectRecommendProducts(); }
ProductImgMapper :
public interface ProductImgMapper extends GeneralDAO<ProductImg> {
//根据商品id查询当前商品的图片信息 public List<ProductImg> selectProductImgByProductId(int productId); }
ProductMapper :
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.qfedu.fmmall.dao.ProductMapper"> <resultMap id="BaseResultMap" type="com.qfedu.fmmall.entity.Product"> <id column="product_id" jdbcType="VARCHAR" property="productId" /> <result column="product_name" jdbcType="VARCHAR" property="productName" /> <result column="category_id" jdbcType="INTEGER" property="categoryId" /> <result column="root_category_id" jdbcType="INTEGER" property="rootCategoryId" /> <result column="sold_num" jdbcType="INTEGER" property="soldNum" /> <result column="product_status" jdbcType="INTEGER" property="productStatus" /> <result column="create_time" jdbcType="TIMESTAMP" property="createTime" /> <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" /> <result column="content" jdbcType="LONGVARCHAR" property="content" /> </resultMap> <resultMap id="ProductVOMap" type="com.qfedu.fmmall.entity.ProductVO"> <id column="product_id" jdbcType="VARCHAR" property="productId" /> <result column="product_name" jdbcType="VARCHAR" property="productName" /> <result column="category_id" jdbcType="INTEGER" property="categoryId" /> <result column="root_category_id" jdbcType="INTEGER" property="rootCategoryId" /> <result column="sold_num" jdbcType="INTEGER" property="soldNum" /> <result column="product_status" jdbcType="INTEGER" property="productStatus" /> <result column="create_time" jdbcType="TIMESTAMP" property="createTime" /> <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" /> <result column="content" jdbcType="LONGVARCHAR" property="content" /> <collection property="imgs" select="com.qfedu.fmmall.dao.ProductImgMapper.selectProductImgByProductId" column="product_id"/> </resultMap> <select id="selectRecommendProducts" resultMap="ProductVOMap"> SELECT product_id, product_name, category_id, root_category_id, sold_num, product_status, content, create_time, update_time FROM product ORDER BY create_time DESC LIMIT 0,3; </select> </mapper>
ProductImgMapper :
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.qfedu.fmmall.dao.ProductImgMapper"> <resultMap id="BaseResultMap" type="com.qfedu.fmmall.entity.ProductImg"> <id column="id" jdbcType="VARCHAR" property="id" /> <result column="item_id" jdbcType="VARCHAR" property="itemId" /> <result column="url" jdbcType="VARCHAR" property="url" /> <result column="sort" jdbcType="INTEGER" property="sort" /> <result column="is_main" jdbcType="INTEGER" property="isMain" /> <result column="created_time" jdbcType="TIMESTAMP" property="createdTime" /> <result column="updated_time" jdbcType="TIMESTAMP" property="updatedTime" /> </resultMap> <select id="selectProductImgByProductId" resultMap="BaseResultMap"> SELECT id, item_id, url, sort, is_main, created_time, updated_time FROM product_img WHERE item_id=#{productId}; </select> </mapper>
测试:
@RunWith(SpringRunner.class) @SpringBootTest(classes = ApiApplication.class) public class ApiApplicationTests { @Autowired private ProductMapper productMapper; @Test public void testRecommend(){ List<ProductVO> productVOS = productMapper.selectRecommendProducts(); for (ProductVO p:productVOS) { System.out.println(p); } } }
3.业务层实现
ProductService
public interface ProductService { public ResultVO listRecommendProducts(); }
ProductServiceImpl
@Service public class ProductServiceImpl implements ProductService { @Autowired private ProductMapper productMapper; public ResultVO listRecommendProducts() { List<ProductVO> productVOS = productMapper.selectRecommendProducts(); ResultVO resultVO = new ResultVO(ResStatus.OK, "success", productVOS); return resultVO; } }
IndexController:
@RestController @CrossOrigin @RequestMapping("/index") @Api(value = "提供首页数据显示所需的接口",tags = "首页管理") public class IndexController { @Autowired private IndexImgService indexImgService; @Autowired private CategoryService categoryService; @Autowired private ProductService productService; @GetMapping("/indeximg") @ApiOperation("首页轮播图接口") public ResultVO listIndexImgs(){ return indexImgService.listIndexImgs(); } @GetMapping("/category-list") @ApiOperation("商品分类查询接口") public ResultVO listCategory(){ return categoryService.listCategories(); } @GetMapping("/list-recommends") @ApiOperation("查询推荐商品的接口") public ResultVO listRecommendProducts(){ return productService.listRecommendProducts(); } }
原创文章,作者:,如若转载,请注明出处:https://blog.ytso.com/275389.html