集中问几个问题
来源:8-8 前台商品详情,列表,搜索,动态排序功能开发

慕容0166988
2018-03-12
public ServerResponse<PageInfo> getProductByKeywordCategory(String keyword,Integer categoryId,int pageNum,int pageSize,String orderBy){
if(StringUtils.isBlank(keyword) && categoryId == null){
return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getDesc());
}
List<Integer> categoryIdList = new ArrayList<Integer>();
/*
问题1?如果我在这个地址栏中传入id/kewod同时,
并且这个keywoed存在,id也是存在的,我就没有找到这个对应的判断条件是哪个,但是我在地址栏试过了
不行,我就很好奇这是那步起到了这个作用?
*/
if(categoryId != null){
Category category = categoryMapper.selectByPrimaryKey(categoryId);
/*
问题2?这里为什么还要判断关键字,起到什么作用?是所谓的怕想我问题1那样如果
传入id不存在传入关键字,可是我试了好像不行为什么啊?
*/
if(category == null && StringUtils.isBlank(keyword)){
//没有该分类,并且还没有关键字,这个时候返回一个空的结果集,不报错
PageHelper.startPage(pageNum,pageSize);
List<ProductListVo> productListVoList = Lists.newArrayList();
PageInfo pageInfo = new PageInfo(productListVoList);
return ServerResponse.createBySuccess(pageInfo);
}
categoryIdList = iCategoryService.selectCategoryAndChildrenById(category.getId()).getData();
}
//问题3:这个append是干什么?为甚要把keywod放在里面
if(StringUtils.isNotBlank(keyword)){
keyword = new StringBuilder().append("%").append(keyword).append("%").toString();
}
PageHelper.startPage(pageNum,pageSize);
//排序处理
if(StringUtils.isNotBlank(orderBy)){
if(Const.ProductListOrderBy.PRICE_ASC_DESC.contains(orderBy)){
//问题4:这一步也不太理解要做什么?
String[] orderByArray = orderBy.split("_");
PageHelper.orderBy(orderByArray[0]+" "+orderByArray[1]);
}
}
List<Product> productList = productMapper.selectByNameAndCategoryIds(StringUtils.isBlank(keyword)?null:keyword,categoryIdList.size()==0?null:categoryIdList);
List<ProductListVo> productListVoList = Lists.newArrayList();
for(Product product : productList){
ProductListVo productListVo = assembleProductListVo(product);
productListVoList.add(productListVo);
}
PageInfo pageInfo = new PageInfo(productList);
pageInfo.setList(productListVoList);
return ServerResponse.createBySuccess(pageInfo);
}
2回答
-
Geely
2018-03-13
你好,同学,这是业务问题,另外还是要表扬一下 @艾贺521 互相帮助精神可嘉~~而且回答的也蛮ok的,同学稍安勿躁哈。
简单整理一下
问题1:同时输入的话,往下看,首先判断分类,开始拼接sql,如果关键字不为空,也继续拼接。所以这两个条件是可以都命中的
问题2:这个位置不是判断关键字,是判断分类ID,是否存在因为分类id有可能通过拼接传DB里不存在的,所以要做空校验
问题3:append是一个api,拼接字符串所用。整体拼接出来就是一个sql
问题4:动态排序,拼接 order by 所用。
00 -
艾贺521
2018-03-12
都是好基础的问题。
问题1:如果不走if,直接走接下来的逻辑,你说的是没有进入if的。。。
问题2:和问题1差不多,你整理一下逻辑思路。。你的逻辑好像不太清楚
问题3:数据库的模糊查询 %xxx%,%相当于任意多个字符
问题4:通过什么排序,如果排序的内容比较多,就分隔为数组。
能力有限,不过你问的太基础了,不太好回答。
042018-03-16
相似问题