IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

spring(四) 手动整合web项目(SSH)

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

      清楚了spring的IOC 和 AOP,最后一篇就来整合SSH框架把,记录下来,以后应该会用的到。

                    –WZY

一、web项目中如何使用spring?

      当tomcat启动时,就应该加载spring的配置文件,而不是每次都要手动使用new ClassPathXmlApplicationContext来加载xml。所以,spring提供了一个ContextLoaderListener。有了它,tomcat启动时,就会加载配置文件。

      导入spring.web….jar

          103_1.png

      1、web.xml中添加监听器

          103_2.png

103_3.png 103_4.png

 <!-- 配置监听器,用于加载spring 配置文件 -->
     <listener>
         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
     </listener>

View Code

        这样配置的监听器,默认加载的是WEB-INF目录下的xml文件,而我们的配置文件在src下,所以需要进行下一步的配置。

      2、web.xml中配置 applicationContext.xml 位置

          103_5.png

103_6.png 103_7.png

 <!-- 设置web全局初始化参数,设置配置文件位置 
         * 名称#contextConfigLocation 固定值
         * 值# “classpath:”表示类路径(src)
             也可以有子包 #  classspath:com/wuhao/applicationContext.xml
     -->
     <context-param>
         <param-name>contextConfigLocation</param-name>
         <param-value>classpath:applicationContext.xml</param-value>
     </context-param>

View Code

      3、从ServletContext中获得spring容器,创建的容器已经放在ServletContext中了

          103_8.png  

103_9.png 103_10.png

 //方式1:手动从ServletContext作用域获得内容
         //WebApplicationContext applicationContext = (WebApplicationContext) this.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

         //方式2:提供工具类
         WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());

View Code

          

二、SSH整合所用到的jar包

      2.1、struts2所需jar

        版本:2.3.15.1

        位置:struts-2.3.15.1\apps\struts2-blank\WEB-INF\lib

            103_11.png

          总共:13个jar包        

      2.2、hibernate

          版本:3.6.10

          所需jar

              1、核心jar:hibernate3.jar

                  103_12.png

              2、必须:lib/required 所有

                  103_13.png

              3、jpa :lib\jpa 所有

                  103_14.png

              4、c3p0 \lib\optional\c3p0

                  103_15.png

              5、 log4j 整合jar  slf4j-log4j12-1.7.2.jar

                  103_16.png

              6、mysql驱动包  mysql-connector-java-5.1.22-bin.jar

                   103_17.png

              7、二级缓存  ehcache-1.5.0.jar 、backport-util-concurrent.jar 原本要导3个,logging包在前面struts包中有了,所以无需在导入

                    103_18.png              

           hibernate总共:13个jar包

      2.3、spring

          版本:3.2.0

          jar包:

             核心:4个(core、beans、expression、context) + commons-logging..jar(这个jar包在前面很多地方导入过,struts2、hibernate的二级缓存、spring都需要,只需要导一份即可)

                  103_19.png

              aop:aop联盟、spring aop、aspectj、spring-aspect

                  aop联盟:spring-framework-3.0.2.RELEASE-dependencies\org.aopalliance\com.springsource.org.aopalliance\1.0.0

                  spring aop和spring-aspect:在核心包中

                  aspectj:spring-framework-3.0.2.RELEASE-dependencies\org.aspectj\com.springsource.org.aspectj.weaver\1.6.8.RELEASE     

              jdbc:2个 spring-jdbc、spring-tx

              web:1个 spring-web

              test:1个 spring-test (整合测试)

          总共:12个

      2.4、整合jar

          spring3 整合hibernate3,spring提供整合jar : spring-orm…jar

          struts2 整合 spring3 ,struts提供整合jar:struts2-spring-plugin-2.3.15.1.jar

              位置:struts-2.3.15.1\lib

          总共:2个

        

      2.5、整理jar

          前面拿到总共39个,去除一个重复的,剩下总共38个。

                103_20.png

          

三、配置文件

      3.1、hibernate

          hibernate.cfg.xml  核心配置文件

          *.hbm.xml 映射文件(domain)

      3.2、spring

          applicationContext.xml

          jdbcInfo.properties

          web.xml(ContextLoaderListener)

      3.3、struts2

          struts.xml

          web.xml(前端过滤器  StrutsPrepareAndExecuteFilter)

      

