可以看到,我们查询到7条数据,总页数totalPage为2,并且根据价格从小到大进行了排序,证明我们的编码是正确的。接下来,通过相同的代码逻辑,我们继续实现根据搜索关键词进行查询。
根据关键词查询 Response DTO 实现使用上面实现的com.liferunner.dto.SearchProductDTO.
Custom Mapper 实现在com.liferunner.custom.ProductCustomMapper中新增方法:
List<SearchProductDTO> searchProductList(@Param("paramMap") Map<String, Object> paramMap);在mapper/custom/ProductCustomMapper.xml中添加查询SQL:
<select resultType="com.liferunner.dto.SearchProductDTO" parameterType="Map"> SELECT p.id as productId, p.product_name as productName, p.sell_counts as sellCounts, pi.url as imgUrl, tp.priceDiscount FROM products p LEFT JOIN products_img pi ON p.id = pi.product_id LEFT JOIN ( SELECT product_id, MIN(price_discount) as priceDiscount FROM products_spec GROUP BY product_id ) tp ON tp.product_id = p.id WHERE pi.is_main = 1 <if test="paramMap.keyword != null and paramMap.keyword != ''"> AND p.item_name LIKE "%${paramMap.keyword}%" </if> ORDER BY <choose> <when test="paramMap.sortby != null and paramMap.sortby == 'sell'"> p.sell_counts DESC </when> <when test="paramMap.sortby != null and paramMap.sortby == 'price'"> tp.priceDiscount ASC </when> <otherwise> p.created_time DESC </otherwise> </choose> </select> Service 实现在com.liferunner.service.IProductService中新增查询接口:
/** * 查询商品列表 * * @param keyword 查询关键词 * @param sortby 排序方式 * @param pageNumber 当前页码 * @param pageSize 每页展示多少条数据 * @return 通用分页结果视图 */ CommonPagedResult searchProductList(String keyword, String sortby, Integer pageNumber, Integer pageSize);在com.liferunner.service.impl.ProductServiceImpl实现上述接口方法:
@Override public CommonPagedResult searchProductList(String keyword, String sortby, Integer pageNumber, Integer pageSize) { Map<String, Object> paramMap = new HashMap<>(); paramMap.put("keyword", keyword); paramMap.put("sortby", sortby); // mybatis-pagehelper PageHelper.startPage(pageNumber, pageSize); val searchProductDTOS = this.productCustomMapper.searchProductList(paramMap); // 获取mybatis插件中获取到信息 PageInfo<?> pageInfo = new PageInfo<>(searchProductDTOS); // 封装为返回到前端分页组件可识别的视图 val commonPagedResult = CommonPagedResult.builder() .pageNumber(pageNumber) .rows(searchProductDTOS) .totalPage(pageInfo.getPages()) .records(pageInfo.getTotal()) .build(); return commonPagedResult; }上述方法和之前searchProductList(Integer categoryId, String sortby, Integer pageNumber, Integer pageSize)唯一的区别就是它是肯定搜索关键词来进行数据查询,使用重载的目的是为了我们后续不同类型的业务扩展而考虑的。
Controller 实现在com.liferunner.api.controller.ProductController中添加关键词搜索API:
@GetMapping("/search") @ApiOperation(value = "查询商品信息列表", notes = "查询商品信息列表") public JsonResponse searchProductList( @ApiParam(name = "keyword", value = "搜索关键词", required = true) @RequestParam String keyword, @ApiParam(name = "sortby", value = "排序方式", required = false) @RequestParam String sortby, @ApiParam(name = "pageNumber", value = "当前页码", required = false, example = "1") @RequestParam Integer pageNumber, @ApiParam(name = "pageSize", value = "每页展示记录数", required = false, example = "10") @RequestParam Integer pageSize ) { if (StringUtils.isBlank(keyword)) { return JsonResponse.errorMsg("搜索关键词不能为空!"); } if (null == pageNumber || 0 == pageNumber) { pageNumber = DEFAULT_PAGE_NUMBER; } if (null == pageSize || 0 == pageSize) { pageSize = DEFAULT_PAGE_SIZE; } log.info("============根据关键词:{} 搜索列表==============", keyword); val searchResult = this.productService.searchProductList(keyword, sortby, pageNumber, pageSize); return JsonResponse.ok(searchResult); } Test API测试参数:keyword : 西凤,sortby : sell,pageNumber : 1,pageSize : 10
根据销量排序正常,查询关键词正常,总条数32,每页10条,总共3页正常。 福利讲解
在本节编码实现中,我们使用到了一个通用的mybatis分页插件mybatis-pagehelper,接下来,我们来了解一下这个插件的基本情况。
mybatis-pagehelper