【Spring九】三大框架的整合

三大框架整合的顺序:先hibernate,后spring,struts2
   1、建立工程
   2、设置编码格式
   3、设置所有的jsp的编码格式(preference->jsp)
   4、导入jar包
   5、写hibernate的配置文件、持久化类、映射文件
Classes.hbm.xml:
xml  version= "1.0" encoding ="utf-8"?>
DOCTYPE  hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"  >

< hibernate-mapping >
      < class  name="com.oterman.domain.Classes" >
            <id name= "cid" type ="long" length="5">
                <column name= "cid">column >
                <generator class= "increment">generator >
            id>
            <property name= "cname" length ="20"> property>
            <property name= "description" length="200" >property>
      class  >
hibernate-mapping >
================================
hibernate.cfg.xml:
xml  version= '1.0' encoding ='utf-8'?>
DOCTYPE  hibernate-configuration PUBLIC
         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
< hibernate-configuration >
< session-factory >
      < property  name="connection.username"> root property>
      < property  name="connection.password"> root property>
      < property  name="connection.url">
          jdbc:mysql:// localhost:3306/hibernate_itheima03
      property  >
      < property  name="dialect">
          org.hibernate.dialect.MySQLDialect
      property  >
      < property  name="connection.driver_class">
          com.mysql.jdbc.Driver
      property  >
     
      < property  name="hbm2ddl.auto"> update property>
      < property  name="show_sql"> true property>

      < mapping  resource="com/oterman/domain/Classes.hbm.xml" />

session-factory >
hibernate-configuration >

 
   6、写dao
   7、写service
   8、spring的配置文件
       (1)、写sessionFactory
applicationContext.xml:
< beans  xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx= "http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
          http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
          http://www.springframework.org/schema/aop
          http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
          http://www.springframework.org/schema/tx
          http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"  >
      < import  resource="applicationContext-db.xml"/>
      < import  resource="applicationContext-classes.xml"/>
     
beans >  
=======================================================
applicationContext-db.xml:
< beans  xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx= "http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
          http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
          http://www.springframework.org/schema/aop
          http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
          http://www.springframework.org/schema/tx
          http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"  >
            
            <bean id= "sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
                <property name= "configLocation" >
                     <value> classpath:hibernate/hibernate.cfg.xmlvalue >
                property>
            bean>
          
            <bean id= "transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" >
                <property name= "sessionFactory">
                     <ref bean= "sessionFactory"/>
                property>
            bean>
          
            <tx:advice id= "tx" transaction-manager="transactionManager" > 
                <tx:attributes>
                     <tx:method name= "save*" read-only="false" />
                tx:attributes>
            tx:advice>
          
            <aop:config>
            
                <aop:pointcut expression="execution(* com.oterman.service.*.*(..))" id= "trans"/>
                <aop:advisor advice-ref="tx" pointcut-ref="trans" />
            aop:config>

beans >
       (2)、测试
      @Test
      public  void testSessionFactory(){
          ApplicationContext context=  new ClassPathXmlApplicationContext("spring/applicationContext.xml" );
          SessionFactory sessionFactory=(SessionFactory) context.getBean( "sessionFactory"  );
     }

       (3)、写dao和service
       (4)、测service
       (5)、aop的配置
       (6)、测试service,看service是否是代理对象
      @Test
      public  void testSaveClasses(){
          ApplicationContext context=  new ClassPathXmlApplicationContext("spring/applicationContext.xml" );
          ClassesService classesService=(ClassesService) context.getBean( "classesService"  );
          
          Classes classes=  new Classes();
          classes.setCname(  "hah");
          classesService.saveClasses(classes);
          
     }

   9、写action
   10、把action放入到spring中
   11、写action的配置文件
struts.xml:
xml  version= "1.0" encoding ="UTF-8"?>
DOCTYPE  struts PUBLIC
       "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
       "http://struts.apache.org/dtds/struts-2.1.7.dtd" >
< struts >
    
    <constant name="struts.devMode" value= "true"/>
    
    <package name="struts-global" namespace="/" extends="struts-default" >
            <global-results>
                <result name= "errHandler" type ="chain">
                     <param name="actionName" >errorProcessor param>
                result>
            global-results>
          
            <global-exception-mappings>
                <exception-mapping exception="java.lang.Exception"
                     result= "errHandler" />
            global-exception-mappings>

            <action name= "errorProcessor" class="com.oterman.exception.ErrorProcessor" >
                <result> error.jspresult >
            action>
      package  >
 
    <include file="struts2/struts-classes.xml">include >
struts >   

================================
struts-classes.xml:
xml  version= "1.0" encoding ="UTF-8"?>
DOCTYPE  struts PUBLIC
       "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
       "http://struts.apache.org/dtds/struts-2.1.7.dtd" >
< struts >
      < package  name="classes"  namespace="/" extends="struts-global" >
            
            <action name= "classesAction_*" method ="{1}" class="classesAction" >
                <result> index.jspresult >
            action>
      package  >

struts >   


   12、写web.xml配置文件 
xml  version= "1.0" encoding ="UTF-8"?>
< web-app  version= "2.5"
       xmlns= "http://java.sun.com/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  >
   <welcome-file-list >
     <welcome-file >index.jsp welcome-file>
   welcome-file-list >
 
     
      < listener  >
            <listener-class> org.springframework.web.context.ContextLoaderListenerlistener-class >
      listener  >
      < context-param  >
            <param-name> contextConfigLocationparam-name >
            <param-value> classpath:spring/applicationContext.xmlparam-value >
      context-param  >
     
     
      < filter  >
            <filter-name> struts2 filter-name>
            <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter filter-class>
      filter  >
      < filter-mapping  >
            <filter-name> struts2 filter-name>
            <url-pattern> /* url-pattern>
      filter-mapping  >
 
web-app >

   13、写jsp
   14、测试
整个工程结构视图:



你可能感兴趣的:(Spring,Struts2,Hibernate,框架,spring,hibernate,struts2.0)