四、spring整合hibernate

      4.1、首先要了解hibernate的配置文件,和hibernate需要什么,然后如何将其让spring来管理。

          hibernate.cfg.xml

            103_21.png

            103_22.png

        通过这两个图,可以知道,spring需要帮hibernate获取session,而获取session则先要获得sessionFactory,获取sessionFactory则需要加载配置文件。所以spring就需要提供两个东西,一个sessionFactory,一个session。spring如何做的呢?

            103_23.png

103_24.png 103_25.png

 <?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="
 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

 <!-- 配置数据源  c3p0,-->
 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
     <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
     <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test_1"></property>
     <property name="user" value="root"></property>
     <property name="password" value="root"></property>
 </bean>

 <!-- 获取sessionFactory需要加载hibernate.cfg.xml获取其中的配置的信息,那么直接将那些配置信息在这里配置 -->
 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
     <property name="dataSource" ref="dataSource"></property>
     <!-- 配置一些其他的信息,方言等 -->
     <property name="hibernateProperties">
         <props>
             <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>
     <!-- 添加映射文件 -->
     <property name="mappingLocations" value="classpath:com/wuhao/domain/User.hbm.xml"></property>
 </bean>

 <!-- hibernate中通过sessionFactory创建得session对对象进行操作,spring提供一个hibernateTemplate进行操作,跟spring中的jdbcTemplate一样,
     继承HibernateDaoSupport后,注入SessionFactory即可,在dao层就不用在写hibernateTemplate的set方法了。
  -->
  <bean id="userDao" class="com.wuhao.dao.UserDao">
      <property name="sessionFactory" ref="sessionFactory"></property>
  </bean>


 </beans>

View Code

        使用:

            103_26.png 

        这样一来,就可以将hibernte.cfg.xml给删除掉了。

        还有一个问题,事务的处理。

        简单,之前一篇文章我们就讲解了spring如何进行事务管理的,那时候使用的是jdbc管理器,现在换一个即可,使用hibernate事务管理器

            103_27.png

        所以整合完hibernate后,spring的配置文件就成了这样

103_28.png 103_29.png

 <?xml version="1.0" encoding="UTF-8"?>
 <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.xsd
                               http://www.springframework.org/schema/aop 
                               http://www.springframework.org/schema/aop/spring-aop.xsd
                               http://www.springframework.org/schema/tx 
                               http://www.springframework.org/schema/tx/spring-tx.xsd">
 <!-- hibernate -->
 <!-- 配置数据源  c3p0,-->
 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
     <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
     <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test_1"></property>
     <property name="user" value="root"></property>
     <property name="password" value="root"></property>
 </bean>

 <!-- 获取sessionFactory需要加载hibernate.cfg.xml获取其中的配置的信息,那么直接将那些配置信息在这里配置 -->
 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
     <property name="dataSource" ref="dataSource"></property>
     <!-- 配置一些其他的信息,方言等 -->
     <property name="hibernateProperties">
         <props>
             <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>
     <!-- 添加映射文件 -->
     <property name="mappingLocations" value="classpath:com/wuhao/domain/User.hbm.xml"></property>
 </bean>

 <!-- hibernate中通过sessionFactory创建得session对对象进行操作,spring提供一个hibernateTemplate进行操作,跟spring中的jdbcTemplate一样,
     继承HibernateDaoSupport后,注入SessionFactory即可,在dao层就不用在写hibernateTemplate的set方法了。
  -->
  <bean id="userDao" class="com.wuhao.dao.UserDao">
      <property name="sessionFactory" ref="sessionFactory"></property>
  </bean>

  <!-- hiberntae事务管理器,也需要将sessionFactory加进来,从hibernate中得代码就可以看出来,事务也是由sessionFactory获取来的-->
 <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
     <property name="sessionFactory" ref="sessionFactory"></property>
 </bean>
 <!-- 事务详情 -->
 <tx:advice id="txAdvice" transaction-manager="txManager">
     <tx:attributes>
         <tx:method name="add" propagation="REQUIRED"/>
     </tx:attributes>
 </tx:advice>

 <!-- aop事务管理 ,要使用事务和aop,需要在上面导入dtd。我这里图方便,没写接口,所以需要叫spring使用cglib-->
 <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
 <aop:config>
     <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.wuhao.service.*.*(..))"/>
 </aop:config>
 </beans>

