商品列表展示
dao层
/**
* 分页查询商品信息:可输入的条件有:商品名(模糊查询),商品状态,商品类别
* @param productCondition
* @param rowIndex 表示第几行开始取数据
* @param pageSize 表示一共取多少行
* @return
*/
List<Product> queryProductList(@Param("productCondition")Product productCondition,
@Param("rowIndex")int rowIndex,
@Param("pageSize")int pageSize);
/**
* 返回按照上面的查询条件总共有多少条记录符合条件
* @param productCondition
* @return
*/
int queryProductCount(@Param("productCondition")Product productCondition);
<!--List<Product> queryProductList(@Param("productCondition")Product productCondition,
@Param("rowIndex")int rowIndex,
@Param("pageSize")int pageSize);-->
<select id="queryProductList" resultMap="productMap">
SELECT
p.product_id,
p.product_name,
p.product_desc,
p.img_addr,
p.normal_price,
p.promotion_price,
p.priority,
p.create_time,
p.last_edit_time,
p.status,
p.product_category_id,
p.shop_id,
pm.product_img_id,
pm.img_addr,
pm.img_desc,
pm.priority,
pm.create_time,
pm.product_id
FROM
product p
LEFT JOIN
product_img pm
ON
p.product_id=pm.product_id
<where>
<if test="productCondition.shop != null
and productCondition.shop.shopId != null">
p.shop_id=#{productCondition.shop.shopId}
</if>
<if test="productCondition.productCategory != null
and productCondition.productCategory.productCategoryId != null">
AND p.product_category_id=#{productCondition.productCategory.productCategoryId}
</if>
<if test="productCondition.productName != null">
AND p.product_name LIKE '%${productCondition.productName}%'
</if>
<if test="productCondition.status != null">
AND p.status=#{productCondition.status}
</if>
</where>
ORDER BY
p.priority DESC
LIMIT #{rowIndex}, #{pageSize};
</select>
<!--int queryProductCount(Product productCondition);-->
<select id="queryProductCount" resultType="java.lang.Integer">
SELECT COUNT(1)
FROM
product p
LEFT JOIN
product_img pm
ON
p.product_id=pm.product_id
<where>
<if test="productCondition.shop != null
and productCondition.shop.shopId != null">
p.shop_id=#{productCondition.shop.shopId}
</if>
<if test="productCondition.productCategory != null
and productCondition.productCategory.productCategoryId != null">
AND p.product_category_id=#{productCondition.productCategory.productCategoryId}
</if>
<if test="productCondition.productName != null">
AND p.product_name LIKE '%${productCondition.productName}%'
</if>
<if test="productCondition.status != null">
AND p.status=#{productCondition.status}
</if>
</where>
</select>
service层
/**
*获取商品名(模糊查询),商品状态,shopId,ProductCategoryId
* @param productCondition
* @param pageIndex
* @param pageSize
* @return
*/
ProductExecution getProductList(Product productCondition, int pageIndex, int pageSize);
@Override
public ProductExecution getProductList(Product productCondition, int pageIndex, int pageSize) {
int rowIndex = PageCalculator.calculateRowIndex(pageIndex,pageSize);
List<Product> productList = productDao.queryProductList( productCondition, pageIndex, pageSize );
int count = productDao.queryProductCount( productCondition );
ProductExecution productExecution = new ProductExecution();
if(productList != null && productList.size() > 0){
productExecution.setState( ProductStateEnum.SUCCESS.getState() );
productExecution.setProductList( productList );
productExecution.setCount( count );
} else{
productExecution.setState(ProductStateEnum.INNER_ERROR.getState());
}
return productExecution;
}
controller层
@RequestMapping(value="/getproductlistbyshop", method = RequestMethod.GET)
@ResponseBody
public Map<String, Object> getProductListByShop(HttpServletRequest request){
Map<String, Object> modelMap = new HashMap<>();
Long shopId = 1L;
Shop shop = new Shop();
shop.setShopId( shopId );
//从前台获取传过来的页码
int pageIndex = HttpServletRequestUtil.getInt( request, "pageIndex" );
//从前台获取每页要求的商品数量
int pageSize = HttpServletRequestUtil.getInt( request, "pageSize" );
//空值判断
if((pageIndex > -1) && (pageSize > 0) && (shop != null) &&(shop.getShopId() != null)){
//获取从前台传入的检索条件,包括商品类别,商品名,然后进行查询,分页
/*Long productCategoryId = HttpServletRequestUtil.getLong( request, "productCategoryId" );
String productName = HttpServletRequestUtil.getString( request, "productName" );
Product productCondition = compactProductCondition(shopId, productCategoryId, productName);*/
Product productCondition = compactProductCondition(shopId, 7L, null);
//传入商品查询条件,以及商品查询分页条件,返回商品查询列表和总数
ProductExecution productExecution = productService.getProductList( productCondition, pageIndex, pageSize );
if(productExecution.getState() == ProductStateEnum.SUCCESS.getState()){
modelMap.put( "success", true );
modelMap.put( "productList", productExecution.getProductList() );
modelMap.put( "count", productExecution.getCount() );
} else{
modelMap.put( "success", false );
modelMap.put( "errMsg", productExecution.getStateInfo() );
}
} else{
modelMap.put( "success", false );
modelMap.put( "errMsg", "pageSize or pageIndex or shopId is empty" );
}
return modelMap;
}
/**
* 封装商品查询条件到Product中
* @param shopId
* @param productCategoryId
* @param productName
* @return
*/
private Product compactProductCondition(Long shopId, Long productCategoryId, String productName){
Product productCondition = new Product();
Shop shop = new Shop();
shop.setShopId(shopId);
productCondition.setShop( shop );
if(productCategoryId != -1L){
ProductCategory productCategory = new ProductCategory();
productCategory.setProductCategoryId( productCategoryId );
productCondition.setProductCategory( productCategory );
}
if(productName != null){
productCondition.setProductName(productName);
}
return productCondition;
}
商品下架