以前一直接觸.net相關的web開發,現在猛然使用javaWeb還是很不習慣,就連搭個框架也是第一次。
一、談談項目架構<br /> 一開始接觸.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配置<br /> 因為model和映射文件是自動生成所以稍加配置就好,需要注意的是複合主鍵的設置,自動生成的會把複合主鍵對應一個複合模型。如商品評論表的複合主鍵類型:
package com.book.model;// Generated 2015-11-2 9:07:06 by Hibernate Tools 4.0.0.Finalimport 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.Finalimport 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; } }相應的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"> <key-many-to-one name="book"> <column name="BookID" /> </key-many-to-one > <key-many-to-one name="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標籤。
由於採用spring3.2+hibernate4.1所以得到sessionFactory的方式只限於sessionFactory.getCurrentSession();但是必須開啟事物:
<bean id="transactionManager" > <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 事務的傳播特性--> <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必須配置為開啟事務否則getCurrentSession()獲取不到--> <tx:method name="*" propagation="REQUIRED" read-only="true " /> </tx:attributes> </tx:advice>
以上都是我配置的時候出現問題的地方。下面是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" 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" > <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> <!-- 配置事務管理器--> <bean id="transactionManager" > <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 事務的傳播特性--> <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必須配置為開啟事務否則getCurrentSession()獲取不到--> <tx:method name="*" propagation="REQUIRED" read-only="true" /> </tx:attributes> </tx:advice> <!-- 那些類那些方法使用事務--> <aop:config> <!-- 只對業務邏輯層實施事務--> <aop:pointcut id="allManagerMethod" expression="execution(* com.book.test.*.*(..))" /> <aop:advisor pointcut-ref="allManagerMethod" advice-ref= "txadvice" /> </aop:config> <bean name="basedao" /> <bean name="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 這也是需要注意的地方,糾結好久。
運行結果:
成功加載訂單表訂單1項目訂購數量。
畢竟第一次使用java開發這類項目,慢慢學習吧,希望大家可以喜歡JavaWeb搭建的網上圖書商城框架,對大家的學習有所幫助。