Table of Contents
Today’s task
1.1 Function of classification module:
1.1.1 Function of query classification:
Home Java javaTutorial 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

Jun 23, 2017 pm 04:28 PM
javaweb web study Actual combat notes online store

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;
Copy after login

1.1.2.2 Function implementation:

1. Directly query all categories:

CategoryDao categoryDao = new CategoryDaoImpl();

List<Category>  list = categoryDao.findAll();
Copy after login
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");

});
Copy after login

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; 

    }
Copy after login

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;
Copy after login

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

        }
Copy after login

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

    }
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to delete Xiaohongshu notes How to delete Xiaohongshu notes Mar 21, 2024 pm 08:12 PM

How to delete Xiaohongshu notes? Notes can be edited in the Xiaohongshu APP. Most users don’t know how to delete Xiaohongshu notes. Next, the editor brings users pictures and texts on how to delete Xiaohongshu notes. Tutorial, interested users come and take a look! Xiaohongshu usage tutorial How to delete Xiaohongshu notes 1. First open the Xiaohongshu APP and enter the main page, select [Me] in the lower right corner to enter the special area; 2. Then in the My area, click on the note page shown in the picture below , select the note you want to delete; 3. Enter the note page, click [three dots] in the upper right corner; 4. Finally, the function bar will expand at the bottom, click [Delete] to complete.

What should I do if the notes I posted on Xiaohongshu are missing? What's the reason why the notes it just sent can't be found? What should I do if the notes I posted on Xiaohongshu are missing? What's the reason why the notes it just sent can't be found? Mar 21, 2024 pm 09:30 PM

As a Xiaohongshu user, we have all encountered the situation where published notes suddenly disappeared, which is undoubtedly confusing and worrying. In this case, what should we do? This article will focus on the topic of &quot;What to do if the notes published by Xiaohongshu are missing&quot; and give you a detailed answer. 1. What should I do if the notes published by Xiaohongshu are missing? First, don't panic. If you find that your notes are missing, staying calm is key and don't panic. This may be caused by platform system failure or operational errors. Checking release records is easy. Just open the Xiaohongshu App and click &quot;Me&quot; → &quot;Publish&quot; → &quot;All Publications&quot; to view your own publishing records. Here you can easily find previously published notes. 3.Repost. If found

How to add product links in notes in Xiaohongshu Tutorial on adding product links in notes in Xiaohongshu How to add product links in notes in Xiaohongshu Tutorial on adding product links in notes in Xiaohongshu Mar 12, 2024 am 10:40 AM

How to add product links in notes in Xiaohongshu? In the Xiaohongshu app, users can not only browse various contents but also shop, so there is a lot of content about shopping recommendations and good product sharing in this app. If If you are an expert on this app, you can also share some shopping experiences, find merchants for cooperation, add links in notes, etc. Many people are willing to use this app for shopping, because it is not only convenient, but also has many Experts will make some recommendations. You can browse interesting content and see if there are any clothing products that suit you. Let’s take a look at how to add product links to notes! How to add product links to Xiaohongshu Notes Open the app on the desktop of your mobile phone. Click on the app homepage

PHP Practical: Code Example to Quickly Implement Fibonacci Sequence PHP Practical: Code Example to Quickly Implement Fibonacci Sequence Mar 20, 2024 pm 02:24 PM

PHP Practice: Code Example to Quickly Implement the Fibonacci Sequence The Fibonacci Sequence is a very interesting and common sequence in mathematics. It is defined as follows: the first and second numbers are 0 and 1, and from the third Starting with numbers, each number is the sum of the previous two numbers. The first few numbers in the Fibonacci sequence are 0,1,1.2,3,5,8,13,21,...and so on. In PHP, we can generate the Fibonacci sequence through recursion and iteration. Below we will show these two

Let's learn how to input the root number in Word together Let's learn how to input the root number in Word together Mar 19, 2024 pm 08:52 PM

When editing text content in Word, you sometimes need to enter formula symbols. Some guys don’t know how to input the root number in Word, so Xiaomian asked me to share with my friends a tutorial on how to input the root number in Word. Hope it helps my friends. First, open the Word software on your computer, then open the file you want to edit, and move the cursor to the location where you need to insert the root sign, refer to the picture example below. 2. Select [Insert], and then select [Formula] in the symbol. As shown in the red circle in the picture below: 3. Then select [Insert New Formula] below. As shown in the red circle in the picture below: 4. Select [Radical Formula], and then select the appropriate root sign. As shown in the red circle in the picture below:

Revealing the appeal of C language: Uncovering the potential of programmers Revealing the appeal of C language: Uncovering the potential of programmers Feb 24, 2024 pm 11:21 PM

The Charm of Learning C Language: Unlocking the Potential of Programmers With the continuous development of technology, computer programming has become a field that has attracted much attention. Among many programming languages, C language has always been loved by programmers. Its simplicity, efficiency and wide application make learning C language the first step for many people to enter the field of programming. This article will discuss the charm of learning C language and how to unlock the potential of programmers by learning C language. First of all, the charm of learning C language lies in its simplicity. Compared with other programming languages, C language

How to enable administrative access from the cockpit web UI How to enable administrative access from the cockpit web UI Mar 20, 2024 pm 06:56 PM

Cockpit is a web-based graphical interface for Linux servers. It is mainly intended to make managing Linux servers easier for new/expert users. In this article, we will discuss Cockpit access modes and how to switch administrative access to Cockpit from CockpitWebUI. Content Topics: Cockpit Entry Modes Finding the Current Cockpit Access Mode Enable Administrative Access for Cockpit from CockpitWebUI Disabling Administrative Access for Cockpit from CockpitWebUI Conclusion Cockpit Entry Modes The cockpit has two access modes: Restricted Access: This is the default for the cockpit access mode. In this access mode you cannot access the web user from the cockpit

Learn the main function in Go language from scratch Learn the main function in Go language from scratch Mar 27, 2024 pm 05:03 PM

Title: Learn the main function in Go language from scratch. As a simple and efficient programming language, Go language is favored by developers. In the Go language, the main function is an entry function, and every Go program must contain the main function as the entry point of the program. This article will introduce how to learn the main function in Go language from scratch and provide specific code examples. 1. First, we need to install the Go language development environment. You can go to the official website (https://golang.org

See all articles