完成分类模块的功能
完成商品模块的功能

CREATE TABLE `category` ( `cid` varchar(32) NOT NULL, `cname` varchar(20) DEFAULT NULL, PRIMARY KEY (`cid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
立即学习“Java免费学习笔记(深入)”;
1.直接查询所有分类:
CategoryDao categoryDao = new CategoryDaoImpl(); List<Category> list = categoryDao.findAll();
2.异步加载分类:
$(function() {
$.post("/store_v2.0/CategoryServlet", {"method" : "findAll"}, function(data) {
$.each(data, function(i, n) {
$("#menu").append("<li><a href='#'>" + n.cname + "</a></li>");
});
}, "json");
});
立即学习“Java免费学习笔记(深入)”;
3.使用缓存技术:对程序进行优化.
* 缓存:其实就是内存中的一块空间.可以使用缓存将数据源中的数据拿到,存入到内存中.后期获得数据的话 从缓存中进行获得.
* Memcache :
* EHCache :是Hibernate常使用的二级缓存的插件.
* Redis :
* 使用ehcache:
* 引入jar包:
* 引入配置文件:
// 业务层查询所有分类的方法:public List<Category> findAll() throws SQLException {/* * CategoryDao categoryDao = new CategoryDaoImpl(); return
* categoryDao.findAll(); *//** * 从缓存中查询数据:
* * 有数据,直接将缓存的数据返回.
* * 如果没有,查询数据库,数据存入到缓存中. */List<Category> list = null; // 从缓存中进行查询:CacheManager cacheManager = CacheManager
.create(CategoryServiceImpl.class.getClassLoader().getResourceAsStream("ehcache.xml"));
Cache cache = cacheManager.getCache("categoryCache");
Element element = cache.get("list");if(element != null){// 缓存中有数据:System.out.println("缓存中有数据...");
list = (List<Category>) element.getObjectValue();
}else{// 缓存中没有数据:System.out.println("缓存中没有数据...");
CategoryDao categoryDao = new CategoryDaoImpl();
list = categoryDao.findAll();
Element e = new Element("list", list);// cache.cache.put(e);
}return list;
}
立即学习“Java免费学习笔记(深入)”;
CREATE TABLE `product` ( `pid` varchar(32) NOT NULL, `pname` varchar(50) DEFAULT NULL, `market_price` double DEFAULT NULL, `shop_price` double DEFAULT NULL, `pimage` varchar(200) DEFAULT NULL, `pdate` datetime DEFAULT NULL, `is_hot` int(11) DEFAULT NULL,-- 1:热门 `pdesc` varchar(255) DEFAULT NULL, `pflag` int(11) DEFAULT NULL,-- 1:下架 `cid` varchar(32) DEFAULT NULL, PRIMARY KEY (`pid`), KEY `sfk_0001` (`cid`), CONSTRAINT `sfk_0001` FOREIGN KEY (`cid`) REFERENCES `category` (`cid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
立即学习“Java免费学习笔记(深入)”;
ProductService productService = new ProductServiceImpl();try {// 查询热门商品:List<Product> hotList = productService.findByHot();// 查询最新商品:List<Product> newList = productService.findByNew();
req.setAttribute("hotList",hotList);
req.setAttribute("newList",newList);
} catch (SQLException e) {
e.printStackTrace();throw new RuntimeException();
}
立即学习“Java免费学习笔记(深入)”;
public String findById(HttpServletRequest req,HttpServletResponse resp){// 接收参数:String pid = req.getParameter("pid");// 调用业务层:ProductService productService = new ProductServiceImpl();try {
Product product = productService.findById(pid);
req.setAttribute("product",product);
} catch (SQLException e) {
e.printStackTrace();throw new RuntimeException();
}// 页面跳转return "/jsp/product_info.jsp";
}
立即学习“Java免费学习笔记(深入)”;
1.在首页上点击分类的链接:
2.提交到Servlet:
* 接收参数:分类的ID
* 当前页面:当前页数1
* 调用业务层:
* 封装PageBean:
* 页面跳转:
以上就是JAVA商城实战之异步加载分类、Redis缓存分类和显示商品的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号