JavaWeb搭建網(wǎng)上圖書商城畢業(yè)設(shè)計
以前一直接觸.net相關(guān)的web開發(fā),現(xiàn)在猛然使用javaWeb還是很不習(xí)慣,就連搭個框架也是第一次。
一、談?wù)勴椖考軜?gòu)
一開始接觸.net相關(guān)的開發(fā)所以對于.net相關(guān)的開發(fā)還是比較熟悉的,但我在學(xué)校學(xué)的java方向的開發(fā),而我打算把這兩種平臺結(jié)合起來,使用java做后臺也就是服務(wù)提供者,將所有業(yè)務(wù)邏輯在java平臺完成并使用我比較熟悉的.net做Web端的開發(fā)。這樣一來安卓app,web端就都有了??蛻舳私y(tǒng)一通過分布式框架調(diào)用服務(wù)。找了很久最終選擇了Hprose,這一輕量級、跨語言、跨平臺、無侵入式、高性能動態(tài)遠(yuǎn)程對象調(diào)用引擎庫。之所以選擇它一方面是因為學(xué)習(xí)成本低,另一方面是它的跨平臺調(diào)用非常輕松高效,因為我們要使用.net做web需要調(diào)用java發(fā)布的服務(wù)!大概看了一下Hprose的文檔,發(fā)現(xiàn)使用內(nèi)置的HproseServlet發(fā)布服務(wù)開發(fā)速度比較快也比較簡單,所以準(zhǔn)備使用這種方式發(fā)布服務(wù)??蓡栴}來了,傳統(tǒng)的ssh架構(gòu)感覺有點重了,準(zhǔn)備使用.net開發(fā)web端所以感覺沒有必要整合Struts,于是就是hibernate+spring+hprose這種架構(gòu)。
二、數(shù)據(jù)庫設(shè)計
一個小網(wǎng)上書城,所以設(shè)計的還有欠缺,以實用為主,主要是練手java開發(fā)~~。所以使用了navicat簡單設(shè)計了一下,不過沒有設(shè)計表關(guān)聯(lián),取而代之的是后來一個一個添加關(guān)系的,發(fā)現(xiàn)這個設(shè)計工具有點問題,圖示:

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

稍作修改就是這樣--

三、spring3+hibernate4配置
因為model和映射文件是自動生成所以稍加配置就好,需要注意的是復(fù)合主鍵的設(shè)置,自動生成的會把復(fù)合主鍵對應(yīng)一個復(fù)合模型。如商品評論表的復(fù)合主鍵類型:
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;
}
}
商品評論表模型:
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 映射文件:
<?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>
因為商品評論表有兩個是外鍵所以使用了key-many-to-one標(biāo)簽。
由于采用spring3.2+hibernate4.1所以得到sessionFactory的方式只限于sessionFactory.getCurrentSession();但是必須開啟事物:
<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>
以上都是我配置的時候出現(xiàn)問題的地方。下面是spring配置文件:
<?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>
<!-- 可追加配置二級緩存 -->
<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>
<!-- 只對業(yè)務(wù)邏輯層實施事務(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測試:
<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>
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 這也是需要注意的地方,糾結(jié)好久。
運行結(jié)果:

成功加載訂單表訂單1項目訂購數(shù)量。
畢竟第一次使用java開發(fā)這類項目,慢慢學(xué)習(xí)吧,希望大家可以喜歡JavaWeb搭建的網(wǎng)上圖書商城框架,對大家的學(xué)習(xí)有所幫助。
相關(guān)文章
Java注解實現(xiàn)動態(tài)數(shù)據(jù)源切換的實例代碼
本篇文章主要介紹了Java注解實現(xiàn)動態(tài)數(shù)據(jù)源切換的實例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06
play for scala 實現(xiàn)SessionFilter 過濾未登錄用戶跳轉(zhuǎn)到登錄頁面
這篇文章主要介紹了play for scala 實現(xiàn)SessionFilter 過濾未登錄用戶跳轉(zhuǎn)到登錄頁面的相關(guān)資料,需要的朋友可以參考下2016-11-11
spring中@ComponentScan自動掃描并指定掃描規(guī)則
本文主要介紹了spring中@ComponentScan自動掃描并指定掃描規(guī)則,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
Spring中的InitializingBean接口源碼解析
這篇文章主要介紹了Spring中的InitializingBean接口源碼解析,InitializingBean接口為Bean初始化提供了一種方式,實現(xiàn)InitializingBean接口的Bean,在BeanFactory設(shè)置其所有屬性后會調(diào)用其afterPropertiesSet()方法,需要的朋友可以參考下2024-02-02
Java反射學(xué)習(xí) getClass()函數(shù)應(yīng)用
所謂反射,可以理解為在運行時期獲取對象類型信息的操作,本文將詳細(xì)介紹,需要的朋友可以參考下2012-12-12
Java數(shù)據(jù)結(jié)構(gòu)之順序表篇
順序表,全名順序存儲結(jié)構(gòu),是線性表的一種。線性表用于存儲邏輯關(guān)系為“一對一”的數(shù)據(jù),順序表自然也不例外,不僅如此,順序表對數(shù)據(jù)物理存儲結(jié)構(gòu)也有要求。順序表存儲數(shù)據(jù)時,會提前申請一整塊足夠大小的物理空間,然后將數(shù)據(jù)依次存儲起來,存儲時數(shù)據(jù)元素間不留縫隙2022-01-01
Spring中的BeanFactory與FactoryBean區(qū)別詳解
這篇文章主要介紹了Spring中的BeanFactory與FactoryBean區(qū)別詳解,BeanFactory是一個接口,它是spring中的一個工廠,FactoryBean也是一個接口,實現(xiàn)了3個方法,通過重寫其中方法自定義生成bean,需要的朋友可以參考下2024-01-01
SpringBoot中的@RestControllerAdvice注解詳解
這篇文章主要介紹了SpringBoot中的@RestControllerAdvice注解詳解,RestControllerAdvice注解用于創(chuàng)建全局異常處理類,用于捕獲和處理整個應(yīng)用程序中的異常,需要的朋友可以參考下2024-01-01
Java通過經(jīng)緯度坐標(biāo)獲取兩個點之間的直線距離的示例
這篇文章主要介紹了Java通過經(jīng)緯度坐標(biāo)獲取兩個點之間的直線距離的示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
SpringBoot整合Dubbo+Zookeeper實現(xiàn)RPC調(diào)用
這篇文章主要給大家介紹了Spring Boot整合Dubbo+Zookeeper實現(xiàn)RPC調(diào)用的步驟詳解,文中有詳細(xì)的代碼示例,對我們的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-07-07

