您好,欢迎访问一九零五行业门户网

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

今日任务完成分类模块的功能
完成商品模块的功能
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:
   * 页面跳转:
以上就是java商城实战之异步加载分类、redis缓存分类和显示商品的详细内容。
其它类似信息

推荐信息