实验7 实现简单的图书在线销售系统一Word文档下载推荐.docx
- 文档编号:17049120
- 上传时间:2022-11-28
- 格式:DOCX
- 页数:12
- 大小:86.92KB
实验7 实现简单的图书在线销售系统一Word文档下载推荐.docx
《实验7 实现简单的图书在线销售系统一Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《实验7 实现简单的图书在线销售系统一Word文档下载推荐.docx(12页珍藏版)》请在冰豆网上搜索。
`price`int(11)NOTNULL,
`picture`varchar(30)NOTNULL,
`recommend`int(11)NOTNULL,
PRIMARYKEY(`bookid`),
KEY`FK_Relationship_3`(`catalogid`)
)ENGINE=InnoDBDEFAULTCHARSET=utf8AUTO_INCREMENT=15;
--导出表中的数据`book`
INSERTINTO`book`(`bookid`,`catalogid`,`bookname`,`price`,`picture`,`recommend`)VALUES
(1,1,'
书1'
11,'
a19.jpg'
0),
(2,2,'
书2'
22,'
a15.jpg'
1),
(3,2,'
书3'
33,'
(5,2,'
书5'
55,'
(6,2,'
书6'
66,'
(7,2,'
书7'
77,'
(8,2,'
书8'
88,'
(9,2,'
书9'
99,'
(10,2,'
书10'
10,'
(11,2,'
书11'
(12,2,'
书12'
12,'
(13,2,'
书13'
13,'
(14,2,'
书14'
14,'
1);
--表的结构`catalog`
CREATETABLE`catalog`(
`catalogid`int(11)NOTNULLauto_increment,
`catalogname`varchar(20)NOTNULL,
PRIMARYKEY(`catalogid`)
)ENGINE=InnoDBDEFAULTCHARSET=utf8AUTO_INCREMENT=3;
--导出表中的数据`catalog`
INSERTINTO`catalog`(`catalogid`,`catalogname`)VALUES
(1,'
类别1'
),
(2,'
类别2'
);
--表的结构`orderitem`
CREATETABLE`orderitem`(
`orderitemid`int(11)NOTNULLauto_increment,
`orderid`int(11)defaultNULL,
`bookid`int(11)defaultNULL,
`quantity`int(11)NOTNULL,
PRIMARYKEY(`orderitemid`),
KEY`FK_Relationship_2`(`orderid`),
KEY`FK_Relationship_4`(`bookid`)
)ENGINE=InnoDBDEFAULTCHARSET=utf8AUTO_INCREMENT=1;
--导出表中的数据`orderitem`
--表的结构`orders`
CREATETABLE`orders`(
`orderid`int(11)NOTNULLauto_increment,
`userid`int(11)defaultNULL,
`orderdate`timestampNOTNULLdefaultCURRENT_TIMESTAMPonupdateCURRENT_TIMESTAMP,
PRIMARYKEY(`orderid`),
KEY`FK_Relationship_1`(`userid`)
--导出表中的数据`orders`
--表的结构`user`
CREATETABLE`user`(
`userid`int(11)NOTNULLauto_increment,
`username`varchar(20)NOTNULL,
`password`varchar(20)NOTNULL,
`sex`varchar(4)defaultNULL,
`age`int(11)defaultNULL,
PRIMARYKEY(`userid`)
)ENGINE=InnoDBDEFAULTCHARSET=utf8AUTO_INCREMENT=2;
--导出表中的数据`user`
INSERTINTO`user`(`userid`,`username`,`password`,`sex`,`age`)VALUES
admin'
'
NULL,NULL),
第一步:
实现数据层的对象关系映射。
第二步:
实现DAO、Service、Action三层处理代码。
第三步:
通过applictionContext.xml完成Bean配置。
第四步:
实现视图。
1、编写可保存图书信息的可持久化对象:
org/apex/bookstore/vo/Book.java
packageorg.apex.bookstore.vo;
importjava.util.HashSet;
importjava.util.Set;
/**
*Bookentity.
*
*@authorMyEclipsePersistenceTools
*/
publicclassBookimplementsjava.io.Serializable{
//Fields
privateIntegerbookid;
privateCatalogcatalog;
privateStringbookname;
privateIntegerprice;
privateStringpicture;
//Constructors
/**defaultconstructor*/
publicBook(){
}
/**minimalconstructor*/
publicBook(Stringbookname,Integerprice,Stringpicture){
this.bookname=bookname;
this.price=price;
this.picture=picture;
/**fullconstructor*/
publicBook(Catalogcatalog,Stringbookname,Integerprice,
Stringpicture){
this.catalog=catalog;
//Propertyaccessors
publicIntegergetBookid(){
returnthis.bookid;
publicvoidsetBookid(Integerbookid){
this.bookid=bookid;
publicCataloggetCatalog(){
returnthis.catalog;
publicvoidsetCatalog(Catalogcatalog){
publicStringgetBookname(){
returnthis.bookname;
publicvoidsetBookname(Stringbookname){
publicIntegergetPrice(){
returnthis.price;
publicvoidsetPrice(Integerprice){
publicStringgetPicture(){
returnthis.picture;
publicvoidsetPicture(Stringpicture){
}
2、编写Hibernate映射文件以实现ORM:
org/apex/bookstore/vo/Book.hbm.xml
<
?
xmlversion="
1.0"
encoding="
utf-8"
>
!
DOCTYPEhibernate-mappingPUBLIC"
-//Hibernate/HibernateMappingDTD3.0//EN"
"
MappingfileautogeneratedbyMyEclipsePersistenceTools
-->
hibernate-mapping>
<
classname="
org.apex.bookstore.vo.Book"
table="
book"
idname="
bookid"
type="
java.lang.Integer"
columnname="
/>
generatorclass="
native"
/id>
many-to-onename="
catalog"
class="
org.apex.bookstore.vo.Catalog"
fetch="
select"
lazy="
false"
catalogid"
/many-to-one>
propertyname="
bookname"
java.lang.String"
length="
20"
not-null="
true"
/property>
price"
picture"
30"
/class>
/hibernate-mapping>
3、在Spring配置文件applicationContext.xml中的sessionFactoryBean配置中合适的位置添加映射文件声明:
<
value>
org/apex/bookstore/vo/Book.hbm.xml<
/value>
4、在org/apex/bookstore/dao/impl/BookDAO.java中实现以下方法:
publicListgetNewBook(){
Sessionsession=getSession();
Queryquery=session.createQuery("
fromBookb"
query.setFirstResult(0);
query.setMaxResults(5);
Listbooks=query.list();
session.close();
returnbooks;
5、在org/apex/bookstore/action/BookAction.java中实现以下方法:
publicStringnewBook()throwsException{
Listbooks=bookService.getNewBook();
Maprequest=(Map)ActionContext.getContext().get("
request"
request.put("
books"
books);
returnSUCCESS;
6、配置struts.xml,添加对newBookAction的请求处理:
actionname="
newBook"
bookAction"
method="
resultname="
success"
/newBook_succcess.jsp<
/result>
/action>
7、修改applictionContext.xml内容,实现依赖注入以及ActionBean配置。
1)声明bookDAOBean,该Bean从已声明的baseDAO派生:
beanid="
bookDAO"
org.apex.bookstore.dao.impl.BookDAO"
parent="
baseDAO"
/bean>
2)声明bookServiceBean并注入BookDAO。
bookService"
org.apex.bookstore.service.impl.BookService"
<
<
refbean="
/>
3)修改bookActionBean配置,注入bookService依赖:
8、将视图newBook_succcess.jsp文件修改为以下内容:
%@pagecontentType="
text/html;
charset=gb2312"
%>
%@taglibprefix="
s"
uri="
/struts-tags"
H1>
SPANclass=green14b>
新书展示<
/SPAN>
/H1>
DIVclass=info_bk1>
s:
iteratorvalue="
#request['
books'
]"
id="
DIVclass=xh5>
imgheight=105width=80src="
/BookStore/picture/<
propertyvalue="
#book.picture"
BR>
#book.bookname"
br>
价格:
#book.price"
元<
formaction="
addToCart.action"
post"
数量:
inputtype="
text"
name="
quantity"
value="
1"
size="
4"
hidden"
#book.bookid"
image"
submit"
src="
/BookStore/picture/buy.gif"
/form>
/DIV>
/s:
iterator>
9、在index.jsp页面合适的位置用以下内容替换。
该服务器端strutstag的作用是嵌入newBook请求的响应内容:
executeResult="
四、实验思考
1、Hibernate、Spring、Struts三个框架的作用分别是什么?
struts一个web的mvc框架,处理页面数据,处理页面跳转等
hibernate一个面向对象的持久化框架,用面向对象的方式实现CRUD操作
spring一个IOC框架,管理对象的创建和装配。
整合后台的业务逻辑。
1.struts主要采用的是MVC设计模式,即ModelViewControl
Model即模型层:
主要做业务逻辑和业务处理
View即视图层:
用来显示数据给用户看
Control:
即控制层:
主要做接收用户请求,根据用户的需求调用相应的模型层进行业务处理,最后决定转发到那个视图响应数据给用户看
2.hibernate主要是用来和数据库打交道的,比如对数据进行持久化操作
3.spring最主要的技术应该是IOC(控制反转)和AOP(面向切面编程)。
2、Book.hbm.xml、struts.xml、applicationContext.xml文件中的粗体大号字显示的部分的作用是什么?
Book.hbm.xml中的粗体大号字是对表book的进行多对一的说明,struts.xml则是添加对newBookAction的请求处理
在applicationContext.xml则是实现依赖注入以及ActionBean配置
3、视图newBook_succcess.jsp文件中粗体大号字显示的部分的作用是什么?
作用是验证成功以后跳转到加入购物车的页面,并且选输入所购买的书的数量。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实验7 实现简单的图书在线销售系统一 实验 实现 简单 图书 在线 销售 系统
![提示](https://static.bdocx.com/images/bang_tan.gif)