• 近期将进行后台系统升级,如有访问不畅,请稍后再试!
  • 极客文库-知识库上线!
  • 极客文库小编@勤劳的小蚂蚁,为您推荐每日资讯,欢迎关注!
  • 每日更新优质编程文章!
  • 更多功能模块开发中。。。

从零到企业级SSM电商项目实战教程(三十一)前台商品详情,列表,搜索,动态排序功能开发

文章目录[隐藏]

ProductController.java

controller 包下的 portal 包下新建 ProductController.java

package com.mmall.controller.portal;

import com.github.pagehelper.PageInfo;
import com.mmall.common.ServerResponse;
import com.mmall.service.IProductService;
import com.mmall.vo.ProductDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: Geekerstar(jikewenku.com)
 * @Date: 2018/6/23 17:18
 * @Description:
 */
@RestController
@RequestMapping("/product/")
public class ProductController {

    @Autowired
    private IProductService iProductService;

    @RequestMapping("detail.do")
    @ResponseBody
    public ServerResponse<ProductDetailVo> detail(Integer productId){
        return iProductService.getProductDetail(productId);
    }

    @RequestMapping("list.do")
    @ResponseBody
    public ServerResponse<PageInfo> list(@RequestParam(value = "keyword",required = false)String keyword,
                                         @RequestParam(value = "categoryId",required = false)Integer categoryId,
                                         @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                         @RequestParam(value = "pageSize",defaultValue = "10") int pageSize,
                                         @RequestParam(value = "orderBy",defaultValue = "") String orderBy){
        return iProductService.getProductByKeywordCategory(keyword,categoryId,pageNum,pageSize,orderBy);
    }

}

ICategoryService.java

这里修改一下 selectCategoryAndChildrenById 方法

ServerResponse<List<Integer>> selectCategoryAndChildrenById(Integer categoryId);

IProductService.java

ServerResponse<ProductDetailVo> getProductDetail(Integer productId);

ServerResponse<PageInfo> getProductByKeywordCategory(String keyword,Integer categoryId,int pageNum,int pageSize,String orderBy);

ProductServiceImpl.java

public ServerResponse<ProductDetailVo> getProductDetail(Integer productId){
    if(productId == null){
        return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getDesc());
    }
    Product product = productMapper.selectByPrimaryKey(productId);
    if(product == null){
        return ServerResponse.createByErrorMessage("产品已下架或者删除");
    }
    if(product.getStatus() != Const.ProductStatusEnum.ON_SALE.getCode()){
        return ServerResponse.createByErrorMessage("产品已下架或者删除");
    }
    ProductDetailVo productDetailVo = assembleProductDetailVo(product);
    return ServerResponse.createBySuccess(productDetailVo);
}

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>();

    if(categoryId != null){
        Category category = categoryMapper.selectByPrimaryKey(categoryId);
        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();
    }
    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)){
            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);
}

Const.java

public interface ProductListOrderBy{
    Set<String> PRICE_ASC_DESC = Sets.newHashSet("price_desc","price_asc");
}

public enum ProductStatusEnum{
    ON_SALE(1,"在线");
    private String value;
    private int code;
    ProductStatusEnum(int code,String value){
        this.code = code;
        this.value = value;
    }

    public String getValue() {
        return value;
    }

    public int getCode() {
        return code;
    }
}

ProductMapper.java

List<Product> selectByNameAndCategoryIds(@Param("productName")String productName,@Param("categoryIdList")List<Integer> categoryIdList);

ProductMapper.xml

<select id="selectByNameAndCategoryIds" resultMap="BaseResultMap" parameterType="map">
  SELECT
  <include refid="Base_Column_List"></include>
  from mmall_product
  where status = 1
  <if test="productName != null">
    and name like #{productName}
  </if>
  <if test="categoryIdList != null" >
    and category_id in
    <foreach item="item" index="index" open="(" separator="," close=")" collection="categoryIdList">
      #{item}
    </foreach>
  </if>
</select>

喜欢 (0)
[247507792@qq.com]
分享 (0)

欢迎 注册账号 登录 发表评论!

  • 精品技术教程
  • 编程资源分享
  • 问答交流社区
  • 极客文库知识库

客服QQ


QQ:2248886839


工作时间:09:00-23:00