[JAVAWEB learning notes] online shopping mall practice 2: asynchronous loading classification, Redis cache classification and display product, javawebredis

Source: Internet
Author: User

[JAVAWEB learning notes] online shopping mall practice 2: asynchronous loading classification, Redis cache classification and display product, javawebredis
Online mall practice 2

Today's task

  • Complete the classification module function
  • Complete product module functions
1.1 functions of the classification module: 1.1.1 query classification:

1.1.2 code implementation for querying categories: 1.1.2.1 create a 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='#'>" + n.cname + "</a></li>");        });    }, "json");});

 

 

3. Use the cache technology to optimize the program.

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

* Memcache:

* EHCache: a plug-in that is frequently used by Hibernate.

* Redis:

* Use ehcache:

* Introduce the jar package:

* Introduce the configuration file:

 

// Method for querying all types at the business layer: public List <Category> findAll () throws SQLException {/** CategoryDao categoryDao = new CategoryDaoImpl (); return * categoryDao. findAll (); * // *** query data from the cache: ** if there is data, the cached data is directly returned. ** if no, query the database and store the data in the cache. */List <Category> list = null; // query from the cache: CacheManager cacheManager = CacheManager. create (CategoryServiceImpl. class. getClassLoader (). getResourceAsStream ("ehcache. xml "); Cache c Ache = cacheManager. getCache ("categoryCache"); Element element = cache. get ("list"); if (element! = Null) {// data in the cache: System. out. println ("the cache contains data... "); list = (List <Category>) element. getObjectValue ();} else {// No data in the cache: System. out. println ("no data in cache... "); 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-end page: 1.2.1 prepare for product display: 1.2.1.1 create a 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: popular 'pdesc' varchar (255) default null, 'pflag' int (11) default null, -- 1: dismounting 'cid' varchar (32) default null, primary key ('pid '), KEY 'sfk _ 000000' ('cid'), CONSTRAINT 'sfk _ 000000' foreign key ('cid ') REFERENCES 'category '('cid') ENGINE = InnoDB default charset = utf8;

 

1.2.1.2 create a class:

 

1.2.2 display of popular products and latest products on the homepage

ProductService productService = new ProductServiceImpl (); try {// query popular products: List <Product> hotList = productService. findByHot (); // query the latest Product: 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) {// receiving parameter: String pid = req. getParameter ("pid"); // call the business layer: ProductService productService = new ProductServiceImpl (); try {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 items under a specific category:

1. Click the category link on the homepage:

2. Submit to Servlet:

* Receiving parameter: Category ID

* Current page: Current page number 1

* Call the business layer:

* Encapsulate PageBean:

* Page Jump:

 

 

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.