您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 项目/工程管理 > 第9章Spring与Struts2Hibernate框架的整合
第9章Spring与Struts2、Hibernate框架的整合基础一、搭建Struts框架注:导入的jar包与前面课程介绍的不同二、搭建Spring框架1.添加的类库包括AOP、Core、PersistentCore、JDBC、Web库2.修改applicationContext.xml的存储目录为WebRoot/WEB-INF三、搭建hibernate框架1.建立数据库连接2.搭建框架,注意选择的配置文件为applicationContext.xml而不是hibernate.cfg.xml。1)设置SessionFactory的Beanid为sessionFactory。2)设置DataSource的BeanId为dataSource3)取消“CreateSessionFactoryclass”选项4)导入包commons-pool-1.3到lib文件夹。四、修改web.xml加入Spring监听器配置,至此所有框架搭建完毕。listenerlistener-classorg.springframework.web.context.ContextLoaderListener/listener-class/listener五、数据库建表六、在工程下创建domain、dao包,反向工程自动生成实体类、配置文件、dao类。1)反向工程将生成的dao文件2)移动dao类到dao包,修改由于移动dao类后applicationContext.xml出现的错误,并将id名首字母改成小写。3)为dao类提取出接口。在工作区右键dao文件-》refactor-》extractinterface七、编写service、service接口并配置1)创建service包2)创建service类,定义方法findScoreBySnumber()。publicclassStudentService{privateIStudentDAOstudentDAO;publicvoidsetStudentDAO(IStudentDAOstudentDAO){this.studentDAO=studentDAO;}publicListfindScoreBySnumber(Studentstudent){returnstudentDAO.findBySnumber(student.getSnumber());}}3)为service类提取抽象接口,提取时不要把set、get方法选中。4)在applicationContext.xml配置该servicebean,右键文件的空白处-》spring-》newbean。。生成配置文件后去掉多余的属性。beanid=studentServiceclass=com.spring.service.StudentServicepropertyname=studentDAOrefbean=studentDAO//property/bean八、在applicationContext.xml中配置事务代理,将以下内容放到sessionFactory配置节点下方,并修改绿色底纹部分内容。!--声明Hibernate事务管理器--beanid=transactionManagerclass=org.springframework.orm.hibernate3.HibernateTransactionManagerpropertyname=sessionFactoryref=sessionFactory//bean!--声明事务拦截器--beanid=transactionInterceptorclass=org.springframework.transaction.interceptor.TransactionInterceptorpropertyname=transactionManagerref=transactionManager/propertyname=transactionAttributesprops!--设置事务管理策略--propkey=get*PROPAGATION_REQUIRED,readOnly/proppropkey=*PROPAGATION_REQUIRED/prop/props/property/bean!--声明代理创建--beanid=ProxyCreatorclass=org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator!--指定需生成业务代理的Bean--propertyname=beanNameslistvaluestudentService/value/list/property!--调用事务拦截器--propertyname=interceptorNameslistvaluetransactionInterceptor/value/list/property/bean九、编写JSP页面s:formaction=querys:textfieldname=textfield1label=请输入考号/s:textfields:submitvalue=查询/s:submit/s:form十、建立action包,创建action类并配置1)创建actionpublicclassStudentActionextendsActionSupport{privateIStudentServicestudentService;privateStringtextfield1;publicIStudentServicegetStudentService(){returnstudentService;}publicvoidsetStudentService(IStudentServicestudentService){this.studentService=studentService;}publicStringgetTextfield1(){returntextfield1;}publicvoidsetTextfield1(Stringtextfield1){this.textfield1=textfield1;}publicStringfindBySnumber(){Studentstudent=newStudent();student.setSnumber(textfield1);Listlist=studentService.findScoreBySnumber(student);HttpServletRequestrequest=ServletActionContext.getRequest();request.setAttribute(list,list);returnsuccess;}}2)在applicationContext.xml中配置actionbeanbeanid=studentActionclass=com.spring.action.StudentActionpropertyname=studentServicerefbean=studentService//property/bean3)在Struts.xml配置action,class属性不再指定包名,而是指定applicationContext.xml中bean的id名,这样在找action时,spring框架参与其中。!--此处的class属性值不是实际的类--actionname=queryclass=studentActionmethod=findBySnumberresultname=success/result.jsp/result/action十一、编写结果页面result.jspbodytabletrtd考号/tdtd姓名/tdtd院系/tdtd专业/tdtd科目/tdtd成绩/td/tr%Listlist=(List)request.getAttribute(list);Studentstudent=(Student)list.get(0);Setset1=student.getScoreses();for(Iteratorit=set1.iterator();it.hasNext();){Scorescore=(Score)it.next();%trtd%=student.getSnumber()%/tdtd%=student.getName()%/tdtd%=student.getDepartment()%/tdtd%=student.getSpecialty()%/tdtd%=score.getCourses().getName()%/tdtd%=score.getScore()%/td/tr%}%/table/body注意:lazy=“false”的添加
本文标题:第9章Spring与Struts2Hibernate框架的整合
链接地址:https://www.777doc.com/doc-2199711 .html