SSH整合

来源:互联网 发布:验证码注册软件 编辑:程序博客网 时间:2024/05/22 16:41

导入jar包

1.导入Struts2的14个jar包2.导入Hibernate的20个jar包3.导入Spring的15个jar包    ----4个核心包:beans,core,context,expression    ----2个日志包:logging规范包,log4j的实现包    ----aop4个包:两个aspectj的包,一个aop联盟规范包,一个springaop实现包    ----一个test包,用于整合junit测试    ----一个web,用于整合控制层    ----三个整合数据访问层,一个tx包控制事务,一个jdbc和orm整合数据访问层4.javassist有两个,这样会冲突,删去任意一个即可

导入配置文件

1.导入Struts2的struts.xml2.hibernate导入hibernate.cfg.xml,以及与bean相关的xxx.hbm.xml3.Spring导入applicationContext.xml与log4j.properties

Spring整合Struts2(本来是struts框架自动生成,交给Spring)

1.将xxxAction交给Spring管理,在application中设置<bean id="xxxAction" class="包名.xxxAction" scope="prototype">然后在struts.xml修改为<action name="xxx_*" class="xxxAction" method="{1}">action中的class为bean中的id,然后xxxService需要手动注入2.注意scope需设置为多例,即prototype。

Spring整合Hibernate(抛弃hibernate.cfg.xml)

1.先配置连接池信息     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">        <property name="driverClass" value="com.mysql.jdbc.Driver"/>        <property name="jdbcUrl" value="jdbc:mysql:///xxx"/>        <property name="user" value="xxx"/>        <property name="password" value="xxx"/>    </bean>2.配置sessionFactory   <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">        <!-- 整合连接池,四大属性 -->        <property name="dataSource" ref="dataSource"></property>        <!-- 整合其余属性,显示SQL语句以及方言 -->        <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="mappingResources">            <list>                <value>com/he/domain/Customer.hbm.xml</value>            </list>        </property>    </bean>3.sessionFactory配好后,向dao注入,此时还缺少最后一步事物    <!-- 配置事务 -->    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">         <property name="sessionFactory" ref="sessionFactory"/>    </bean>    <!-- 开启注解事务 -->4.在dao中使用@Transactional注解5.dao中使用session,需要继承HibernateDaoSupport,然后可直接使用this.getHibernateTemplate()获取模版对象,通过它既可以直接使用save,update等方法,也可以通过它获取session对象,然后进行操作,而且都是与线程绑定的session

遇到的问题

1.在最后如果使用hibernate的延时加载会出现nosession问题,因为dao交给Spring管理了,会出现session的问题,需要在web.xml中配置过滤器,使session转到web层 <filter>        <filter-name>OpenSessionInViewFilter</filter-name>        <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>    </filter>    <filter-mapping>        <filter-name>OpenSessionInViewFilter</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>2.我的web.xml配置    <!-- 整合web设置的监听器 -->      <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>      </listener>      <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath:applicationContext.xml</param-value>      </context-param>      <!-- 解决延迟加载造成的nosession問題 -->      <filter>         <filter-name>OpenSessionInViewFilter</filter-name>         <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>      </filter>      <filter-mapping>         <filter-name>OpenSessionInViewFilter</filter-name>         <url-pattern>/*</url-pattern>      </filter-mapping>      <!-- 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>
原创粉丝点击