一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務(wù)器之家 - 編程語言 - JAVA教程 - JavaWeb搭建網(wǎng)上圖書商城畢業(yè)設(shè)計(jì)

JavaWeb搭建網(wǎng)上圖書商城畢業(yè)設(shè)計(jì)

2020-01-13 17:37lijiao JAVA教程

這篇文章主要介紹了JavaWeb搭建網(wǎng)上圖書商城框架,特別適合正在為網(wǎng)上商城畢業(yè)設(shè)計(jì)煩惱的同學(xué),需要的朋友可以參考下

  以前一直接觸.net相關(guān)的web開發(fā),現(xiàn)在猛然使用javaWeb還是很不習(xí)慣,就連搭個(gè)框架也是第一次。

一、談?wù)勴?xiàng)目架構(gòu)
  一開始接觸.net相關(guān)的開發(fā)所以對(duì)于.net相關(guān)的開發(fā)還是比較熟悉的,但我在學(xué)校學(xué)的java方向的開發(fā),而我打算把這兩種平臺(tái)結(jié)合起來,使用java做后臺(tái)也就是服務(wù)提供者,將所有業(yè)務(wù)邏輯在java平臺(tái)完成并使用我比較熟悉的.net做Web端的開發(fā)。這樣一來安卓app,web端就都有了。客戶端統(tǒng)一通過分布式框架調(diào)用服務(wù)。找了很久最終選擇了Hprose,這一輕量級(jí)、跨語言、跨平臺(tái)、無侵入式、高性能動(dòng)態(tài)遠(yuǎn)程對(duì)象調(diào)用引擎庫。之所以選擇它一方面是因?yàn)閷W(xué)習(xí)成本低,另一方面是它的跨平臺(tái)調(diào)用非常輕松高效,因?yàn)槲覀円褂?net做web需要調(diào)用java發(fā)布的服務(wù)!大概看了一下Hprose的文檔,發(fā)現(xiàn)使用內(nèi)置的HproseServlet發(fā)布服務(wù)開發(fā)速度比較快也比較簡(jiǎn)單,所以準(zhǔn)備使用這種方式發(fā)布服務(wù)。可問題來了,傳統(tǒng)的ssh架構(gòu)感覺有點(diǎn)重了,準(zhǔn)備使用.net開發(fā)web端所以感覺沒有必要整合Struts,于是就是hibernate+spring+hprose這種架構(gòu)。

二、數(shù)據(jù)庫設(shè)計(jì)

  一個(gè)小網(wǎng)上書城,所以設(shè)計(jì)的還有欠缺,以實(shí)用為主,主要是練手java開發(fā)~~。所以使用了navicat簡(jiǎn)單設(shè)計(jì)了一下,不過沒有設(shè)計(jì)表關(guān)聯(lián),取而代之的是后來一個(gè)一個(gè)添加關(guān)系的,發(fā)現(xiàn)這個(gè)設(shè)計(jì)工具有點(diǎn)問題,圖示:

JavaWeb搭建網(wǎng)上圖書商城畢業(yè)設(shè)計(jì)

其實(shí)表關(guān)聯(lián)一看就能看出來~~,接下來就是hibernate一些映射了,同樣也是使用插件生成model和映射文件

JavaWeb搭建網(wǎng)上圖書商城畢業(yè)設(shè)計(jì)

稍作修改就是這樣--

JavaWeb搭建網(wǎng)上圖書商城畢業(yè)設(shè)計(jì)

三、spring3+hibernate4配置
  因?yàn)閙odel和映射文件是自動(dòng)生成所以稍加配置就好,需要注意的是復(fù)合主鍵的設(shè)置,自動(dòng)生成的會(huì)把復(fù)合主鍵對(duì)應(yīng)一個(gè)復(fù)合模型。如商品評(píng)論表的復(fù)合主鍵類型

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.book.model;
// Generated 2015-11-2 9:07:06 by Hibernate Tools 4.0.0.Final
 
import java.util.Date;
 
/**
 * CommentsId generated by hbm2java
 */
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;
  }
 
}

商品評(píng)論表模型

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.book.model;
// Generated 2015-10-30 14:56:21 by Hibernate Tools 4.0.0.Final
 
import java.sql.Date;
 
/**
 * Comments generated by hbm2java
 */
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;
  }
  
}

相應(yīng)的Hibernate 映射文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated 2015-10-30 14:56:21 by Hibernate Tools 4.0.0.Final -->
<hibernate-mapping>
  <class name="com.book.model.Comments" table="comments" catalog="bookstore">
    <composite-id name="id" class="com.book.model.CommentsPk">
      <key-many-to-one name="book" class="com.book.model.Book">
        <column name="BookID" />
      </key-many-to-one>
      <key-many-to-one name="user" class="com.book.model.User">
        <column name="UserID" />
      </key-many-to-one>
      <key-property name="commentsDate" type="timestamp">
        <column name="CommentsDate" length="19" />
      </key-property>
    </composite-id>
    <property name="content" type="string">
      <column name="Content" length="65535" />
    </property>
    <property name="pic" type="string">
      <column name="Pic" length="65535" />
    </property>
    <property name="client" type="java.lang.Integer">
      <column name="Client" />
    </property>
  </class>
</hibernate-mapping>

