java web 博客园_JavaWeb之网上图书商城-框架搭建

一、写在前面

以前一直接触.net相关的web开发,现在猛然使用javaWeb还是很不习惯。就连搭个框架也是第一次,在互联网众多的资料中看了许多,我想是应该本人的需求有点变态,所以花了将近半天时间的才明白是个什么回事。。。

二、谈谈项目架构

因为高中开始接触.net相关的开发所以对于.net相关的开发还是比较 熟悉的,但我在学校学的java方向的开发,而我打算把这两种平台结合起来,使用java做后台也就是服务提供者,将所有业务逻辑在java平台完成并使用我比较熟悉的.net做Web端的开发。这样一来安卓app,web端就都有了。客户端统一通过分布式框架调用服务。找了很久最终选择了 Hprose(网址:http://www.hprose.com/)这一轻量级、跨语言、跨平台、无侵入式、高性能动态远程对象调用引擎库。之所以选择它一方面是因为学习成本低,另一方面是他的跨平台调用非常轻松高效,因为我们要使用.net做web需要调用java发布的服务!大概看了一下Hprose的文档,发现使用内置的HproseServlet发布服务开发速度比较快也比较简单,所以准备使用这种方式发布服务。可问题来了,传统的ssh架构感觉有点重了,准备使用.net开发web端所以感觉没有必要整合Struts,于是就是hibernate+spring+hprose这种架构。

三、数据库设计

一个小网上书城,所以设计的还有欠缺,以实用为主,主要是练手java开发~~。所以使用了navicat简单设计了一下,不过没有设计表关联,取而代之的是后来一个一个添加关系的,发现这个设计工具有点问题,图示:

pBWnP9KK0x9pxemPtOL0R1px+iOtuG3gskoAAAAAJktxDAAAAIDJUhwDAAAAYLL+L02yXrplN8PjAAAAAElFTkSuQmCC

其实表关联一看就能看出来~~,接下来就是hibernate一些映射了,同样也是使用插件生成model和映射文件

XQ7yM7fB7I4538NAACAceZ8C+63UpW3x8O7zcpW8QUtgev3ObuHr0WZw9GvjFH0H5YekViBIQrTAAAAAElFTkSuQmCC

稍作修改就是这样--

DyPQgnqAPTNbAAAAAElFTkSuQmCC

三、spring3+hibernate4配置

因为model和映射文件是自动生成所以稍加配置就好,需要注意的是复合主键的设置,自动生成的会把复合主键对应一个复合模型。如商品评论表的复合主键类型:

packagecom.book.model;//Generated 2015-11-2 9:07:06 by Hibernate Tools 4.0.0.Final

importjava.util.Date;/*** CommentsId generated by hbm2java*/

public class CommentsPk implementsjava.io.Serializable {privateBook book;privateUser user;privateDate commentsDate;publicCommentsPk() {

}publicCommentsPk(Book book, User user, Date commentsDate) {this.book =book;this.user =user;this.commentsDate =commentsDate;

}publicBook getBook() {return this.book;

}public voidsetBook(Book book) {this.book =book;

}publicUser getUser() {return this.user;

}public voidsetUser(User user) {this.user =user;

}publicDate getCommentsDate() {return this.commentsDate;

}public voidsetCommentsDate(Date commentsDate) {this.commentsDate =commentsDate;

}public booleanequals(Object other) {if ((this ==other))return true;if ((other == null))return false;if (!(other instanceofCommentsPk))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 inthashCode() {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());returnresult;

}

}

商品评论表模型:

packagecom.book.model;//Generated 2015-10-30 14:56:21 by Hibernate Tools 4.0.0.Final

importjava.sql.Date;/*** Comments generated by hbm2java*/

public class Comments implementsjava.io.Serializable {privateString content;privateString pic;privateInteger client;privateCommentsPk id;publicComments()

{

}publicString getContent() {returncontent;

}public voidsetContent(String content) {this.content =content;

}publicString getPic() {returnpic;

}public voidsetPic(String pic) {this.pic =pic;

}publicInteger getClient() {returnclient;

}public voidsetClient(Integer client) {this.client =client;

}publicCommentsPk getId() {returnid;

}public voidsetId(CommentsPk id) {this.id =id;

}publicComments(String content, String pic, Integer client, CommentsPk id) {super();this.content =content;this.pic =pic;this.client =client;this.id =id;

}

}

相应的Hibernate 映射文件:

/p>

"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

因为商品评论表有两个是外键所以使用了key-many-to-one标签。

由于采用spring3.2+hibernate4.1所以得到sessionFactory的方式只限于sessionFactory.getCurrentSession();但是必须开启事物:

以上都是我配置的时候出现问题的地方。

一下是spring配置文件:

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">

classpath:config

org.hibernate.dialect.MySQLDialect

true

update

false

thread

一切就绪之后我们使用servlet测试:

test

com.book.test.Test

test

/index

protected void doGet(HttpServletRequest request, HttpServletResponse response) throwsServletException, 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 这也是需要注意的地方,纠结好久。

运行结果:

AaCgdLVdtdUAAAAAAElFTkSuQmCC

成功加载订单表订单1项目订购数量。

毕竟第一次使用java开发这类项目,慢慢学习吧~~

这个网上书城系统使用Eclipse开发的,代码完整,jar包齐全,sql脚本包含在里面,将下载下来的项目导入到Eclipse中即可运行,本项目做了很多校验,对可能出现的bug做了考虑,属于比较完善的系统。本系统包含九个模块,前台模块分为:用户模快,分类模块,图书模块,购物车模块,订单模块;后台模块分为:管理员模块,分类管理模快,图书管理模快,订单管理模块。书城界面简洁,易于操作,简单易懂,代码均有注释,各模块功能完善。各大模块的功能描述:前台: 1). 用户模块功能有: * 用户注册: > 表单页面是jQuery做校验(包含了ajax异步请求) # 在输入框失去焦点时进行校验; # 在提交时对所有输入框进行校验; # 在输入框得到焦点时,隐藏错误信息。 > 表单页面使用一次性图形验证码; > 在servlet中再次做了表单校验。 * 用户登录: > 表单校验与注册功能相同; > 登录成功时会把当前用户名保存到cookie中,为了在登录页面的输入框中显示! * 用户退出:销毁session 2). 分类模块 * 查询所有分类: > 有1级和2级分类 > 在页面中使用手风琴式菜单(Javascript组件)显示分类。 3). 图书模块: * 按分类查询 * 按作者查询 * 按出版社查询 * 按书名模糊查询 * 多条件组合查询 * 按id查询 除按id查询外,其他都是分页查询。 技术难点: > 组合查询:根据多个条件拼凑sql语句。 > 带条件分页查询:条件可能会丢失。使用自定义的PageBean来传递分页数据! > 页面上的分页导航:页码列表的显示不好计算! 4). 购物车模块: * 添加条目 * 修改条目数量 * 删除条目 * 批量删除条目 * 我的购物车 * 查询被勾选条目 购物车没有使用sesson或cookie,而是存储到数据库中。 技术难点: > 添加条目时,如果两次添加针对同一本书的条目,不是添加,而是合并; > 修改数量时使用ajax时请求服务器端,服务器端返回json。 > 大量js代码 5). 订单模块: * 生成订单 * 我的订单 * 查看订单详细 * 订单支付 * 订单确认收货 * 取消订单 后台 1). 管理员 * 管理员登录 2). 分类管理 * 添加1级分类 * 添加2级分类: 需要为2级分类指定所属1级分类 * 编辑1级分类 * 编辑2级分类: 可以修改所属1级分类 * 删除1级分类: 存在子分类时,不能删除 * 删除2级分类: 当前2级分类下存在图书时不能删除 * 查看所有分类 3). 图书管理 * 各种查询:与前台相同 * 添加图书: > 上传图片 > 页面中使用动态下拉列表显示2级分类,当指定1级分类后,2级分类下拉列表中动态显示该1级分类下所有2级分类名称 * 修改图书: 与添加图书相似,也使用动态下拉列表 * 删除图书: 需要删除图书对应图片,再删除图书 4). 订单管理 * 各种查询 * 订单发货 * 订单取消
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值