JavaWeb搭建網(wǎng)上圖書商城畢業(yè)設(shè)計(jì)
以前一直接觸.net相關(guān)的web開發(fā),現(xiàn)在猛然使用javaWeb還是很不習(xí)慣,就連搭個(gè)框架也是第一次。
一、談?wù)勴?xiàng)目架構(gòu)
一開始接觸.net相關(guān)的開發(fā)所以對于.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端就都有了??蛻舳私y(tǒng)一通過分布式框架調(diào)用服務(wù)。找了很久最終選擇了Hprose,這一輕量級(jí)、跨語言、跨平臺(tái)、無侵入式、高性能動(dòng)態(tài)遠(yuǎn)程對象調(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ā)速度比較快也比較簡單,所以準(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簡單設(shè)計(jì)了一下,不過沒有設(shè)計(jì)表關(guān)聯(lián),取而代之的是后來一個(gè)一個(gè)添加關(guān)系的,發(fā)現(xiàn)這個(gè)設(shè)計(jì)工具有點(diǎn)問題,圖示:
其實(shí)表關(guān)聯(lián)一看就能看出來~~,接下來就是hibernate一些映射了,同樣也是使用插件生成model和映射文件
稍作修改就是這樣--
三、spring3+hibernate4配置
因?yàn)閙odel和映射文件是自動(dòng)生成所以稍加配置就好,需要注意的是復(fù)合主鍵的設(shè)置,自動(dòng)生成的會(huì)把復(fù)合主鍵對應(yīng)一個(gè)復(fù)合模型。如商品評(píng)論表的復(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; } }
商品評(píng)論表模型:
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>
因?yàn)樯唐吩u(píng)論表有兩個(gè)是外鍵所以使用了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>
以上都是我配置的時(shí)候出現(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> <!-- 可追加配置二級(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> <!-- 只對業(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測試:
<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()); }
因?yàn)闆]有使用structs我們需要自己查找spring的BeanFactory來獲得dao bean 這也是需要注意的地方,糾結(jié)好久。
運(yùn)行結(jié)果:
成功加載訂單表訂單1項(xiàng)目訂購數(shù)量。
畢竟第一次使用java開發(fā)這類項(xiàng)目,慢慢學(xué)習(xí)吧,希望大家可以喜歡JavaWeb搭建的網(wǎng)上圖書商城框架,對大家的學(xué)習(xí)有所幫助。
- Java 實(shí)戰(zhàn)項(xiàng)目錘煉之網(wǎng)上商城系統(tǒng)的實(shí)現(xiàn)流程
- Java 實(shí)戰(zhàn)項(xiàng)目之家居購物商城系統(tǒng)詳解流程
- Java 實(shí)戰(zhàn)交易平臺(tái)項(xiàng)目之寵物在線商城系統(tǒng)
- SSH框架網(wǎng)上商城項(xiàng)目第25戰(zhàn)之使用java email給用戶發(fā)送郵件
- JAVAWEB實(shí)現(xiàn)簡單的商城項(xiàng)目(一)實(shí)例代碼解析
- Java 實(shí)戰(zhàn)項(xiàng)目錘煉之仿天貓網(wǎng)上商城的實(shí)現(xiàn)流程
相關(guān)文章
Java注解實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)源切換的實(shí)例代碼
本篇文章主要介紹了Java注解實(shí)現(xiàn)動(dòng)態(tài)數(shù)據(jù)源切換的實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06play for scala 實(shí)現(xiàn)SessionFilter 過濾未登錄用戶跳轉(zhuǎn)到登錄頁面
這篇文章主要介紹了play for scala 實(shí)現(xiàn)SessionFilter 過濾未登錄用戶跳轉(zhuǎn)到登錄頁面的相關(guān)資料,需要的朋友可以參考下2016-11-11spring中@ComponentScan自動(dòng)掃描并指定掃描規(guī)則
本文主要介紹了spring中@ComponentScan自動(dòng)掃描并指定掃描規(guī)則,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04Spring中的InitializingBean接口源碼解析
這篇文章主要介紹了Spring中的InitializingBean接口源碼解析,InitializingBean接口為Bean初始化提供了一種方式,實(shí)現(xiàn)InitializingBean接口的Bean,在BeanFactory設(shè)置其所有屬性后會(huì)調(diào)用其afterPropertiesSet()方法,需要的朋友可以參考下2024-02-02Java反射學(xué)習(xí) getClass()函數(shù)應(yīng)用
所謂反射,可以理解為在運(yùn)行時(shí)期獲取對象類型信息的操作,本文將詳細(xì)介紹,需要的朋友可以參考下2012-12-12Java數(shù)據(jù)結(jié)構(gòu)之順序表篇
順序表,全名順序存儲(chǔ)結(jié)構(gòu),是線性表的一種。線性表用于存儲(chǔ)邏輯關(guān)系為“一對一”的數(shù)據(jù),順序表自然也不例外,不僅如此,順序表對數(shù)據(jù)物理存儲(chǔ)結(jié)構(gòu)也有要求。順序表存儲(chǔ)數(shù)據(jù)時(shí),會(huì)提前申請一整塊足夠大小的物理空間,然后將數(shù)據(jù)依次存儲(chǔ)起來,存儲(chǔ)時(shí)數(shù)據(jù)元素間不留縫隙2022-01-01Spring中的BeanFactory與FactoryBean區(qū)別詳解
這篇文章主要介紹了Spring中的BeanFactory與FactoryBean區(qū)別詳解,BeanFactory是一個(gè)接口,它是spring中的一個(gè)工廠,FactoryBean也是一個(gè)接口,實(shí)現(xiàn)了3個(gè)方法,通過重寫其中方法自定義生成bean,需要的朋友可以參考下2024-01-01SpringBoot中的@RestControllerAdvice注解詳解
這篇文章主要介紹了SpringBoot中的@RestControllerAdvice注解詳解,RestControllerAdvice注解用于創(chuàng)建全局異常處理類,用于捕獲和處理整個(gè)應(yīng)用程序中的異常,需要的朋友可以參考下2024-01-01Java通過經(jīng)緯度坐標(biāo)獲取兩個(gè)點(diǎn)之間的直線距離的示例
這篇文章主要介紹了Java通過經(jīng)緯度坐標(biāo)獲取兩個(gè)點(diǎn)之間的直線距離的示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07SpringBoot整合Dubbo+Zookeeper實(shí)現(xiàn)RPC調(diào)用
這篇文章主要給大家介紹了Spring Boot整合Dubbo+Zookeeper實(shí)現(xiàn)RPC調(diào)用的步驟詳解,文中有詳細(xì)的代碼示例,對我們的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-07-07