下面来讲解一下后台获取商品列表的接口开发:
1、在ProductManageController
类中新增下列代码:
//获取商品列表接口
@RequestMapping("get_list.do")
@ResponseBody
public ServerResponse getList(HttpSession session, @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize){
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.getProductList(pageNum,pageSize);
}else {
return ServerResponse.createByErrorMessage("当前登录者不是管理员,无权限操作");
}
}
*Service
:
//查询商品列表
ServerResponse<PageInfo> getProductList(int pageNum, int pageSize);
在vo包下新建ProductListVo
类:
ProductListVo
:
package com.mmall.vo;
import java.math.BigDecimal;
public class ProductListVo {
private Integer id;
private Integer categoryId;
private String name;
private String subtitle;
private String mainImage;
private BigDecimal price;
private Integer status;
private String imageHost;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getCategoryId() {
return categoryId;
}
public void setCategoryId(Integer categoryId) {
this.categoryId = categoryId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSubtitle() {
return subtitle;
}
public void setSubtitle(String subtitle) {
this.subtitle = subtitle;
}
public String getMainImage() {
return mainImage;
}
public void setMainImage(String mainImage) {
this.mainImage = mainImage;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getImageHost() {
return imageHost;
}
public void setImageHost(String imageHost) {
this.imageHost = imageHost;
}
}
*ServiceImpl
:
//查询商品List
public ServerResponse<PageInfo> getProductList(int pageNum,int pageSize){
//startPage--start
//填充自己的sql逻辑
//PageHelper--收尾
PageHelper.startPage(pageNum,pageSize);
List<Product> productList=productMapper.selectList();
List<ProductListVo> productListVoList= Lists.newArrayList();
for (Product productItem: productList){
ProductListVo productListVo=assembleProductListVo(productItem);
productListVoList.add(productListVo);
}
PageInfo pageResult=new PageInfo(productList);
pageResult.setList(productListVoList);
return ServerResponse.createBySuccess(pageResult);
}
//封装返回给前端的商品列表信息Vo类
private ProductListVo assembleProductListVo(Product product){
ProductListVo productListVo=new ProductListVo();
productListVo.setId(product.getId());
productListVo.setSubtitle(product.getSubtitle());
productListVo.setPrice(product.getPrice());
productListVo.setMainImage(product.getMainImage());
productListVo.setCategoryId(product.getCategoryId());
productListVo.setName(product.getName());
productListVo.setStatus(product.getStatus());
productListVo.setImageHost(PropertiesUtil.getProperty("ftp.server.http.prefix","http://image.imooc.com/"));
return productListVo;
}
其中在下面这行代码中的selectList()
使用的是我们自己封装的方法:
List<Product> productList=productMapper.selectList();
ProductMapper
:
//查询商品列表
List<Product> selectList();
ProductMapper.xml
:
<!--查询商品列表-->
<select id="selectList" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from mmall_product
order by id asc
</select>
2、接口测试:
后台获取商品列表接口测试: