1、综合性实验报告模版 软件学院综合性实验报告专业:JAVA 年级/班级: 09计算机1班 20102011学年第二学期课程名称Java EE程序设计指导教师周运姓名李金振学号0928524068实验地点过街楼机房实验时间5月20、27、6月3、10、17日上午1-2节项目名称用SSH框架开发网上书店实验类型综合性一、 实验目的1.熟悉并掌握spring框架。2.熟悉struts框架,了解struts2的工作机制及工作流程。3.掌握hibernate语言的使用,能够实现对数据库的增删查改。4.熟悉开发流程,能够开发网上书店。二、 实验仪器或设备1. 计算机一台2. 开发软件MyEclipse,和数
2、据库软件MySQL。三、 总体设计(设计原理、设计方案及流程等)1. 创建网上书店的数据库。2. 创建web工程。3. 加载Spring框架,以及所需的jar包,在Spring中创建相应的java类,构造bean对象,并调用bean对象。4. 进行数据库的链接。5. 加载hibernate框架,对数据进行持久化。6. 对数据库中对应的表进行hibernate逆向工程,以生成与之对应的java类。7. 加载struts框架及所需要的jar包。8. 对网上书店进行部署应用程序,启动测试。四、 实验步骤(包括主要步骤、代码分析等)1. 在MySQL中创建bookstore.sql(代码如下:)Cre
3、ate database bookstore;Use bookstore;create table book( bookid int auto_increment not null, catalogid int, bookname varchar(20) not null, price int not null, picture varchar(30) not null, primary key (bookid);create table catalog(catalogid int auto_increment not null, catalogname varchar(20) not nul
4、l, primary key (catalogid);create table orderitem( orderitemid int auto_increment not null, orderid int, bookid int, quantity int not null, primary key (orderitemid);create table orders( orderid int auto_increment not null, userid int, orderdate timestamp not null, primary key (orderid);create table
5、 user( userid int auto_increment not null, username varchar(20) not null, password varchar(20) not null, sex varchar(4), age int, primary key (userid);alter table book add constraint FK_Relationship_3 foreign key (catalogid) references catalog (catalogid) on delete restrict on update restrict;alter
6、table orderitem add constraint FK_Relationship_2 foreign key (orderid) references orders (orderid) on delete restrict on update restrict;alter table orderitem add constraint FK_Relationship_4 foreign key (bookid) references book (bookid) on delete restrict on update restrict;alter table orders add c
7、onstraint FK_Relationship_1 foreign key (userid) references user (userid) on delete restrict on update restrict;2. 在MyEclipse中创建webProject,命名为bookstore。3. 加载Spring框架,添加用户自定义包hibernate和配置文件applicationContext.xml.4. 在MyEclipse Database Explorer中创建数据库的链接。5. 加载hibernate框架。点击next6. 在MyEclipse Database Ex
8、plorer中选中所创建的表,进行hibernate逆向工程,生成与数据库表对应的java对象和映射文件。7. 修改web.xml,代码如下: web-app version=2.5 xmlns= xmlns:xsi=http:/www.w3.org/2001/XMLSchema-instance xsi:schemaLocation= index.jsp struts2 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter struts2 /* org.springframework.web.context
9、.ContextLoaderListener contextConfigLocation /WEB-INF/applicationContext.xml 8. 加载struts框架,并加载所需要的jar包。9. 创建DAO,BaseDAO.java,IBookDAO.java,ICatalogDAO.java,IUserDAO.javaBaseDAO.java代码如下:package org.apex.bookstore.dao;import org.hibernate.SessionFactory;import org.hibernate.classic.Session;public cla
10、ss BaseDAO private SessionFactory sessionFactory; public SessionFactory getSessionFactory() return sessionFactory; public void setSessionFactory(SessionFactory sessionFactory) this.sessionFactory=sessionFactory; public Session getSession() Session session=sessionFactory.openSession(); return session
11、; IBookDAO.java代码如下:package org.apex.bookstore.dao;import java.util.List;import org.apex.bookstore.vo.Book;public interface IBookDAO public List getBookByCatalogid(Integer catalogid); public List getBookByCatalogidPaging(Integer catalogid,int currentPage,int pageSize); public int getTotalByCatalog(I
12、nteger catalogid); public Book getBookById(Integer bookid); public List getRequiredBookByHql(String hql); public List getNewBook();ICatalogDAO.java代码如下:package org.apex.bookstore.dao;import java.util.List;public interface ICatalogDAO public List getAllCatalogs();IUserDAO.java代码如下:package org.apex.bo
13、okstore.dao;import org.apex.bookstore.vo.User;public interface IUserDAO public void saveUser(User user); public User validateUser(String username,String password); public boolean exitUser(String username);10. 创建service,IBookService.java, ICatalogService.java, IUserService.javaIBookService.java代码如下:p
14、ackage org.apex.bookstore.service;import java.util.List;import org.apex.bookstore.vo.Book;public interface IBookService public List getBookByCatalogid(Integer catalogid); public List getBookByCatalogidPaging(Integer catalogid,int currentPage,int pageSize); public int getTotalByCatalog(Integer catalo
15、gid); public Book getBookById(Integer bookid); public List getRequiredBookByHql(String hql); public List getNewBook();ICatalogService.java代码如下:package org.apex.bookstore.service;import java.util.List;public interface ICatalogService public List getAllCatalogs(); IUserService.java代码如下: package org.ap
16、ex.bookstore.service;import org.apex.bookstore.vo.User;public interface IUserService public void saveUser(User user); public User validateUser(String username,String password); public boolean exitUser(String username);11. 创建action,包括BookAction.java和UserAction.javaBookAction.java代码如下:package org.apex
17、.bookstore.action;import java.util.List;import java.util.Map;import org.apex.bookstore.service.IBookService;import org.apex.bookstore.service.ICatalogService;import org.apex.bookstore.util.Pager;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class Bo
18、okAction extends ActionSupport protected ICatalogService catalogService; protected IBookService bookService; protected Integer catalogid; private Integer currentPage=1; private String bookname; private int minPrice; private int maxPrice; public Integer getCatalogid() return catalogid; public void se
19、tCatalogid(Integer catalogid) this.catalogid = catalogid; public ICatalogService getCatalogService() return catalogService; public void setCatalogService(ICatalogService catalogService) this.catalogService = catalogService; public String browseCatalog() throws Exception List catalogs=catalogService.
20、getAllCatalogs(); Map request=(Map)ActionContext.getContext().get(request); request.put(catalogs,catalogs); return SUCCESS; public String browseBookPaging() throws Exception int totalSize=bookService.getTotalByCatalog(catalogid); Pager pager=new Pager(currentPage,totalSize); /List books=bookService.
21、getBookByCatalogid(catalogid); List books=bookService.getBookByCatalogidPaging(catalogid, currentPage, pager.getPageSize(); Map request=(Map)ActionContext.getContext().get(request); request.put(books, books); request.put(pager,pager); /购物车要返回时,需要记住返回的地址 Map session=ActionContext.getContext().getSess
22、ion(); request.put(catalogid,catalogid); return SUCCESS; public String browseBook() throws Exception List books=bookService.getBookByCatalogid(catalogid); Map request=(Map)ActionContext.getContext().get(request); request.put(books,books); return SUCCESS; public String searchBook() throws Exception S
23、tringBuffer hql = new StringBuffer(from Book b ); if(bookname!=null&bookname.length()!=0) hql.append(where b.bookname like % + bookname +%); / if(minPrice !=0 & maxPrice !=0)/ hql.append( and b.price +minPrice + and b.price +maxPrice); List books = bookService.getRequiredBookByHql(hql.toString(); Ma
24、p request = (Map)ActionContext.getContext().get(request); System.out.println(hql); System.out.println(bookname); request.put(books,books); return SUCCESS; public String newBook() throws Exception List books=bookService.getNewBook(); Map request=(Map)ActionContext.getContext().get(request); request.p
25、ut(books, books); return SUCCESS; public IBookService getBookService() return bookService; public void setBookService(IBookService bookService) this.bookService = bookService; public Integer getCurrentPage() return currentPage; public void setCurrentPage(Integer currentPage) this.currentPage = curre
26、ntPage; public String getBookname() return bookname; public void setBookname(String bookname) this.bookname = bookname; UserAction.java代码如下: package org.apex.bookstore.action;import java.util.Map;import org.apex.bookstore.service.IUserService;import org.apex.bookstore.service.impl.UserService;import
27、 org.apex.bookstore.vo.User;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class UserAction extends ActionSupport protected String username; protected String password; protected String sex; protected int age; protected User user; protected IUserServi
28、ce userService; public User getUser() return this.user; public void setUser(User user) this.user=user; public void setUserService(IUserService userService) this.userService=userService; public IUserService getUserService() return this.userService; public String execute() throws Exception User u=userService.validateUser(user.getUsername(),user.getPassword(); if(u !=null) Map sessio
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1