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

分析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 list=CategoryService.getAllCats();
		//将返回的集合放入request
		//request.setAttribute("allCats", list);
		
		//调用业务层查询最新商品,查询最热商品,返回2个集合
		ProductService ProductService=new ProductServiceImp();
		List list01=ProductService.findHots();
		List 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 findHots()throws Exception;

	List 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 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.class));
	}

	@Override
	public List 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.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 findHots()throws Exception;

	List 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 findHots() throws Exception {
		return ProductDao.findHots();
	}

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

}

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

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

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

获取最热商品信息


	

${p.pname}

¥${p.shop_price }

获取最新商品信息


	

${p.pname}

¥${p.shop_price }

效果演示

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

源码下载

[dm href=’https://www.jikewenku.com/product/1700.html’]下载地址[/dm]

导航目录

[dm href=’https://www.jikewenku.com/project/2786.html’]查看导航[/dm]

本站所有文章均由网友分享,仅用于参考学习用,请勿直接转载,如有侵权,请联系网站客服删除相关文章。若由于商用引起版权纠纷,一切责任均由使用者承担
极客文库 » Javaweb网上商城项目实战(16)实现首页热门商品、最新商品查询

Leave a Reply

欢迎加入「极客文库」,成为原创作者从这里开始!

立即加入 了解更多