JavaWeb搭建网上图书商城毕业设计
以前一直接触.net相关的web开发,现在猛然使用javaweb还是很不习惯,就连搭个框架也是第一次。
一、谈谈项目架构
一开始接触.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配置
因为model和映射文件是自动生成所以稍加配置就好,需要注意的是复合主键的设置,自动生成的会把复合主键对应一个复合模型。如商品评论表的复合主键类型:
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; } }
相应的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标签。
由于采用spring3.2+hibernate4.1所以得到sessionfactory的方式只限于sessionfactory.getcurrentsession();但是必须开启事物:
<bean id="transactionmanager" class="org.springframework.orm.hibernate4.hibernatetransactionmanager"> <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" 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> <!-- 配置事务管理器 --> <bean id="transactionmanager" class="org.springframework.orm.hibernate4.hibernatetransactionmanager"> <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" 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 这也是需要注意的地方,纠结好久。
运行结果:
成功加载订单表订单1项目订购数量。
毕竟第一次使用java开发这类项目,慢慢学习吧,希望大家可以喜欢javaweb搭建的网上图书商城框架,对大家的学习有所帮助。