SSH配置步骤(详细教程)

第一步:导入相应的jar包

struts2的9个jar包

hibernatejar包(有C3P0的相应jar包

spring的核心jar包 切面AOP相关jar包 与struts整合的webJar包   与hibernate整合的ormjar包

第二步:配置web.xml文件

注意配置struts的过滤器 和spring的加载文件路径和监听器

第三步 配置各个框架的相应文件

hibernate:配置entity与表的关系映射(eg:dept.hbm.xml)

spring:

applicationContext-action.xml

applicationContext-dao.xml

applicationContext-service.xml

applicationContext-public.xml

主要负责一些项目单例的对象放入IOC容器中:

首先配置连接池DataSource,其次再SessionFactory,在配置声明式事务管理(spring与hibernate整合必须配置)

datasource的配置则是c3P0连接池的一些基本属性

SessionFactory中配置有DataSource

注入DataSource 与configure的配置,可以通过hibernate.cfg.xml配置再通过

<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>

也可以通过hibernateProperties进行相应配置hibernate的常量与上面映射文件的路径

<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>
            </props>
        </property>
        <property name="mappingLocations">
            <list>
                <value>classpath:com/gh/entity/*.hbm.xml</value>
            </list>
        </property>
    </bean>

声明书事务管理则是配置AOP是项目再service层对事务的支持

    <!-- 三 声明式事务管理配置 -->
    <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="*" read-only="false"/>
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.gh.serviceImp.DeptServiceImp.*(..))"/>
    </aop:config>   

红色的部分必须是接口,因为spring默认是JDK代理,这在上一个讲了

接下来配置struts.xml文件,配置action与方法,这与struts框架没什么区别

描述了一下步骤,其实理解了三个框架就能很简单的记到这些步骤

猜你喜欢

转载自blog.csdn.net/weixin_40234548/article/details/81272197
今日推荐