View Code

      总结:其实非常简单,一开始整合肯定毫无头绪,不知道从哪里下手,那么就先将一个最简单的hibernate操作对象的小demo写出来,看看hibernate需要什么,然后spring就配置什么,给什么。这样就简单明了了。

五、struts2整合spring

      5.1、struts2自己创建action。而不用spring帮我们创建

          struts.xml

              103_30.png       

          action中userService对象的创建就需要让spring帮我们创建

              103_31.png    

          applicationContext.xml

              103_32.png

          这样,就可以了。

      

      5.2、spring帮我们创建action,struts2什么也不用做了。‘

            前提:需要导入struts-spring-plugin…jar

          103_33.png

      全部整合完后的applicationContext.xml

103_34.png 103_35.png

 <?xml version="1.0" encoding="UTF-8"?>
 <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.xsd
                               http://www.springframework.org/schema/aop 
                               http://www.springframework.org/schema/aop/spring-aop.xsd
                               http://www.springframework.org/schema/tx 
                               http://www.springframework.org/schema/tx/spring-tx.xsd">
 <!-- struts2自己创建Action的配置,就不需要21到24行的代码,如果是spring创建action,则需要 -->
 <!-- 
     action中的userService对象,在这里配置就能够获取到。为什么能够直接配置就能够得到,
     需要导入struts-spring-plugin...jar,因为有它和default.properties的结合所以才能够直接配置得到
  -->
 <bean id="userService" class="com.wuhao.service.UserService">
 <!-- userDao在hibernate整合时就已经创建了bean,所以就直接拿过来用 -->
     <property name="userDao" ref="userDao"></property>
 </bean>          
 <!-- 配置action -->
 <bean id="userAction" class="com.wuhao.action.UserAction">
     <property name="userService" ref="userService"></property>
 </bean> 

 <!-- hibernate -->
 <!-- 配置数据源  c3p0,-->
 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
     <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
     <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test_1"></property>
     <property name="user" value="root"></property>
     <property name="password" value="root"></property>
 </bean>

 <!-- 获取sessionFactory需要加载hibernate.cfg.xml获取其中的配置的信息,那么直接将那些配置信息在这里配置 -->
 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
     <property name="dataSource" ref="dataSource"></property>
     <!-- 配置一些其他的信息,方言等 -->
     <property name="hibernateProperties">
         <props>
             <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>
     <!-- 添加映射文件 -->
     <property name="mappingLocations" value="classpath:com/wuhao/domain/User.hbm.xml"></property>
 </bean>

 <!-- hibernate中通过sessionFactory创建得session对对象进行操作,spring提供一个hibernateTemplate进行操作,跟spring中的jdbcTemplate一样,
     继承HibernateDaoSupport后,注入SessionFactory即可,在dao层就不用在写hibernateTemplate的set方法了。
  -->
  <bean id="userDao" class="com.wuhao.dao.UserDao">
      <property name="sessionFactory" ref="sessionFactory"></property>
  </bean>

  <!-- hiberntae事务管理器,也需要将sessionFactory加进来,从hibernate中得代码就可以看出来,事务也是由sessionFactory获取来的-->
 <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
     <property name="sessionFactory" ref="sessionFactory"></property>
 </bean>
 <!-- 事务详情 -->
 <tx:advice id="txAdvice" transaction-manager="txManager">
     <tx:attributes>
         <tx:method name="add" propagation="REQUIRED"/>
     </tx:attributes>
 </tx:advice>

 <!-- aop事务管理 ,要使用事务和aop,需要在上面导入dtd。我这里图方便,没写接口,所以需要叫spring使用cglib-->
 <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
 <aop:config>
     <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.wuhao.service.*.*(..))"/>
 </aop:config>
 </beans>

View Code

六、总结

      那么到这里,手动整合ssh就结束了,其实非常简单,一步步来,从导包到整合hibernate在整合struts的步骤进行,那么就可以成功整合了,上面所有的我都没有采用接口编程,为了方便,日后注意一下这点即可。非常简单。

      但是在整合hibernate时,我们的你domain都映射文件都是自己手动编写的,实际开发中肯定不需要一个个手动编写,因为是先有数据库,然后在来写你domain,所以之后会说到通过反转引擎根据数据库中的表来创建domain和映射文件。

        

文章永久链接:https://tech.souyunku.com/?p=24249


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(76) 打赏



未经允许不得转载:搜云库技术团队 » spring(四) 手动整合web项目(SSH)

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367