"Javaweb Study notes" online Shop 2: Asynchronous load classification, Redis cache classification and display products

Source: Internet
Author: User

Online Mall Combat 2

Today's mission

    • Complete the function of the classification module
    • Complete the function of the commodity module
1.1 Function of Classification module: the function of 1.1.1 query classification:

1.1.2 The code implementation of the query classification: 1.1.2.1 Create the table:
CREATE TABLE ' category ' (  varchar(+ notNULL,  varchar (DEFAULTNULL,  PRIMARYKEY  (' Cid ') ENGINE=DEFAULT CHARSET=UTF8;

1.1.2.2 Feature Implementation:

1. Check all categories directly:

New Categorydaoimpl (); List<Category>  list = Categorydao.findall ();

2. Asynchronous Load Classification:

$ (function() {    $.post ("/store_v2.0/categoryservlet", {        "method": "FindAll"      function (data) {        function(i, N) {            $ ("#menu"). Append ("<li><a href= ' # ' > "+ n.cname +" </a></li> ");        });    " JSON ");});

3. Using the caching technology: Optimize the program.

* Cache: In fact, it is a piece of space in memory. You can use the cache to get the data from the data source and deposit it into memory. The subsequent acquisition of the data is obtained from the cache.

* Memcache:

* EHCache: is a two-level cache plug-in that hibernate often uses.

* Redis:

* Using Ehcache:

* Introduction of JAR Packages:

* Introduction of configuration files:

  //the business layer queries all classification methods:     PublicList<category> FindAll ()throwsSQLException {/** CategoryDao CategoryDao = new Categorydaoimpl (); return * Categorydao.findall (); */        /*** Query data from the cache: * * with data, directly return the cached data.         * * If not, query the database and the data is stored in the cache. */List<Category> list =NULL; //to query from the cache:CacheManager CacheManager=CacheManager. Create (Categoryserviceimpl.class. getClassLoader (). getResourceAsStream ("Ehcache.xml")); Cache Cache= Cachemanager.getcache ("Categorycache"); Element element= Cache.get ("list"); if(Element! =NULL){            //there is data in the cache:System.out.println ("There is data in the cache ..."); List= (list<category>) Element.getobjectvalue (); }Else{            //There is no data in the cache:System.out.println ("No data in cache ..."); CategoryDao CategoryDao=NewCategorydaoimpl (); List=Categorydao.findall (); Element e=NewElement ("list", list); //Cache.Cache.put (e); }        returnlist; }

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 'DoubleDEFAULT NULL, ' Shop_price 'DoubleDEFAULT NULL, ' pimage ' varchar (200default NULL, ' pdate ' datetime DEFAULT null, ' Is_hot 'int(one) DEFAULT NULL,--1: Popular ' pdesc ' varchar (255) DEFAULT NULL, ' Pflag 'int(one) DEFAULT NULL,--1: Lower frame ' cid ' varchar (32DEFAULT 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 a class:

1.2.2 display of popular items on the first page and display of the latest products

Productservice Productservice =NewProductserviceimpl (); Try {            //Search for popular products:List<Product> hotList =Productservice.findbyhot (); //Check the latest products:List<Product> NewList =productservice.findbynew (); Req.setattribute ("HotList", hotList); Req.setattribute ("NewList", NewList); } Catch(SQLException e) {e.printstacktrace (); Throw Newruntimeexception (); }

1.2.3 Display of merchandise details

 PublicString FindByID (httpservletrequest req,httpservletresponse resp) {//Receive parameters:String pid= Req.getparameter ("pid"); //calling the business layer:Productservice Productservice=NewProductserviceimpl (); Try{Product Product=Productservice.findbyid (PID); Req.setattribute ("Product", product); } Catch(SQLException e) {e.printstacktrace (); Throw Newruntimeexception (); }        //page Jump        return"/jsp/product_info.jsp"; }

1.2.4 Displays the items under a category:

1. Click on the category link on the home page:

2. Submit to Servlet:

* Receive parameters: ID of the category

* Current Page: Current pages 1

* Call the Business layer:

* Package Pagebean:

* Page Jump:

"Javaweb Study notes" online Shop 2: Asynchronous load classification, Redis cache classification and display products

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.