8.完成首页展示功能(二)

  • Post author:
  • Post category:其他




  • 2.banner的显示

参考菜单功能的流程,新建:

1). 商品对象(banner这里显示的也是商品信息)

Commodity.class

package com.qyuz.model;
import java.io.Serializable;
public class Commodity implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	String id;
	String name;
	String descr;
	String picurl;
	Double price;
	Integer stock;
	Integer categoryId;
	String url1;
	String url2;
	String url3;
	public Commodity(){
		
	}
	
	public Commodity(String name, String descr, String picurl,
			Double price, Integer stock, Integer categoryId, int type) {
		this.name = name;
		this.descr = descr;
		this.picurl = picurl;
		this.price = price;
		this.stock = stock;
		this.categoryId = categoryId;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescr() {
		return descr;
	}

	public void setDescr(String descr) {
		this.descr = descr;
	}

	public String getPicurl() {
		return picurl;
	}

	public void setPicurl(String picurl) {
		this.picurl = picurl;
	}

	public Double getPrice() {
		return price;
	}

	public void setPrice(Double price) {
		this.price = price;
	}

	public Integer getStock() {
		return stock;
	}

	public void setStock(Integer stock) {
		this.stock = stock;
	}

	public Integer getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(Integer categoryId) {
		this.categoryId = categoryId;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public String getUrl1() {
		return url1;
	}

	public void setUrl1(String url1) {
		this.url1 = url1;
	}

	public String getUrl2() {
		return url2;
	}

	public void setUrl2(String url2) {
		this.url2 = url2;
	}

	public String getUrl3() {
		return url3;
	}

	public void setUrl3(String url3) {
		this.url3 = url3;
	}

	public String toString(){
		return " Commodity[id="+this.id 
				+",name=" +this.name
				+",desc="+this. descr
				+",picurl="+this. picurl
				+",price="+this. price
				+",stock="+this. stock
				+",categoryId="+this.categoryId
				+"]";
	}
}

2).Dao层

ComDao.class

package com.qyuz.dao;

import java.util.List;
import java.util.Map;

import com.qyuz.model.Commodity;

public interface ComDao {
	
	/**
	 * 
	 * @return
	 */
	public List<Commodity> getComs(Map map);
}

ComDaoMapper.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.qyuz.dao.ComDao">
   
   <select id="getComs" parameterType="Map" resultType="com.qyuz.model.Commodity" >
   		select * from commodity
   		where 1=1
   		<if test="id != null">
   			and id=${id}
   		</if>
   		<if test="categoryId != null">
   			and category_id=${categoryId}
   		</if>
   		<if test="start != null and end != null">
   			limit ${start},${end}
   		</if>
   </select>
</mapper>

3). Service层

IComService.class

package com.qyuz.service;

import java.util.List;

import com.qyuz.model.Commodity;


public interface IComService {
	/**
	 * 
	 * @return
	 */
	public List<Commodity> getBannerComs();
}

ComServiceImpl.class

package com.qyuz.service.impl;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qyuz.dao.ComDao;
import com.qyuz.model.Commodity;
import com.qyuz.service.IComService;


@Service
public class ComServiceImpl implements IComService {

	@Autowired
	ComDao dao;
	
	@Override
	public List<Commodity> getBannerComs() {
		// TODO Auto-generated method stub
		HashMap map = new HashMap();
		map.put("categoryId", 1);
		return dao.getComs(map);
	}
}

4). controller层

在HomeController.class添加,

属性:

@Autowired
IComService comService;

index函数里添加:

List<Commodity> banners = comService.getBannerComs();
map.put("banners", banners);

5).view层

将home.jsp中banner部分的代码替换为:

<div class="banner">
		<div class="col-sm-3 banner-mat">
			<img class="img-responsive" src="images/ba1.jpg" alt="">
		</div>


		<div class="col-sm-6 matter-banner">
			<div class="slider">
				<div class="callbacks_container">
					<ul class="rslides" id="slider">
						<c:forEach items="${banners}" var="b" varStatus="vs">
							<li><img src="${b.picurl}" alt=""></li>
						</c:forEach>
					</ul>
				</div>
			</div>
		</div>
		<div class="col-sm-3 banner-mat">
			<img class="img-responsive" src="images/ba1.jpg" alt="">
		</div>
		<div class="clearfix"></div>
	</div>


  • 3.最新商品列表


因为前边已经新建了商品相关的对象和dao层等,之后对商品的查询只需要在之前的基础上添加即可,dao层的sql可以实现最新商品的查询,所以不用修改dao层。



1).service层,ComService.class添加函数:

public List<Commodity> getRecentComs();



ComServiceImpl.class添加函数:

@Override
public List<Commodity> getRecentComs() {
	HashMap map = new HashMap();
	map.put("categoryId", 4);
	return dao.getComs(map);
}


2).controller层,HomeController.class的index函数里添加:

List<Commodity> recents = comService.getRecentComs();
map.put("recents", recents);

3)view层,将home.jsp中content的内容替换如下:

<div class="content">
		<div class="container">
			<div class="content-top">
				<h1>Recent Products</h1>
				<div class="content-top1">
					<c:forEach items="${recents}" var="r">
						<div class="col-md-3 col-md2">
							<div class="col-md1 simpleCart_shelfItem">
								<a href="com/detail.do?id=${r.id}"> <img class="img-responsive"
									src="${r.picurl}" alt="" /> </a>
								<h3>
									<a href="com/detail.do?id=${r.id}">${r.name}</a>
								</h3>
								<div class="price">
									<h5 class="item_price">$${r.price}</h5>
									<a href="javascript:void(0)" οnclick="addToCart('${r.id}')"
										class="item_add">Add To Cart</a>
									<div class="clearfix"></div>
								</div>
							</div>
						</div>
					</c:forEach>
					<div class="clearfix"></div>
				</div>
			</div>
		</div>
	</div>

4).测试,http://localhost:8080/myecom/index.do。
至此,电商首页的跟查询相关的主要工作完成。



版权声明:本文为qr_828原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。