一、功能分析
在搜索结果页面点击商品图片或者商品标题,展示商品详情页面。
请求的url:/item/{itemId}
参数:商品id
返回值:String 逻辑视图
业务逻辑:
1、从url中取参数,商品id
2、根据商品id查询商品信息(tb_item)得到一个TbItem对象,缺少images属性,可以创建一个pojo继承TbItem,添加一个getImages方法。在e3-item-web工程中。
创建pojo
package cn.e3mall.item.pojo;
import cn.e3mall.pojo.TbItem;
public class Item extends TbItem{
public String[] getImages() {
String image2 = this.getImage();
if (image2 != null && !"".equals(image2)) {
String[] strings = image2.split(",");
return strings;
}
return null;
}
public Item() {
}
public Item(TbItem tbItem) {
this.setBarcode(tbItem.getBarcode());
this.setCid(tbItem.getCid());
this.setCreated(tbItem.getCreated());
this.setId(tbItem.getId());
this.setImage(tbItem.getImage());
this.setNum(tbItem.getNum());
this.setPrice(tbItem.getPrice());
this.setSellPoint(tbItem.getSellPoint());
this.setStatus(tbItem.getStatus());
this.setTitle(tbItem.getTitle());
this.setUpdated(tbItem.getUpdated());
}
}
二、dao层
查询tb_item,tb_item_desc两个表,都是单表查询。可以使用逆向工程。
三、Service层
1、根据商品id查询商品信息
参数:商品id
返回值:TbItem
2、根据商品id查询商品描述
参数:商品id
返回值:TbItemDesc
实现类里添加实现代码:
@Override
public TbItemDesc getItemDescById(long itemId) {
TbItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(itemId);
return itemDesc;
}
新增了方法,要安装到本地仓库。
e3-manager->run as->maven install
发布服务:
引入服务:
四、Controller
请求的url:/item/{itemId}
参数:商品id
返回值:String 逻辑视图
/**
* 商品详情页面展示controller
*
*/
@Controller
public class ItemController {
@Autowired
private ItemService itemService;
@RequestMapping("/item/{itemId}")
public String showItemInfo(@PathVariable Long itemId, Model model) {
// 跟据商品id查询商品信息
TbItem tbItem = itemService.getItemById(itemId);
// 把TbItem转换成Item对象
Item item = new Item(tbItem);
// 根据商品id查询商品描述
TbItemDesc itemDesc = itemService.getItemDescById(itemId);
// 把数据传递给页面
model.addAttribute("item", item);
model.addAttribute("itemDesc", itemDesc);
return "item";
}
}
五、测试
该install的install,该重启的重启。
搜索手机
点击小米手机,进入详情页