Java Web 网络商城案例演示九(首页热门商品和最新商品显示)
一、分析SQL语句的编写
查询数据库商品表当中最新的9件商品信息
-- 查询商品表中最新的9件商品信息
SELECT * FROM product WHERE pflag=0 ORDER BY pdate DESC LIMIT 0,9;
-- 查询来自(product的当中排序按降序0到9 并且有货 )
-- 查询商品表中最热最新的9件商品信息
SELECT * FROM product WHERE pflag=0 AND is_hot=1 ORDER BY pdate DESC LIMIT 0,9;
二、原理分析
三、步骤实现
1、准备工作(放文页面就发送请求):
2、在IndexServlet的execute的方法当中实现功能
调用业务层查询最新商品,查询最热商品,返回两个集合
将2个集合放入到request
转发到真实的首页
3、建立商品模块的相关的程序
ProdectServlet ProdectService ProdectServiceImpl ProductDao ProductDaoImpl Product
ProdectServlet
ProdectService
public interface ProductService {
List<Product> findHots() throws Exception;
List<Product> findNews() throws Exception;
}
ProdectServiceImpl
public class ProductServiceImpl implements ProductService {
ProductDao productDao = new ProductDaoImpl();
@Override
public List<Product> findHots() throws Exception {
// TODO Auto-generated method stub
return productDao.findHots();
}
@Override
public List<Product> findNews() throws Exception {
// TODO Auto-generated method stub
return productDao.findNews();
}
}
ProductDao
public interface ProductDao {
List<Product> findHots() throws Exception;
List<Product> findNews() throws Exception;
}
ProductDaoImpl
public class ProductDaoImpl implements ProductDao {
@Override
public List<Product> findHots() throws Exception {
// TODO Auto-generated method stub
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 {
// TODO Auto-generated method stub
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));
}
}
3、调用service,和dao
IndexServlet当中
public class IndexServlet extends BaseServlet {
@Override
public String execute(HttpServletRequest request, HttpServletResponse resp) throws Exception {
/*
* // 调用业务层的一个功能,获取全部的分类信息,返回集合 CategoryService categoryService = new
* CategoryServiceImpl(); List<Category> list = categoryService.getAllCats(); //
* 将返回的集合放入request内 request.setAttribute("allCats", list); // 转发到真实的首页
*/
//调用业务层查询最新商品,查询最热商品,返回两个集合
ProductService productService = new ProductServiceImpl();
List<Product> list01 = productService.findHots();
List<Product> list02 = productService.findNews();
//将2个集合放入到request
request.setAttribute("hots", list01);
request.setAttribute("news", list02);
//转发到真实的首页
return "/jsp/index.jsp";
}
}
4、在index.jsp当中所以遍历的方式获取到IndexServlet当中的数据(删除重复的数据)
<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>