【JAVAWEB學習筆記】網上商城實戰2:非同步載入分類、Redis緩衝分類和顯示商品,javawebredis

來源:互聯網
上載者:User

【JAVAWEB學習筆記】網上商城實戰2:非同步載入分類、Redis緩衝分類和顯示商品,javawebredis
網上商城實戰2

今日任務

  • 完成分類別模組的功能
  • 完成商品模組的功能
1.1      分類別模組的功能:1.1.1    查詢分類的功能:

1.1.2    查詢分類的代碼實現:1.1.2.1  建立表:
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  功能實現:

1.直接查詢所有分類:

CategoryDao categoryDao = new CategoryDaoImpl();List<Category>  list = categoryDao.findAll();

2.非同步載入分類:

$(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.使用緩衝技術:對程式進行最佳化.

* 緩衝:其實就是記憶體中的一塊空間.可以使用緩衝將資料來源中的資料拿到,存入到記憶體中.後期獲得資料的話 從緩衝中進行獲得.

* Memcache   :

* EHCache :是Hibernate常使用的二級緩衝的外掛程式.

* Redis      :

* 使用ehcache:

   * 引入jar包:

   * 引入設定檔:

 

  // 業務層查詢所有分類的方法:    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      前台頁面上的商品顯示:1.2.1    商品顯示準備工作:1.2.1.1  建立表:
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  建立類:

 

1.2.2    首頁上的熱門商品的顯示和最新商品的顯示

       

 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    商品詳情的顯示

    

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    顯示某個分類下的商品:

1.在首頁上點擊分類的連結:

2.提交到Servlet:

    * 接收參數:分類的ID

    * 當前頁面:當前頁數1

    * 調用業務層:

         * 封裝PageBean:

   * 頁面跳轉:

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.