• 极客专栏正式上线!欢迎访问 https://www.jikewenku.com/topic.html
  • 极客专栏正式上线!欢迎访问 https://www.jikewenku.com/topic.html

Javaweb网上商城项目实战(16)实现首页热门商品、最新商品查询

项目实战 Geekerstar 11个月前 (05-26) 923次浏览 已收录 0个评论 扫描二维码
文章目录[隐藏]

分析SQL语句编写

#查询商品表中最新的9件商品信息
SELECT * FROM product WHERE pflag=0 ORDER BY pdate DESC LIMIT 0 ,9 

#查询商品表中最热,最新的9件商品信息
SELECT * FROM product WHERE pflag=0 AND is_hot=1 ORDER BY pdate DESC LIMIT 0 ,9 

原理分析

具体实现

建立商品模块相关程序

1、domain包下根据数据库表创建Product类,生成get和set方法、toString方法,无参有参构造。
Product.java

package com.geekerstar.store.domain;

import java.util.Date;

public class Product {
	private String pid; //商品编号
	private String pname; //商品名称
	private double market_price;//商品市场价格
	private double shop_price;//商品商场价格
	private String pimage;//商品图片路径
	private Date pdate;//商品上架日期
	private int is_hot;//商品是否热门
	private String pdesc;//商品描述
	private int pflag;//商品是否在货架上  0:在货架上 1:下架
	private String cid;//商品所在分类id
	public String getPid() {
		return pid;
	}
	public void setPid(String pid) {
		this.pid = pid;
	}
	public String getPname() {
		return pname;
	}
	public void setPname(String pname) {
		this.pname = pname;
	}
	public double getMarket_price() {
		return market_price;
	}
	public void setMarket_price(double market_price) {
		this.market_price = market_price;
	}
	public double getShop_price() {
		return shop_price;
	}
	public void setShop_price(double shop_price) {
		this.shop_price = shop_price;
	}
	public String getPimage() {
		return pimage;
	}
	public void setPimage(String pimage) {
		this.pimage = pimage;
	}
	public Date getPdate() {
		return pdate;
	}
	public void setPdate(Date pdate) {
		this.pdate = pdate;
	}
	public int getIs_hot() {
		return is_hot;
	}
	public void setIs_hot(int is_hot) {
		this.is_hot = is_hot;
	}
	public String getPdesc() {
		return pdesc;
	}
	public void setPdesc(String pdesc) {
		this.pdesc = pdesc;
	}
	public int getPflag() {
		return pflag;
	}
	public void setPflag(int pflag) {
		this.pflag = pflag;
	}
	public String getCid() {
		return cid;
	}
	public void setCid(String cid) {
		this.cid = cid;
	}
	public Product() {
		// TODO Auto-generated constructor stub
	}
	public Product(String pid, String pname, double market_price, double shop_price, String pimage, Date pdate,
			int is_hot, String pdesc, int pflag, String cid) {
		super();
		this.pid = pid;
		this.pname = pname;
		this.market_price = market_price;
		this.shop_price = shop_price;
		this.pimage = pimage;
		this.pdate = pdate;
		this.is_hot = is_hot;
		this.pdesc = pdesc;
		this.pflag = pflag;
		this.cid = cid;
	}
	@Override
	public String toString() {
		return "Product [pid=" + pid + ", pname=" + pname + ", market_price=" + market_price + ", shop_price="
				+ shop_price + ", pimage=" + pimage + ", pdate=" + pdate + ", is_hot=" + is_hot + ", pdesc=" + pdesc
				+ ", pflag=" + pflag + ", cid=" + cid + "]";
	}
	
	
}

2、Servlet包下修改IndexServlet.java

package com.geekerstar.store.web.servlet;

import java.util.List;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.geekerstar.store.web.base.BaseServlet;
import com.geekerstar.store.domain.Product;
import com.geekerstar.store.service.ProductService;
import com.geekerstar.store.service.serviceImp.ProductServiceImp;

public class IndexServlet extends BaseServlet {
	@Override
	public String execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
		//调用业务层功能:获取全部分类信息,返回集合
		//CategoryService CategoryService=new CategoryServiceImp();
		//List<Category> list=CategoryService.getAllCats();
		//将返回的集合放入request
		//request.setAttribute("allCats", list);
		
		//调用业务层查询最新商品,查询最热商品,返回2个集合
		ProductService ProductService=new ProductServiceImp();
		List<Product> list01=ProductService.findHots();
		List<Product> list02=ProductService.findNews();
		//将2个集合放入到request
		request.setAttribute("hots", list01);
		request.setAttribute("news", list02);
		//转发到真实的首页
		return "/jsp/index.jsp";
	}
}

