- 商品表
商品表
create table 'mmall_product'(
'id' int(11) NOT NULL AUTO_INCREMENT COMMENT '商品id',
'category_id' int(11) NOT NULL COMMENT '分類id,對應mmal_category分類表的主鍵',
'name' varchar(100) NOT NULL COMMENT '商品名稱',
'subtitle' varchar(200) DEFAULT NULL COMMENT '商品副標題',
'main_image' varchar(500) DEFAULT NULL COMMENT '產品主圖,url相對地址',
'sub_images' text COMMENT '圖片地址,json格式,擴展用',
'detail' text COMMENT '商品詳情',
'price' decimal(20,2) NOT NULL COMMENT '價格,單位-元保留兩位小數',
'stock' int(11) NOT NULL COMMENT '庫存數量',
'status int(6) DEFAULT '1' COMMENT '商品狀態:1-在售,2-下架,3-刪除',
'create_time' datetime DEFAULT NULL COMMENT '創建時間',
'update_time' datetime DEFAULT NULL COMMENT '更新時間',
PRIMARY KEY ('id')
)ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8
- 設置共用常量類
public class Const {
public static final String CURRENT_USER = "currentUser";
public static final String EMAIL="email";
public static final String USERNAME="username";
public interface Role{
int ROLE_CUSTOMER=0;//普通用戶
int ROLE_ADMIN=1;//管理員
}
public interface ProductListOrderBy{
Set<String> PRICE_ASC_DESC= Sets.newHashSet("price_desc","price_asc");
}
}
一、前臺商品詳情
- 思路:根據前端傳來的商品id進行查詢,轉換成Vo類展示在前端上。
- controller
//前臺商品詳情
//http://localhost:8080/product/detail.do?productId=2
@RequestMapping("detail.do")
@ResponseBody
public ServerResponse<ProductDetailVo> getProductDetail(Integer productId){
return iProductService.productDetail(productId);
}
- impl
//前臺商品詳情
public ServerResponse<ProductDetailVo> productDetail(Integer productId){
if (productId == null){
return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
}
Product product=productMapper.selectByProductId(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.setSubImages(product.getSubImages());
productDetailVo.setCategoryId(product.getCategoryId());
productDetailVo.setDetail(product.getDetail());
productDetailVo.setName(product.getName());
productDetailVo.setStatus(product.getStatus());
productDetailVo.setStock(product.getStock());
//賦值imageHost,從配置文件中獲取圖片的前綴
productDetailVo.setImageHost(PropertiesUtil.getProperty("ftp.server.http.prefix","http://image.mmall.com/"));
//找出該商品的父節點商品
Category category=categoryMapper.selectByPrimaryKey(product.getCategoryId());
//爲空則默認爲根節點
if (category == null){
productDetailVo.setParentCategoryId(0);
}else {
productDetailVo.setParentCategoryId(category.getParentId());
}
productDetailVo.setCreateTime(DateTimeUtil.dateToStr(product.getCreateTime()));
productDetailVo.setUpdateTime(DateTimeUtil.dateToStr(product.getUpdateTime()));
return productDetailVo;
}
二、商品搜索及動態排序List
- controller
//前臺商品搜索及動態排序
//http://localhost:8080/product/list.do?keyword=xxx&categoryId=1&orderBy=price_desc
@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);
}
- impl
//前臺商品搜索及動態排序
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.selecteCategoryAndChildrenById(category.getId()).getData();
}
//關鍵字不爲空
if (StringUtils.isNotBlank(keyword)){
//%關鍵字%用於實現like模糊查詢
keyword=new StringBuilder().append("%").append(keyword).append("%").toString();
}
//設置分頁的起始頁和每頁數量的大小
PageHelper.startPage(pageNum,pageSize);
//從前端傳來的orderBy爲price_desc或price_asc
if (StringUtils.isNotBlank(orderBy)){
if (Const.ProductListOrderBy.PRICE_ASC_DESC.contains(orderBy)){
//price_desc,以_進行分割成price和desc
String[] orderByArray=orderBy.split("_");
//OrderBy price desc
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);
}
//封裝pojo查詢結果,pojo轉成Vo展示在前臺上
PageInfo pageInfo=new PageInfo(productList);
pageInfo.setList(productListVoList);
return ServerResponse.createBySuccess(pageInfo);
}
- Mybatis-Mapper
public interface ProductMapper {
Product selectByPrimaryKey(Integer id);
//前臺商品詳情
Product selectByProductId(@Param("productId") Integer productId);
List<Product> selectByNameAndCategoryIds(@Param("productName") String productName,@Param("categoryIdList")List<Integer> categoryIdList);
}
- Mybatis-SQL
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.mmall.dao.ProductMapper" >
<resultMap id="BaseResultMap" type="com.mmall.pojo.Product" >
<constructor >
<idArg column="id" jdbcType="INTEGER" javaType="java.lang.Integer" />
<arg column="category_id" jdbcType="INTEGER" javaType="java.lang.Integer" />
<arg column="name" jdbcType="VARCHAR" javaType="java.lang.String" />
<arg column="subtitle" jdbcType="VARCHAR" javaType="java.lang.String" />
<arg column="main_image" jdbcType="VARCHAR" javaType="java.lang.String" />
<arg column="sub_images" jdbcType="VARCHAR" javaType="java.lang.String" />
<arg column="detail" jdbcType="VARCHAR" javaType="java.lang.String" />
<arg column="price" jdbcType="DECIMAL" javaType="java.math.BigDecimal" />
<arg column="stock" jdbcType="INTEGER" javaType="java.lang.Integer" />
<arg column="status" jdbcType="INTEGER" javaType="java.lang.Integer" />
<arg column="create_time" jdbcType="TIMESTAMP" javaType="java.util.Date" />
<arg column="update_time" jdbcType="TIMESTAMP" javaType="java.util.Date" />
</constructor>
</resultMap>
<sql id="Base_Column_List" >
id, category_id, name, subtitle, main_image, sub_images, detail, price, stock, status,
create_time, update_time
</sql>
<select id="selectByProductId" resultMap="BaseResultMap" parameterType="int">
select <include refid="Base_Column_List"></include>
from mmall_product
where id=#{productId} and status=1
</select>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from mmall_product
where id = #{id,jdbcType=INTEGER}
</select>
<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>
</mapper>