Rumah  >  Artikel  >  Java  >  JAVA商城实战之异步加载分类、Redis缓存分类和显示商品

JAVA商城实战之异步加载分类、Redis缓存分类和显示商品

巴扎黑
巴扎黑asal
2017-06-23 16:28:192310semak imbas

今日任务

  • 完成分类模块的功能

  • 完成商品模块的功能

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=&#39;#&#39;>" + 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:

   * 页面跳转:

 

 

Atas ialah kandungan terperinci JAVA商城实战之异步加载分类、Redis缓存分类和显示商品. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:hibernate_关联映射_一对多Artikel seterusnya:用try语句关闭资源实例