文章目录
-
-
- 1. Dao层的实现
-
- 1.1 ProductDao接口
- 1.2 ProductDao.xml实现类
- 1.3 ProductDaoTest测试类
- 2. Service层的实现
-
- 2.1 ProductService接口
- 2.2 ProductServiceImpl实现类
- 3. Controller层的实现
-
1. Dao层的实现
1.1 ProductDao接口
/**
* 查询商品列表并分页,可输入的条件有:商品名(模糊查询),商品状态,店铺Id,商品类别
* @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);
1.2 ProductDao.xml实现类
<select id="queryProductList" resultType="com.imooc.o2o.entity.Product">
select
product_id,
product_name,
product_desc,
img_addr,
normal_price,
promotion_price,
priority,
create_time,
last_edit_time,
enable_status,
product_category_id,
shop_id
from
tb_product
<where>
<if test="productCondition.shop!=null and productCondition.shop.shopId!=null">
and shop_id=#{productCondition.shop.shopId}
</if>
<if test="productCondition.productCategory!=null and productCondition.productCategoty.productCategoryId!=null">
and product_category_id=#{productCondition.productCategory.productCategoryId}
</if>
<if test="productCondition.productName!=null">
and product_name like '%${productCondition.productName}%'
</if>
<if test="productCondition.enableStatus!=null">
and enableStatus = #{productCondition.enableStatus}
</if>
</where>
order by priority desc
limit #{rowIndex},#{pageSize};
</select>
<select id="queryProductCount" resultType="int">
select count(1) from tb_product
<where>
<if test="productCondition.shop!=null and productCondition.shop.shopId!=null">
and shop_id=#{productCondition.shop.shopId}
</if>
<if test="productCondition.productCategory!=null and productCondition.productCategoty.productCategoryId!=null">
and product_category_id=#{productCondition.productCategory.productCategoryId}
</if>
<if test="productCondition.productName!=null">
and product_name like '%${productCondition.productName}%'
</if>
<if test="productCondition.enableStatus!=null">
and enableStatus = #{productCondition.enableStatus}
</if>
</where>
order by priority desc
</select>
1.3 ProductDaoTest测试类
@Test
public void testBQueriProductList() throws Exception{
Product productCondition = new Product();
//分页查询,预期返回三条结果
List<Product> productList = productDao.queryProductList(productCondition,0,3);
assertEquals(3,productList.size());
//查询商品总数
int count = productDao.queryProductCount(productCondition);
assertEquals(6,count);
//使用商品名称模糊查询
productCondition.setProductName("测试");
productList = productDao.queryProductList(productCondition, 0, 3);
assertEquals(3,productList.size());
count = productDao.queryProductCount(productCondition);
assertEquals(3,count);
}
2. Service层的实现
2.1 ProductService接口
/**
* 查询商品列表并分页,可输入的条件有:商品名(模糊查询),商品状态,店铺Id,商品类别
* @param productCondition
* @param pageIndex
* @param pageSize
* @return
*/
ProductExecution getProductList(Product productCondition,int pageIndex,int pageSize);
2.2 ProductServiceImpl实现类
/**
* @param productCondition
* @param pageIndex
* @param pageSize
* @return
*/
@Override
public ProductExecution getProductList(Product productCondition, int pageIndex, int pageSize) {
//页码转换为数据库的行码,并调用dao层取回指定页码的商品列表
int rowIndex = PageCaculator.calculateRowIndex(pageIndex,pageSize);
List<Product> productList = productDao.queryProductList(productCondition, rowIndex, pageSize);
//商品总数
int count = productDao.queryProductCount(productCondition);
ProductExecution productExecution = new ProductExecution();
productExecution.setProductList(productList);
productExecution.setCount(count);
return productExecution;
}
3. Controller层的实现
@RequestMapping(value = "/getproductlistbyshop",method = RequestMethod.GET)
@ResponseBody
private Map<String,Object> getProductListByShop(HttpServletRequest request) {
Map<String, Object> modelMap = new HashMap<>();
//获取前台传过来的页码
int pageIndex = HttpServletRequestUtil.getInt(request, "pageIndex");
//获取前台传过来的每页要求返回的商品数上线
int pageSize = HttpServletRequestUtil.getInt(request, "pageSize");
//从当前session中获取店铺信息,主要获取shopId
Shop currentShop = (Shop) request.getSession().getAttribute("currentShop");
//空值判断
if(pageIndex>-1 &&pageSize>-1&¤tShop!=null && currentShop.getShopId()!=null){
//获取传入的需要检索的条件
long productCategryId = HttpServletRequestUtil.getLong(request,"productCategoryId");
String productName = HttpServletRequestUtil.getString(request,"productName");
//筛选的条件可以进行排列组合
Product productCondition
= compactProductCondition(currentShop.getShopId(),productCategryId,productName);
//传入查询条件以及分页查询信息进行查询,返回相应的商品列表以及总数
ProductExecution productExecution
= productService.getProductList(productCondition, pageIndex, pageSize);
modelMap.put("productList",productExecution.getProductList());
modelMap.put("count",productExecution.getCount());
modelMap.put("success",true);
}else{
modelMap.put("success",false);
modelMap.put("errMsg","empty pageSize or pageIndex or shopId");
}
return modelMap;
}
private Product compactProductCondition(Long shopId, long productCategryId, String productName) {
Product productCondition = new Product();
Shop shop =new Shop();
shop.setShopId(shopId);
productCondition.setShop(shop);
//如果指定类别的要去则添加进去
if(productCategryId!=-1L){
ProductCategory productCategory = new ProductCategory();
productCategory.setProductCategoryId(productCategryId);
productCondition.setProductCategory(productCategory);
}
//若有商品名模糊查询的要求则添加进去
if(productName!=null){
productCondition.setProductName(productName);
}
return productCondition;
}