Complete the functions of the classification module
Complete the functions of the product module
CREATE TABLE `category` ( `cid` varchar(32) NOT NULL, `cname` varchar(20) DEFAULT NULL, PRIMARY KEY (`cid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CategoryDao categoryDao = new CategoryDaoImpl(); List<Category> list = categoryDao.findAll();
$(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"); });
// 业务层查询所有分类的方法: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; }
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;
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(); }
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"; }
The above is the detailed content of JAVA Mall Practical Asynchronous Loading of Categories, Redis Cache Categories and Display of Products. For more information, please follow other related articles on the PHP Chinese website!