Home  >  Article  >  Java  >  JAVA Mall Practical Asynchronous Loading of Categories, Redis Cache Categories and Display of Products

JAVA Mall Practical Asynchronous Loading of Categories, Redis Cache Categories and Display of Products

巴扎黑
巴扎黑Original
2017-06-23 16:28:192342browse

Today’s task

  • Complete the functions of the classification module

  • Complete the functions of the product module

1.1 Function of classification module:

1.1.1 Function of query classification:

##1.1.2 Code implementation of query classification:

1.1.2.1 Create table:

CREATE TABLE `category` (

  `cid` varchar(32) NOT NULL,

  `cname` varchar(20) DEFAULT NULL,  PRIMARY KEY (`cid`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

1.1.2.2 Function implementation:

1. Directly query all categories:

CategoryDao categoryDao = new CategoryDaoImpl();

List<Category>  list = categoryDao.findAll();
2. Asynchronous loading classification:

$(function() {

    $.post("/store_v2.0/CategoryServlet", {"method" : "findAll"}, function(data) {

        $.each(data, function(i, n) {

            $("#menu").append("<li><a href=&#39;#&#39;>" + n.cname + "</a></li>");

        });

    }, "json");

});

3. Use caching technology: optimize the program.

* Cache: It is actually a space in the memory. You can use the cache to get the data from the data source and store it in the memory. If the data is obtained later, it will be obtained from the cache.

* Memcache :

* EHCache: It is a second-level cache plug-in commonly used by Hibernate.

* Redis :

* Use ehcache:

* Introducing the jar package:

* Introducing the configuration file:

  // 业务层查询所有分类的方法: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; 

    }

1.2 Product display on the front page:

1.2.1 Product display preparation:

1.2.1.1 Create table:

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;

1.2.1.2 Create class:

1.2.2 Display of popular products and latest products on the homepage

 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();

        }

1.2.3 Display of product details

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";

    }

1.2.4 Display of products under a certain category:

1. Click the category link on the homepage:

2. Submit to Servlet:

* Receive parameters: Category ID

* Current page: Current page number 1

* Call the business layer:

* Encapsulate PageBean:

* Page jump:

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn