栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JavaWeb搭建网上图书商城毕业设计

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JavaWeb搭建网上图书商城毕业设计

  以前一直接触.net相关的web开发,现在猛然使用javaWeb还是很不习惯,就连搭个框架也是第一次。

一、谈谈项目架构
  一开始接触.net相关的开发所以对于.net相关的开发还是比较熟悉的,但我在学校学的java方向的开发,而我打算把这两种平台结合起来,使用java做后台也就是服务提供者,将所有业务逻辑在java平台完成并使用我比较熟悉的.net做Web端的开发。这样一来安卓app,web端就都有了。客户端统一通过分布式框架调用服务。找了很久最终选择了Hprose,这一轻量级、跨语言、跨平台、无侵入式、高性能动态远程对象调用引擎库。之所以选择它一方面是因为学习成本低,另一方面是它的跨平台调用非常轻松高效,因为我们要使用.net做web需要调用java发布的服务!大概看了一下Hprose的文档,发现使用内置的HproseServlet发布服务开发速度比较快也比较简单,所以准备使用这种方式发布服务。可问题来了,传统的ssh架构感觉有点重了,准备使用.net开发web端所以感觉没有必要整合Struts,于是就是hibernate+spring+hprose这种架构。

二、数据库设计

  一个小网上书城,所以设计的还有欠缺,以实用为主,主要是练手java开发~~。所以使用了navicat简单设计了一下,不过没有设计表关联,取而代之的是后来一个一个添加关系的,发现这个设计工具有点问题,图示:

其实表关联一看就能看出来~~,接下来就是hibernate一些映射了,同样也是使用插件生成model和映射文件

稍作修改就是这样--

三、spring3+hibernate4配置
  因为model和映射文件是自动生成所以稍加配置就好,需要注意的是复合主键的设置,自动生成的会把复合主键对应一个复合模型。如商品评论表的复合主键类型

package com.book.model;
// Generated 2015-11-2 9:07:06 by Hibernate Tools 4.0.0.Final

import java.util.Date;


public class CommentsPk implements java.io.Serializable {

  private Book book;
  private User user;
  private Date commentsDate;

  public CommentsPk() {
  }

  public CommentsPk(Book book, User user, Date commentsDate) {
    this.book = book;
    this.user = user;
    this.commentsDate = commentsDate;
  }

  public Book getBook() {
    return this.book;
  }

  public void setBook(Book book) {
    this.book = book;
  }

  public User getUser() {
    return this.user;
  }

  public void setUser(User user) {
    this.user = user;
  }

  public Date getCommentsDate() {
    return this.commentsDate;
  }

  public void setCommentsDate(Date commentsDate) {
    this.commentsDate = commentsDate;
  }

  public boolean equals(Object other) {
    if ((this == other))
      return true;
    if ((other == null))
      return false;
    if (!(other instanceof CommentsPk))
      return false;
    CommentsPk castOther = (CommentsPk) other;

    return ((this.getBook() == castOther.getBook()) || (this.getBook() != null && castOther.getBook() != null
 && this.getBook().equals(castOther.getBook())))
 && ((this.getUser() == castOther.getUser()) || (this.getUser() != null && castOther.getUser() != null
     && this.getUser().equals(castOther.getUser())))
 && ((this.getCommentsDate() == castOther.getCommentsDate())
     || (this.getCommentsDate() != null && castOther.getCommentsDate() != null
  && this.getCommentsDate().equals(castOther.getCommentsDate())));
  }

  public int hashCode() {
    int result = 17;

    result = 37 * result + (getBook() == null ? 0 : this.getBook().hashCode());
    result = 37 * result + (getUser() == null ? 0 : this.getUser().hashCode());
    result = 37 * result + (getCommentsDate() == null ? 0 : this.getCommentsDate().hashCode());
    return result;
  }

}

商品评论表模型

package com.book.model;
// Generated 2015-10-30 14:56:21 by Hibernate Tools 4.0.0.Final

import java.sql.Date;


public class Comments implements java.io.Serializable {

  private String content;
  private String pic;
  private Integer client;
  private CommentsPk id;
  public Comments()
  {
    
  }
  public String getContent() {
    return content;
  }
  public void setContent(String content) {
    this.content = content;
  }
  public String getPic() {
    return pic;
  }
  public void setPic(String pic) {
    this.pic = pic;
  }
  public Integer getClient() {
    return client;
  }
  public void setClient(Integer client) {
    this.client = client;
  }
  public CommentsPk getId() {
    return id;
  }
  public void setId(CommentsPk id) {
    this.id = id;
  }
  public Comments(String content, String pic, Integer client, CommentsPk id) {
    super();
    this.content = content;
    this.pic = pic;
    this.client = client;
    this.id = id;
  }
  
}

相应的Hibernate 映射文件:





  
    
      
 
      
      
 
      
      
 
      
    
    
      
    
    
      
    
    
      
    
  

因为商品评论表有两个是外键所以使用了key-many-to-one标签。

由于采用spring3.2+hibernate4.1所以得到sessionFactory的方式只限于sessionFactory.getCurrentSession();但是必须开启事物:   

 
    
  
  
  
  
    
      
      
      
      

      
    
  

以上都是我配置的时候出现问题的地方。下面是spring配置文件:



    
    
  
  
   
    
    
    
    
  
 
  
  
    
    

  classpath:config

    
    
      
 org.hibernate.dialect.MySQLDialect
 true
 update
 false
 thread
      
    
  
  

    
  
    
  
  
  
  
    
      
      
      
      

      
    
  

  
  
   
    
    
  
    
    


一切就绪之后我们使用servlet测试:


  test
  com.book.test.Test
 
 
  test
  /index
 
   
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    BeanFactory factor = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());
    OrderDao dao= factor.getBean(OrderDao.class);
    
    Object[] list= dao.get(1).getOrderitems().toArray();
    System.out.println(((Orderitem)list[0]).getOrdercount());    
  }

因为没有使用structs我们需要自己查找spring的BeanFactory来获得dao bean 这也是需要注意的地方,纠结好久。

运行结果:

 成功加载订单表订单1项目订购数量。

 毕竟第一次使用java开发这类项目,慢慢学习吧,希望大家可以喜欢JavaWeb搭建的网上图书商城框架,对大家的学习有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/151059.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号