1.修改和新增商品操作
public ServerResponse saveOrUpdateProduct(Product product){
if(product!=null){
```
if(StringUtils.isNotBlank(product.getSubImages())) {
String[] subImageArray = product.getSubImages().split(",");
if (subImageArray.length > 0) {
product.setMainImage(subImageArray[0]);
}
```
if(product.getId()!=null){
int resultCount = productMapper.updateByPrimaryKey(product);
if(resultCount>0){
return ServerResponse.createBySuccessMessage("更新商品成功");
}else{
return ServerResponse.createByErrorMessage("更新商品失败");
}
}else{
int resultCount = productMapper.insertSelective(product);
if(resultCount>0){
return ServerResponse.createBySuccessMessage("新增商品成功");
}else{
return ServerResponse.createByErrorMessage("新增更新商品失败");
}
}
}
return ServerResponse.createByErrorMessage("新增或更新产品参数不正确");
}
2.产品详情页管理
配置内容都是用properties文件统一编写,便于前后端分离,以及后期的维护,同时这样也更方便于进行热部署。
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("产品已经下架或删除");
}
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.setSubImage(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://img.happymmall.com/"));
//ImageHost
Category category = categoryMapper.selectByPrimaryKey(product.getCategoryId());
if(category==null){
productDetailVo.setParentCategoryId(0);//默认根节点 根节点内容为空
}else{
productDetailVo.setParentCategoryId(category.getParentId());
}
//createTime
productDetailVo.setCreateTime(DateTimeUtil.dateToStr(product.getCreateTime()));
productDetailVo.setUpdateTime(DateTimeUtil.dateToStr(product.getUpdateTime()));
return productDetailVo;
}
3.商品列表动态功能开发
这里需要使用分页工具pageHelper,每页显示数量,以及页码。
<select id="selectList" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from mmall_product
ORDER BY id ASC
#通常在后面见加上分号,但是切面时会加上limit关键字分页,就会报错
</select>
这里使用vo来存储所需要显示的部分,对于库存,子图等不予展示
public ServerResponse getProductList(int pageNum,int pageSize){
PageHelper.startPage(pageNum,pageSize);
List<Product> product_list=productMapper.selectList();
List<ProductListVo> productVoList = Lists.newArrayList();
for(Product product:product_list) {
ProductListVo productDetailVo = assembleProductListVo(product);
productVoList.add(productDetailVo);
}
PageInfo pageResult=new PageInfo(product_list);
pageResult.setList(productVoList);
return ServerResponse.createBySuccess(pageResult);
}
//装配成为指定值对象
public ProductListVo assembleProductListVo(Product product){
ProductListVo productListVo=new ProductListVo();
productListVo.setName(product.getName());
productListVo.setId(product.getId());
productListVo.setCategoryId(product.getCategoryId());
productListVo.setImageHost(PropertiesUtil.getProperty("ftp.server.http.prefix","http://img.happymmall.com/"));
productListVo.setMainImage(product.getMainImage());
productListVo.setPrice(product.getPrice());
productListVo.setSubtitle(product.getSubtitle());
productListVo.setStatus(product.getStatus());
return productListVo;
}
4.商品搜索功能
根据商品名和id来搜索,或者其中之一,mybatis编写sql语句时注意,where和if条件的使用方法,where可以代替诸如:where 1=1
之类的硬编码,智能的替换AND关键字,保证sql语句的正确执行,更美观,扩展性更好。
<select id="selectByNameAndProductId" resultMap="BaseResultMap" parameterType="map">
SELECT
<include refid="Base_Column_List"/>
FROM mmall_product
<where>
<if test="productName!=null">
AND productName LIKE #{productName}
</if>
<if test="productId!=null">
and productId=#{productId}
</if>
</where>
</select>
业务实现:
public ServerResponse<PageInfo> searchProduct(String productName,Integer productId,int pageNum,int pageSize){
PageHelper.startPage(pageNum,pageSize);
if(StringUtils.isNotBlank(productName)){
productName=new StringBuilder().append("%").append(productName).append("%").toString();
}
List<Product> productList = productMapper.selectByNameAndProductId(productName,productId);
List<ProductListVo> productListVoList = Lists.newArrayList();
for(Product product:productList) {
ProductListVo productListVo = assembleProductListVo(product);
productListVoList.add(productListVo);
}
PageInfo pageResult=new PageInfo(productList);
pageResult.setList(productListVoList);
return ServerResponse.createBySuccess(pageResult);
}
5.普通用户通过关键字和分类搜索商品
Mybatis ProductMapper.java定义如下:
List<Product> selectByNameAndProductIds(@Param("productName") String productName, @Param("categoryIdList") List<Integer> categoryIdList);
Mybatis ProductMapper.xml定义如下:
<select id="selectByNameAndProductIds" resultMap="BaseResultMap" parameterType="map">
SELECT
<include refid="Base_Column_List"/>
FROM mmall_product
<where>
<if test="productName!=null">
AND productName LIKE #{productName}
</if>
<if test="productIdList!=null">
and category_id in
<foreach collection="categoryIdList" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</if>
</where>
</select>
业务层实现代码:
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(StringUtils.isNotBlank(keyword) && category==null){
//没有该分类并且还没有该关键字 则返回一个空结果集
PageHelper.startPage(pageNum,pageSize);
List<ProductListVo> productListVoList = Lists.newArrayList();
PageInfo pageResult=new PageInfo(productListVoList); //空结果集不需要用setList转换
return ServerResponse.createBySuccess(pageResult);
}
//获取CategoryIdList service平级调用service
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)){ //在Const中定义排序的枚举类
String[] orderByArray=orderBy.split("_"); //与前端约定 用下划线_隔开类别和排序方式
PageHelper.orderBy(orderByArray[0]+" "+orderByArray[1]);
}
}
List<Product> productList = productMapper.selectByNameAndProductIds(
(StringUtils.isBlank(keyword))?null:keyword,categoryIdList.size()==0?null:categoryIdList);
//构建ListVO
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);
}
Web层部分代码:
@RequestMapping(value="list.do")
@ResponseBody
//这里的方法参数是可选的,因此用@RequestParam设置一下
public ServerResponse<PageInfo> list(@RequestParam(value = "keyword",required = false)String keyword,
@RequestParam(value = "categoryId",required = false)Integer categoryId,
@RequestParam(value = "pageNum",required = false)Integer pageNum,
@RequestParam(value = "pageSize",required = false)Integer pageSize,
@RequestParam(value = "orderBy",required = false)String orderBy){
return iProductService.getProductByKeywordCategory(keyword,categoryId,pageNum,pageSize,orderBy);
}