1.创建项目打开Myeclipse8.6,新建web工程,命名为ssh,点击完成。获得目录结构如下:2为项目添加Struts能力a)右键点击项目,选择MyEclipse-AddStrutsCapabilities,或者点击菜单栏中MyEclipse-ProjectCapabilities-AddStrutsCapabilities。b)选择struts2.1版本c)点击下一步,选择StrutsCore(核心库)和Struts2Spring(包含集成Spring插件)的两个最基本库,其他根据需要可以添加。d)点击完成,添加后的项目多了一个Struts.xml(用于配置action),web.xml中添加了核心过滤器的配置。filterfilter-namestruts2/filter-namefilter-classorg.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter/filter-class/filterfilter-mappingfilter-namestruts2/filter-nameurl-pattern/*/url-pattern/filter-mapping3.为项目添加Spring能力a)右键点击项目,选择MyEclipse-AddSpringCapabilities,或者点击菜单栏中MyEclipse-ProjectCapabilities-AddSpringCapabilities。b)选择Spring3.0版本,类库选择Spring3.0AOP、Spring3.0Core、Spring3.0PersistenceCore、Spring3.0PersistenceJDBC、Spring3.0Web5个库,并勾选复制到本地WEB-INF/lib目录。c)点击下一步,默认配置不需修改。d)点击完成,查看添加Spring后的项目,增加了applicationContex.xml文件4.为项目添加Hibernate能力a)右键点击项目,选择MyEclipse-AddHibernateCapabilities,或者点击菜单栏中MyEclipse-ProjectCapabilities-AddHibernateCapabilities。b)选择Hibernate3.3版本,如果要使用注记可选中启用annotation,但是会对后续自动生成实体类造成麻烦,不选就是用xml文件形式进行实体类映射,默认勾选Hibernate3.3Annotation&EntityManager、Hibernate3.3CoreLibraries两个类库,复制jar文件到本地WEB-INF/lib文件夹下。c)点击下一步,选择使用Spring的配置文件applicationContext.xml,这样就不会生成hibernate.cfg.xml,生成与否看个人习惯。d)点击下一步,选择已经存在的applicationContext.xml文件的位置以及创建sessionFactory的名字e)下一步,选择已经建立好的数据库连接mydbf)点击下一步,取消勾选,不生成HibernateSessionFactory类g)点击完成,查看工程,applicationContext.xml文件中增加了数据库连接的配置,包括datasource和sessionFactory7.搭建项目结构(分包)a)创建包结构:model(实体类)、dao(数据库访问类)、service(业务逻辑类)、web(控制类)8.搭建项目结构(model、dao生成)利用Myeclipse生成model和dao(最好自己构建可以减少冗余代码)转到MyEclipse数据库浏览视图下,双击mydb进行连接右键点击users表,选择Hibernate反向工程选择生成目录为app.model,勾选创建POJO即创建表对应的Javabean,同时创建映射文件hbm.xml,勾选更新hibernate配置,勾选创建springdao。下一步,选择id生成策略为native,因为users主键自增,或者在下一步中选择也可以点击完成,回到代码视图查看工程变化,model包下面增加了Users.java、UsersDAO.java、Users.hbm.xml三个文件将UsersDAO移动到dao包中,并抽出接口IUsersDAO因为位置移动需要修改applicationContex.xml中的配置beanid=UsersDAOclass=app.dao.UsersDAOpropertyname=sessionFactoryrefbean=sessionFactory/ref/property/bean9.搭建项目结构(service、web编写)a)编写service层代码,创建IUsersService接口和UsersService实现类,编写CRUD的方法。packageapp.service;importjava.util.List;importapp.model.Users;publicinterfaceIUsersService{publicListUsersfindAll();publicListUsersfindByName(Stringuname);publicUsersfindByUid(Integeruid);publicvoidaddUsers(Usersu)throwsException;publicvoidupdateUsers(Usersu)throwsException;publicvoiddelUsers(Integeruid)throwsException;publicUserslogin(Stringuname,Stringupass);}packageapp.service;importjava.util.List;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importapp.dao.IUsersDAO;importapp.dao.UsersDAO;importapp.model.Users;publicclassUsersServiceimplementsIUsersService{privatestaticfinalLoggerlog=LoggerFactory.getLogger(UsersService.class);privateIUsersDAOusersDAO=null;publicvoidsetUsersDAO(IUsersDAOusersDAO){this.usersDAO=usersDAO;}publicvoidaddUsers(Usersu)throwsException{usersDAO.save(u);}publicvoiddelUsers(Integeruid)throwsException{usersDAO.delete(findByUid(uid));}publicListUsersfindAll(){returnusersDAO.findAll();}publicListUsersfindByName(Stringuname){returnusersDAO.findByUname(uname);}publicUsersfindByUid(Integeruid){returnusersDAO.findById(uid);}publicUserslogin(Stringuname,Stringupass){Listlist=usersDAO.findByUname(uname);if(null==list||list.size()==0)returnnull;Usersu=(Users)usersDAO.findByUname(uname).get(0);if(null==u)returnnull;if(uname.equals(u.getUname())&&upass.equals(u.getUpass()))returnu;elsereturnnull;}publicvoidupdateUsers(Usersu)throwsException{usersDAO.attachDirty(u);}}b)编写web层UsersAction代码,用于对用户增删改查进行处理packageapp.web;importjava.util.ArrayList;importjava.util.List;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importorg.springframework.context.ApplicationContext;importapp.model.Users;importapp.service.IUsersService;importcom.opensymphony.xwork2.ActionContext;importcom.opensymphony.xwork2.ActionSupport;publicclassUsersActionextendsActionSupport{privatestaticfinalLoggerlog=LoggerFactory.getLogger(UsersAction.class);privateIntegeruid=newInteger(0);//用于修改删除时传参数privateUsersu=newUsers();//用于增改privateListUsersuserList=newArrayListUsers();//用于查询privateIUsersServiceusersService=null;publicStringaddUser()throwsException{try{usersService.addUsers(u);}catch(Exceptione){log.error(保存用户错误,e);returnERROR;}returnSUCCESS;}publicStringlistUser()throwsException{userList=usersService.findAll();returnSUCCESS;}publicStringeditUser()throwsException{try{u=usersService.findByUid(uid);}catch(Exceptione){log.error(修改用户错误,e);returnERROR;}returnSUCCESS;}publicStringupdateUser()throwsException{try{usersService.updateUsers(u);}catch(Exceptione){log.error(更新用户错误,e);returnERROR;}returnSUCCESS;}publicStringdelUser()throwsException{try{usersService.delUsers(uid);}catch(Exceptione){log.error(删除用户错误,e);returnERROR;}returnSUCCESS;}publicStringlogin()throwsException{try{UsersloginUser=usersService.login(u.getUname(),u.getUpass());if(null!=loginUser){ActionContextcontext=ActionContext.getContext();context.getSession().put(LOGINUSER