7.查询热销商品前四名的实现
1.数据库

1 CREATE TABLE t_product ( 2 id int(20) NOT NULL COMMENT '商品id', 3 category_id int(20) DEFAULT NULL COMMENT '分类id', 4 item_type varchar(100) DEFAULT NULL COMMENT '商品系列', 5 title varchar(100) DEFAULT NULL COMMENT '商品标题', 6 sell_point varchar(150) DEFAULT NULL COMMENT '商品卖点', 7 price bigint(20) DEFAULT NULL COMMENT '商品单价', 8 num int(10) DEFAULT NULL COMMENT '库存数量', 9 image varchar(500) DEFAULT NULL COMMENT '图片路径', 10 status int(1) DEFAULT '1' COMMENT '商品状态 1:上架 2:下架 3:删除', 11 priority int(10) DEFAULT NULL COMMENT '显示优先级', 12 created_time datetime DEFAULT NULL COMMENT '创建时间', 13 modified_time datetime DEFAULT NULL COMMENT '最后修改时间', 14 created_user varchar(50) DEFAULT NULL COMMENT '创建人', 15 modified_user varchar(50) DEFAULT NULL COMMENT '最后修改人', 16 PRIMARY KEY (id) 17 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2.product实体类

package com.ku.store.entity; import lombok.Data; @Data public class Product extends BaseEntity{ private Integer id; private Integer categoryId;//类别id private String itemType;//项目 private String title; private String sellPoint;//销售点 private Long price; private Integer num; private String image; private Integer status; private Integer priority;//优先权 }
3.mapper层
3.1.接口实现
/** * 查询热销商品前四名 * @return 热销商品前四名的集合 */ List<Product> findHostList();
3.2.接口的xml实现

<resultMap id="ProductEntityMap" type="com.ku.store.entity.Product"> <id column="id" property="id"/> <result column="category_id" property="categoryId"/> <result column="item_type" property="itemType"/> <result column="sell_point" property="sellPoint"/> <result column="created_user" property="createdUser"/> <result column="created_time" property="createdTime"/> <result column="modified_user" property="modifiedUser"/> <result column="modified_time" property="modifiedTime"/> </resultMap> <!-- 查询热销商品的前四名:List<Product> findHostList() --> <select id="findHostList" resultMap="ProductEntityMap"> select * from store.t_product where status = 1 order by priority desc limit 0,4 </select>
3.3.测试接口方法
@Autowired ProductMapper productMapper; @Test public void findHostList(){ List<Product> hostList = productMapper.findHostList(); System.out.println("count:"+hostList.size()); for (Product product : hostList) { System.out.println(product); } }
3.4.测试结果

4.service层
4.1.接口实现
List<Product> getHostList();
4.2.接口实现类

@Service public class ProductServiceImpl implements IProductService { @Autowired private ProductMapper productMapper; @Override public List<Product> getHostList() { List<Product> list = productMapper.findHostList(); for (Product product : list) { product.setCreatedUser(null); product.setCreatedTime(null); product.setModifiedUser(null); product.setModifiedTime(null); } return list; } }
4.3.实现类测试

@Autowired IProductService productService; @Test public void getHostList(){ try { List<Product> list = productService.getHostList(); System.out.println("count=" + list.size()); for (Product item : list) { System.out.println(item); } } catch (ServiceException e) { System.out.println(e.getClass().getSimpleName()); System.out.println(e.getMessage()); } }
4.4.测试结果
5.controller层
5.1.增强BaseController
5.2.实现ProductController

@RestController @RequestMapping("/products") public class ProductController extends BaseController{ @Autowired private IProductService productService; @GetMapping("hot_list") public JsonResult<List<Product>> getHotList(){ List<Product> data = productService.getHostList(); return new JsonResult<>(OK, data); } }
5.3.postman测试

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!