SSH整合

来源:互联网 发布:新西兰华人 知乎 编辑:程序博客网 时间:2024/06/08 02:03

一、三大框架整合原理


1.struts2与spring整合的契机就是spring可以帮助struts2创建action对象


2.Hibernate中的sessionFactory甚至session都交给spring管理,aop事务也交给spring进行管理



二、相关导包工作


1.Hibernate需要的包


(1)hibernate/lib/required


(2)hibernate/lib/jpa(java持久化规范)


(3)数据库驱动


2.struts2需要的包


(1)struts2-blank.war/WEB-INF/lib(给的案例中的包)



注意:javassist包与hibernate中的重复,需要删除一个包

(2)struts2整合spring包(struts2-spring-plugin)

注意:导入这个包之后当struts2启动的时候就会寻找spring容器,找不到会抛出异常

3.spring需要的包

(1)基本的4+2(core+beans+context+expression+logging+log4j)

(2)整合web包(spring-web)

(3)整合Aop(spring-aop+spring-aspect+aop联盟+aopweaving)

(4)整合Jdbc和事务(spring-jdbc+spring-tx+C3P0+spring-orm)

(5)整合JUnit4测试(spring-test)

4.如果IDE是Eclipse的话需要导入标签库

standard.jar以及jstl.jar

三、单独配置spring容器

1.步骤

(1)创建applicationContext.xml文件并且导入约束(beans+context+aop+tx)


注意:这四个约束只有beans是不需要前缀的,其余的三个都是需要约束的


(2)在web.xml文件中配置spring容器随着web的启动而启动的监听器(ContextLoaderListener)以及告知applicationContext.xml的文件位置,配置如下:

<!--配置spring容器随着web的启动而启动  -->  <listener>      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>  <!--配置spring配置文件的位置  -->  <context-param>      <param-name>contextConfigLocation</param-name>      <param-value>classpath:applicationContext.xml</param-value>  </context-param>
四、单独配置struts2

1.步骤

(1)创建配置文件struts.xml,导入dtd约束,在struts2-core中寻找struts.xml的约束,然后将约束复制进去

(2)在web.xml文件中配置struts2的核心过滤器(StrutsPrepareAndExecuteFilter),具体配置如下:

 <!--配置struts2的核心过滤器  -->  <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>
五、整合spring和struts2

1.步骤

(1)配置常量


在struts.xml文件中使用标签,具体配置如下:

<constant name="struts.objectFactory" value="spring"></constant>
配置这个常量的意思就是将action的创建交给spring容器

(2)整合方案

在struts.xml文件中,将action的class属性的值设置成spring中的action对象的bean的name属性值


六、单独配置hibernate

1.步骤

(1)在hibernate.cfg.xml文件中不需要设置事务的隔离级别以及不需要设置session与当前对象进行绑定,因为spring容器自己会管理事务以及自己会管理session

七、整合spring和hibernate


1.步骤



(1)将sessionFactory配置到spring容器中


(2)加载配置


具体代码如下:

<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"><property name="hibernateProperties"><props><prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop><prop key="hibernate.connection.url">jdbc:mysql:///crm_hibernate</prop><prop key="hibernate.connection.username">root</prop><prop key="hibernate.connection.password">root</prop><prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><prop key="hibernate.show_sql">true</prop><prop key="hibernate.format_sql">true</prop><prop key="hibernate.hbm2ddl.auto">update</prop></props></property><!--写到domain层就好了  --><property name="mappingDirectoryLocations" value="classpath:com/sgr/domain"></property></bean>













原创粉丝点击