淘淘商城46-商品详情页面代码编写

版权声明:本文为博主原创文章,如有转载请注明出处,谢谢。 https://blog.csdn.net/pdsu161530247/article/details/82050136

目录

1.商品详情页面效果

2.功能分析

3.编写代码

3.1dao层

3.2service层

3.2.1service接口

3.2.2service实现类

3.3Controller

4.运行测试


1.商品详情页面效果

2.功能分析

在搜索结果页面点击商品图片或者商品标题,展示商品详情页面。

在item.jsp中,我们查需要准备的数据,发现需要准备TbItem与TbItemDesc这两个pojo到tem.jsp中即可。

但是发现一个images数组,在TbItem中并没有这个数组,在TbItem中有image这个字符串,所以可以创建一个pojo继承 TbItem,添加一个getImages方法,放在taotao-item-web工程中

在taotao-item-web中创建一个com.taotao.item.pojo包专门存放pojo,在包下创建一个item继承tbitem

package com.taotao.item.pojo;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import com.taotao.pojo.TbItem;

public class Item extends TbItem{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * 将TbItem中的值复制到Item中
	 * @param tbItem
	 */
	public Item(TbItem tbItem) {
		BeanUtils.copyProperties(tbItem, this);
	}
	/**
	 * el表达式获取图片
	 * @return
	 */
	public String[] getImages(){
		if(StringUtils.isNotBlank(super.getImage())){
			return super.getImage().split(",");
		}
		return null;
	}
}

3.编写代码

请求的url:/item/{itemId}

参数:商品id

返回值:String 逻辑视图

业务逻辑:

  1. 从url中取参数,商品id
  2. 根据商品id查询商品信息(tb_item)得到一个TbItem对象,缺少images属性,可以创建一个pojo继承 ,添加一个getImages方法,放在taotao-item-web工程中。
  3. 还需要商品的描述信息  通过商品描述的Mapper查询出来,再通过调用service将数据传递页面。
  4. 返回页面

3.1dao层

查询tb_item, tb_item_desc两个表,都是单表查询。可以使用逆向工程。

3.2service层

3.2.1service接口

根据id查询商品信息与商品描述

在taotao-manager-interface创建接口

/**
	 * 根据商品id获取商品详细信息
	 * @param itemId
	 * @return
	 */
	TbItem getItemById(Long itemId);
	/**
	 * 根据商品id获取商品描述
	 * @param itemId
	 * @return
	 */
	TbItemDesc getItemDescById(Long itemId);

3.2.2service实现类

在taotao-manager-service实现接口,根据id查询对应信息即可

/**
	 * 根据商品id获取商品详细信息
	 */
	@Override
	public TbItem getItemById(Long itemId) {
		return itemMapper.selectByPrimaryKey(itemId);
	}
	/**
	 * 根据商品id获取商品描述
	 */
	@Override
	public TbItemDesc getItemDescById(Long itemId) {
		return itemDescMapper.selectByPrimaryKey(itemId);
	}

3.3Controller

请求的url:/item/{itemId}

参数:商品id

返回值:String 逻辑视图

package com.taotao.item.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.taotao.item.pojo.Item;
import com.taotao.pojo.TbItem;
import com.taotao.pojo.TbItemDesc;
import com.taotao.service.ItemService;

@Controller
public class ItemController {
	
	@Autowired
	private ItemService itemService;
	
	@RequestMapping("/item/{itemId}")
	public String showItemInfo(@PathVariable Long itemId,Model model) {
		//1.根据id查询商品信息
		TbItem tbItem = itemService.getItemById(itemId);
		//2.将tbItem转为item
		Item item =new Item(tbItem);
		//3.根据id查询商品描述
		TbItemDesc itemDesc = itemService.getItemDescById(itemId);
		//4.将数据传递到页面
		model.addAttribute("item", item);
		model.addAttribute("itemDesc", itemDesc);
		//5.返回视图
		return "item";
	}
}

引入服务

在springmvc.xml中配置

<dubbo:reference interface="com.taotao.service.ItemService" id="itemService" timeout="300000" />

4.运行测试

将修改过的子工程重新maven install ,启动项目即可访问

猜你喜欢

转载自blog.csdn.net/pdsu161530247/article/details/82050136