因?yàn)樯唐吩u(píng)論表有兩個(gè)是外鍵所以使用了key-many-to-one標(biāo)簽。

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<bean id="transactionManager"
   class="org.springframework.orm.hibernate4.HibernateTransactionManager">
   <property name="sessionFactory" ref="sessionFactory" />
 </bean>
 
 <!-- 事務(wù)的傳播特性 -->
 <tx:advice id="txadvice" transaction-manager="transactionManager">
   <tx:attributes>
     <tx:method name="add*" propagation="REQUIRED" />
     <tx:method name="delete*" propagation="REQUIRED" />
     <tx:method name="modify*" propagation="REQUIRED" />
     
      <!--hibernate4必須配置為開啟事務(wù) 否則 getCurrentSession()獲取不到-->
     <tx:method name="*" propagation="REQUIRED" read-only="true" />
   </tx:attributes>
 </tx:advice>

以上都是我配置的時(shí)候出現(xiàn)問題的地方。下面是spring配置文件:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
    
    <!-- 啟用spring注解支持 -->
  <context:annotation-config />
  
  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url"
      value="jdbc:mysql://127.0.0.1/bookstore?useUnicode=true&characterEncoding=UTF-8" />
    <property name="username" value="root" />
    <property name="password" value="yangyang" />
  </bean>
 
  <!-- 可追加配置二級(jí)緩存 -->
  <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" >
    <property name="dataSource" ref="dataSource"/>
    <property name="mappingDirectoryLocations">
       <list>
         <value>classpath:config</value>
       </list>
    </property>
    <property name="hibernateProperties">
      <props>
        <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
        <prop key="hibernate.show_sql">true</prop>
        <prop key="hibernate.hbm2ddl.auto">update</prop>
        <prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>
        <prop key="current_session_context_class">thread</prop>
      </props>
    </property>
  </bean>
  
 
    <!-- 配置事務(wù)管理器 -->
  <bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
  </bean>
  
  <!-- 事務(wù)的傳播特性 -->
  <tx:advice id="txadvice" transaction-manager="transactionManager">
    <tx:attributes>
      <tx:method name="add*" propagation="REQUIRED" />
      <tx:method name="delete*" propagation="REQUIRED" />
      <tx:method name="modify*" propagation="REQUIRED" />
      
       <!--hibernate4必須配置為開啟事務(wù) 否則 getCurrentSession()獲取不到-->
      <tx:method name="*" propagation="REQUIRED" read-only="true" />
    </tx:attributes>
  </tx:advice>
 
  <!-- 那些類那些方法使用事務(wù) -->
  <aop:config>
   <!-- 只對(duì)業(yè)務(wù)邏輯層實(shí)施事務(wù) -->
    <aop:pointcut id="allManagerMethod"
      expression="execution(* com.book.test.*.*(..))" />
    <aop:advisor pointcut-ref="allManagerMethod" advice-ref="txadvice" />
  </aop:config>
    <bean name="basedao" class="com.book.dao.impl.AdressDao" />
    <bean name="orderdao" class="com.book.dao.impl.OrderDao" />
</beans>

一切就緒之后我們使用servlet測(cè)試:

?
1
2
3
4
5
6
7
8
<servlet>
  <servlet-name>test</servlet-name>
  <servlet-class>com.book.test.Test</servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>test</servlet-name>
  <url-pattern>/index</url-pattern>
 </servlet-mapping>
   
?
1
2
3
4
5
6
7
8
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());   
 }

因?yàn)闆]有使用structs我們需要自己查找spring的BeanFactory來獲得dao bean 這也是需要注意的地方,糾結(jié)好久。

運(yùn)行結(jié)果:

JavaWeb搭建網(wǎng)上圖書商城畢業(yè)設(shè)計(jì)

 成功加載訂單表訂單1項(xiàng)目訂購數(shù)量。

 畢竟第一次使用java開發(fā)這類項(xiàng)目,慢慢學(xué)習(xí)吧,希望大家可以喜歡JavaWeb搭建的網(wǎng)上圖書商城框架,對(duì)大家的學(xué)習(xí)有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 无遮免费网站在线入口 | 男男gaygays黑人 | 久久青草免费91线频观看站街 | 青青网在线视频 | 操老妇| 日本美女动态图片 | 亚洲阿v天堂在线2017 | 精品综合 | 国产男女乱淫真视频全程播放 | 日韩亚洲国产激情在线观看 | 国产一级精品高清一级毛片 | 日本老妇乱子伦中文视频 | 变态 另类 人妖小说 | 国产亚洲一欧美一区二区三区 | 美女扒开肌肌让男人桶 | 亚洲国产精品婷婷久久久久 | 十大网站免费货源 | 2022国产麻豆剧果冻传媒入口 | 白丝h视频 | 亚瑟天堂久久一区二区影院 | 5151hh四虎国产精品 | 美女被狂揉下部羞羞动漫 | 国产三及| 亚洲香蕉伊在人在线观看9 亚洲系列国产系列 | 91制片厂果冻传媒杨柳作品 | 男同gay作爰视频网站 | 国产私拍精品88福利视频 | 国产亚洲精品第一综合另类 | 亚洲看片lutube在线入口 | 色综合色狠狠天天久久婷婷基地 | 亚洲一区二区福利视频 | 亚洲国产黄色 | 色噜噜狠狠色综合 | 99r视频在线观看 | 日韩精品视频美在线精品视频 | 日本肉体xxxx | 日韩精品特黄毛片免费看 | 欧美人做人爱a全程免费 | 女王调奴丨vk | 成人精品福利 | 999精品视频这里只有精品 |