3、dao包下创建ProductDao及其实现类
ProductDao.java

package com.geekerstar.store.dao;

import java.util.List;

import com.geekerstar.store.domain.Product;

public interface ProductDao {

	List<Product> findHots()throws Exception;

	List<Product> findNews()throws Exception;

}

ProductDaoImp.java

package com.geekerstar.store.dao.daoImp;

import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.geekerstar.store.dao.ProductDao;
import com.geekerstar.store.domain.Product;
import com.geekerstar.store.utils.JDBCUtils;

public class ProductDaoImp implements ProductDao {

	@Override
	public List<Product> findHots() throws Exception {
		String sql="SELECT * FROM product WHERE pflag=0 AND is_hot=1 ORDER BY pdate DESC LIMIT 0 ,9 ";
		QueryRunner qr=new QueryRunner(JDBCUtils.getDataSource());
		return qr.query(sql, new BeanListHandler<Product>(Product.class));
	}

	@Override
	public List<Product> findNews() throws Exception {
		String sql="SELECT * FROM product WHERE pflag=0 ORDER BY pdate DESC LIMIT 0 , 9 ";
		QueryRunner qr=new QueryRunner(JDBCUtils.getDataSource());
		return qr.query(sql, new BeanListHandler<Product>(Product.class));
		
	}

}

4、Service包下创建ProductService及其实现类
ProductService.java

package com.geekerstar.store.service;

import java.util.List;

import com.geekerstar.store.domain.Product;

public interface ProductService {

	List<Product> findHots()throws Exception;

	List<Product> findNews()throws Exception;

}

ProductServiceImp.java

package com.geekerstar.store.service.serviceImp;

import java.util.List;

import com.geekerstar.store.dao.ProductDao;
import com.geekerstar.store.dao.daoImp.ProductDaoImp;
import com.geekerstar.store.domain.Product;
import com.geekerstar.store.service.ProductService;

public class ProductServiceImp implements ProductService {

	ProductDao ProductDao=new ProductDaoImp();
	

	@Override
	public List<Product> findHots() throws Exception {
		return ProductDao.findHots();
	}

	@Override
	public List<Product> findNews() throws Exception {
		return ProductDao.findNews();
	}

}

获取最新/最热9件商品信息

修改我们项目的首页index.jsp。

根据分析,我们只需要保留一个col-md-2的div就行了,然后采用遍历的方式输出相关商品信息。由于index.jsp太长了这里就不完整贴出来了,只展示我们修改的部分

获取最热商品信息

<c:forEach items="${hots}" var="p">
	<div class="col-md-2"
		style="text-align: center; height: 200px; padding: 10px 0px;">
		<a href="product_info.htm"> <img src="${pageContext.request.contextPath}/${p.pimage}"
			width="130" height="130" style="display: inline-block;">
		</a>
		<p>
			<a href="product_info.html" style='color: #666'>${p.pname}</a>
		</p>
		<p>
			<font color="#E4393C" style="font-size: 16px">¥${p.shop_price }</font>
		</p>
	</div>
</c:forEach>

获取最新商品信息

<c:forEach items="${news}" var="p">
	<div class="col-md-2"
		style="text-align: center; height: 200px; padding: 10px 0px;">
		<a href="product_info.htm"> <img src="${pageContext.request.contextPath}/${p.pimage}"
			width="130" height="130" style="display: inline-block;">
		</a>
		<p>
			<a href="product_info.html" style='color: #666'>${p.pname}</a>
		</p>
		<p>
			<font color="#E4393C" style="font-size: 16px">¥${p.shop_price }</font>
		</p>
	</div>
</c:forEach>

效果演示

运行项目,可以看到我们已经获取到了最新最热的商品信息了

源码下载

下载地址

导航目录

查看导航
丨极客文库, 版权所有丨如未注明 , 均为原创丨
本网站采用知识共享署名-非商业性使用-相同方式共享 3.0 中国大陆许可协议进行授权
转载请注明原文链接:Javaweb网上商城项目实战(16)实现首页热门商品、最新商品查询
喜欢 (0)
[247507792@qq.com]
分享 (0)
Geekerstar
关于作者:
本站技术支持

您必须 登录 才能发表评论!

  • 精品技术教程
  • 编程资源分享
  • 问答交流社区
  • 极客文库知识库

客服QQ


QQ:2248886839


工作时间:09:00-23:00