zoukankan      html  css  js  c++  java
  • spring+struts2+hibernate整合

    web.xml需要配置

    复制代码
       <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext*.xml</param-value>
       </context-param>
    
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        
        <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>
    复制代码

    db.properties

    复制代码
    jdbc.user=root
    jdbc.password=tiger
    jdbc.driverClass=com.mysql.jdbc.Driver
    jdbc.jdbcUrl=jdbc:mysql:///ssh
    
    jdbc.initPoolSize=5
    jdbc.maxPoolSize=10
    复制代码

    applicationContext.xml

    复制代码
    <context:property-placeholder location="classpath:db.properties" />
        
        <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>
    
        <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/my/ssh/entities/*.hbm.xml"></property>
        </bean>
        
        <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>
        
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <tx:method name="get*" read-only="true"/>
                <tx:method name="isValidate" read-only="true"/>
                <tx:method name="*"/>
            </tx:attributes>
        </tx:advice>
        
        
        <aop:config>
            <aop:pointcut expression="execution(* com.my.ssh.service.*.*(..))" id="pointCut"/>
            <aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut"/>
        </aop:config>
    复制代码

    applicationContext-beans.xml

      配置各种bean  注意:对于Struts2 的Action类  scope 需要配置为prototype (不为单例)

    <bean id="employeeAction" class="com.my.ssh.actions.EmployeeAction" scope="prototype">
      <property name="employeeService" ref="employeeService"></property>
      <property name="departmentService" ref="departmentService"></property>
    </bean>

     hibernate.cfg.xml

    复制代码
    <hibernate-configuration>
        <session-factory>
        <!-- 配置基本信息-->
            <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
            <property name="hibernate.show_sql">true</property>
            <property name="hibernate.format_sql">true</property>
            <property name="hibernate.hbm2ddl.auto">update</property>
    
    
            <!-- 二级缓存 -->
            
        </session-factory>
    </hibernate-configuration>
    复制代码

    struts.xml

    <!-- 只需要把class 配置为 在applicationContext-beans.xml 文件中Action所指向的id-->
    <action name="emp-*" class="employeeAction" method="{1}"> </action>
  • 相关阅读:
    浏览器操作本地缓存记录一下
    dotnet new Getting ready... Object reference not set to an instance of an object.
    IIS上vue打包后接口跨域解决
    SpringBoot前言
    Node聊天室和socket.io原理与功能总结
    Node加解密原理和功能探索总结
    Node中文件断点续传原理和方法总结
    Node短链原理与功能实现总结
    Node中F2A原理及功能实现总结
    Node图形邮箱手机验证码实现方法总结
  • 原文地址:https://www.cnblogs.com/Jansens520/p/6632774.html
Copyright © 2011-2022 走看看