面试:商品模块技术要点
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.setImage