Struts2+Hibernate4+Spring4整合

jar包

配置文件

web.xml文件

<!-- needed for ContextLoaderListener -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext*.xml</param-value>
    </context-param>

    <!-- Bootstraps the root web application context before servlet initialization -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
   
   <!-- 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>

applicationContext.xml

<!-- 导入资源文件 -->
    <context:property-placeholder location="classpath:db.properties"/>

    <!-- 配置 C3P0 数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
        
        <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
        <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
    </bean>
    
    <!-- 配置 SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
        <property name="mappingLocations" value="classpath:com/atguigu/ssh/entities/*.hbm.xml"></property>
    </bean>
    
    <!-- 配置 Spring 的声明式事务 -->
    <!-- 1. 配置 hibernate 的事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <!-- 2. 配置事务属性 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="get*" read-only="true"/>
            <tx:method name="lastNameIsValid" read-only="true"/>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>
    
    <!-- 3. 配置事务切入点, 再把事务属性和事务切入点关联起来 -->
    <aop:config>
        <aop:pointcut expression="execution(* com.atguigu.ssh.service.*.*(..))" id="txPointcut"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
    </aop:config>

db.properties

jdbc.user=root
jdbc.password=111111
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///spring6

#peizhi c3p0 jiben shuxing 
jdbc.initPoolSize=5
jdbc.maxPoolSize=10
#...

hibernate.cfg.xml

<session-factory>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
        <property name="hibernate.format_sql">true</property>
        <property name="hibernate.hbm2ddl.auto">update</property>

    </session-factory>

applicationContext-beans.xml

<bean id="employeeDao" class="com.atguigu.ssh.dao.EmployeeDao">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    
    <bean id="departmentDao" class="com.atguigu.ssh.dao.DepartmentDao">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    
    <bean id="employeeService" class="com.atguigu.ssh.service.EmployeeService">
        <property name="employeeDao" ref="employeeDao"></property>
    </bean>
    
    <bean id="departmentService" class="com.atguigu.ssh.service.DepartmentService">
        <property name="departmentDao" ref="departmentDao"></property>
    </bean>
    
    <bean id="employeeAction" class="com.atguigu.ssh.actions.EmployeeAction"
        scope="prototype">
        <property name="employeeService" ref="employeeService"></property>    
        <property name="departmentService" ref="departmentService"></property>
    </bean>

 

struts.xml

 <action name="emp-*" class="employeeAction"
            method="{1}">
            <result name="list">/WEB-INF/views/emp-list.jsp</result>
            <result type="stream" name="ajax-success">
                <param name="contentType">text/html</param>
                <param name="inputName">inputStream</param>
            </result>    
            <result name="input">/WEB-INF/views/emp-input.jsp</result>
            <result name="success" type="redirect">/emp-list</result>
        </action>

 有两个jar包需要注意

 

 

posted @ 2016-06-13 15:30  hellolcc  阅读(252)  评论(0编辑  收藏  举报