JavaWeb搭建網(wǎng)上圖書商城畢業(yè)設(shè)計
以前一直接觸.net相關(guān)的web開發(fā),現(xiàn)在猛然使用javaWeb還是很不習(xí)慣,就連搭個框架也是第一次。
一、談?wù)勴?xiàng)目架構(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)用引擎庫。之所以選擇它一方面是因?yàn)閷W(xué)習(xí)成本低,另一方面是它的跨平臺調(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è)計
一個小網(wǎng)上書城,所以設(shè)計的還有欠缺,以實(shí)用為主,主要是練手java開發(fā)~~。所以使用了navicat簡單設(shè)計了一下,不過沒有設(shè)計表關(guān)聯(lián),取而代之的是后來一個一個添加關(guān)系的,發(fā)現(xiàn)這個設(shè)計工具有點(diǎn)問題,圖示:

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

稍作修改就是這樣--

三、spring3+hibernate4配置
因?yàn)閙odel和映射文件是自動生成所以稍加配置就好,需要注意的是復(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>
因?yàn)樯唐吩u論表有兩個是外鍵所以使用了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ù)邏輯層實(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)交易平臺項(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)文章
idea2023遠(yuǎn)程調(diào)試springboot的過程詳解
這篇文章主要介紹了idea2023遠(yuǎn)程調(diào)試,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-08-08
String.intern()作用與常量池關(guān)系示例解析
這篇文章主要為大家介紹了String.intern()作用與常量池關(guān)系示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
詳解Spring Cloud 跨服務(wù)數(shù)據(jù)聚合框架
這篇文章主要介紹了詳解Spring Cloud 跨服務(wù)數(shù)據(jù)聚合框架,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03
Java中null的意義及其使用時的注意事項(xiàng)說明
這篇文章主要介紹了Java中null的意義及其使用時的注意事項(xiàng)說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09
java用list集合存儲學(xué)生信息并算出成績平均值操作
這篇文章主要介紹了java用list集合存儲學(xué)生信息并算出成績平均值操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08

