Java电商项目面试--商品模块

本文详细探讨了Java电商项目中的商品模块,涵盖POJO、BO、VO模型,高效分页与动态排序,FTP服务及富文本上传。重点讲解了商品的后台管理,包括商品新增更新、搜索、图片上传等功能实现,涉及Controller、Service层及Mybatis流程。同时讨论了动态分页的PageHelper实现、FTP服务器的作用与FastDFS的扩展性优势。
摘要由CSDN通过智能技术生成

面试:商品模块技术要点
1、POJO、BO、VO抽象模型
2、高效分页及动态排序
3、FTP服务对接、富文本上传

一、商品模块功能
前台功能:
1、产品搜索
2、动态排序列表
3、商品详情
后台功能:
1、商品列表
2、商品搜索
3、图片上传
4、增加商品、更新商品、商品上下架
二、后台新增和更新商品
Controller:

@Controller
@RequestMapping("/manage/product")
public class ProductManageController {
   
@Autowired
private IUserService iUserService;
@Autowired
private IProductService iProductService;
@Autowired
private IFileService iFileService;

@RequestMapping("save.do")
@ResponseBody
public ServerResponse productSave(HttpSession session, Product product) {
    User user = (User) session.getAttribute(Const.CURRENT_USER);
    if (user == null) {
        return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录,请登录管理员");
    }
    //如果是管理员
    if (iUserService.checkAdminRole(user).isSuccess()) {
        //增加或者更新商品
         return iProductService.saveOrUpdateProduct(product);
    } else {
        return ServerResponse.createByErrorMessage("无权限操作");
    }
}

Service层:

@Service("iProductService")
public class ProductServiceImpl implements IProductService {
   
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private ICategoryService iCategoryService;

    //保存或者更新商品
    public ServerResponse saveOrUpdateProduct(Product product){
        if(product != null)  //产品不为空
        {
            // 获取前端传来的子图字符串用;分割~譬如前端传来1.jpg,2.jpg,3.jpg,
            // 然后第一块代码就是将这个字符串按照;来分割成字符串数组,就可以获得所有子图,
            // 然后把第一张子图当做主图来存储
            if(StringUtils.isNotBlank(product.getSubImages())){
                String[] subImageArray = product.getSubImages().split(",");
                if(subImageArray.length > 0)
                    product.setMainImage(subImageArray[0]);
            }
            //当产品id存在则更新商品
            if(product.getId() != null){
                int rowCount = productMapper.updateByPrimaryKey(product);
                if(rowCount > 0)
                    return ServerResponse.createBySuccess("更新产品成功");
                return ServerResponse.createBySuccess("更新产品失败");
            }
            //不存在则添加
            else
            {
                int rowCount = productMapper.insert(product);
                if(rowCount > 0)
                    return ServerResponse.createBySuccess("新增产品成功");
                return ServerResponse.createBySuccess("新增产品失败");
            }
        }
        return ServerResponse.createByErrorMessage("新增或更新产品参数不正确");
    }
}

三、后台新增和更新商品
Controller:

@RequestMapping("set_sale_status.do")
@ResponseBody
public ServerResponse setSaleStatus(HttpSession session, Integer productId,Integer status){
    User user = (User)session.getAttribute(Const.CURRENT_USER);
    if(user == null){
             return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录,请登录管理员");
    }
    if(iUserService.checkAdminRole(user).isSuccess()){
            return iProductService.setSaleStatus(productId,status);
    }else{
            return ServerResponse.createByErrorMessage("无权限操作");
    }
}

Service:

public ServerResponse<String> setSaleStatus(Integer productId,Integer status){
    if(productId == null || status == null){
             return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getDesc());
    }
    Product product = new Product();
    product.setId(productId);
    product.setStatus(status);
    int rowCount = productMapper.updateByPrimaryKeySelective(product);
    if(rowCount > 0){
            return ServerResponse.createBySuccess("修改产品销售状态成功");
    }
    return ServerResponse.createByErrorMessage("修改产品销售状态失败");
}

四、后台获取产品详情
Controller:

@RequestMapping("detail.do")
@ResponseBody
public ServerResponse getDetail(HttpSession session, Integer productId){
    User user = (User)session.getAttribute(Const.CURRENT_USER);
    if(user == null)
        return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录,请登录管理员");
    if(iUserService.checkAdminRole(user).isSuccess()){
        return iProductService.manageProductDetail(productId);
    }else{
        return ServerResponse.createByErrorMessage("无权限操作");
    }
}

Service:

@Service("iProductService")
public class ProductServiceImpl implements IProductService {
   
      @Autowired
      private ProductMapper productMapper; 
      @Autowired
      private CategoryMapper categoryMapper;
      @Autowired
      private ICategoryService iCategoryService;

public ServerResponse<ProductDetailVo> manageProductDetail(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("产品已下架或者删除");
    //VO -- value object
    ProductDetailVo productDetailVo = assembleProductDetailVo(product);
    return ServerResponse.createBySuccess(productDetailVo);
}
private ProductDetailVo assembleProductDetailVo(Product product){
    ProductDetailVo productDetailVo = new ProductDetailVo();
    productDetailVo.setId(product.getId());
    productDetailVo.setSubtitle(product.getSubtitle());
    productDetailVo.setPrice(product.getPrice());
    productDetailVo.setMainImage(product.getMainImage());
    productDetailVo.setSubImages(product.getSubImages());
    productDetailVo.setCategoryId(product.getCategoryId());
    productDetailVo.setDetail(product.getDetail());
    productDetailVo.setName(product.getName());
    productDetailVo.setStatus(product.getStatus());
    productDetailVo.setStock(product.getStock());
    productDetailVo.setImageHost(PropertiesUtil.getProperty("ftp.server.http.prefix","http://image.long.com/"));
  • 17
    点赞
  • 133
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北极星小王子

你的鼓励是我创作的最大动力~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值