美文网首页
Struts2 +Spring+ Hibernate整合实例

Struts2 +Spring+ Hibernate整合实例

作者: whoismy8023 | 来源:发表于2017-10-26 15:17 被阅读0次

    一、applicationContext.xml

    <?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:context="http://www.springframework.org/schema/context"
    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-2.5.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-2.5.xsd
                        http://www.springframework.org/schema/aop
                        http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
                       ">
    
    
    <context:property-placeholder location="classpath:jdbc.properties" />
    <!-- 添加一个类路径配置,让Spring 容器知道需要扫描该类路径下所有可以加载到容器中的类。 -->
    <context:component-scan base-package="gnnt" />
    <!-- 配置数据源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName">
            <value>${jdbc.driverClassName}</value>
        </property>
        <property name="url">
            <value>${jdbc.url}</value>
        </property>
        <property name="username">
            <value>${jdbc.username}</value>
        </property>
        <property name="password">
            <value>${jdbc.password}</value>
        </property>
    </bean>
    
    <!-- sessionFactory配置 -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation" value="classpath:hibernate.cfg.xml">
        </property>
        <property name="dataSource" ref="dataSource"></property>
        <property name="lobHandler">
        <ref bean="oracleLobHandler" />
        </property>
    </bean>
    <bean id="hibernateTemplete" class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    
    <!-- 处理大文本需要加入如下两个bean -->
    <bean id="nativeJdbcExtractor"
    class="org.springframework.jdbc.support.nativejdbc.SimpleNativeJdbcExtractor" />
    
    <bean id="oracleLobHandler" class="org.springframework.jdbc.support.lob.OracleLobHandler"
    lazy-init="true">
        <property name="nativeJdbcExtractor">
            <ref bean="nativeJdbcExtractor" />
        </property>
    </bean>
    
    <!-- 事务管理器配置, Hibernate单数据源事务 -->
    <bean id="defaultTransactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    
    <!-- 使用annotation定义事务 -->
    <tx:annotation-driven transaction-manager="defaultTransactionManager"
    proxy-target-class="true" />
    </beans>
    

    二、ehcache.xml

    <?xml version="1.0" encoding="GBK"?>
    <ehcache>
    <diskStore path="java.io.tmpdir" />
    <defaultCache maxElementsInMemory="10000"
                  eternal="false"
                  overflowToDisk="true"
                  timeToIdleSeconds="300"
                  timeToLiveSeconds="180"
                  diskPersistent="false"
                  diskExpiryThreadIntervalSeconds="120" />
    
     <cache name="queryCache"
            maxElementsInMemory="1000"
            eternal="false"
            timeToIdleSeconds="0"
            timeToLiveSeconds="5"
            overflowToDisk="false"
            />
     <cache name = "SimplePageCachingFilter"
               maxElementsInMemory = "10"
               maxElementsOnDisk = "10"
               eternal = "false"
               overflowToDisk = "true"
               diskSpoolBufferSizeMB = "20"
               timeToIdleSeconds = "120"
               timeToLiveSeconds = "120"
               memoryStoreEvictionPolicy = "LFU"
                />
    </ehcache>
    

    三、hibernate.cfg.xml

    <?xml version='1.0' encoding='GBK'?>
    <!DOCTYPE hibernate-configuration PUBLIC
              "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
              "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    
    <hibernate-configuration>
        <session-factory>
            <property name="dialect">
                org.hibernate.dialect.OracleDialect
            </property>
            <property name="show_sql">false</property>
            <property name="hbm2ddl.auto">none</property>
            <property name="format_sql">false</property>
            <property name="hibernate.cache.use_structured_entries">
            true
            </property><!-- 最优化二级缓存 -->
            <property name="hibernate.cache.use_second_level_cache">
            true
            </property>
            <!-- 完全禁用二级缓存开关,对那些在类的映射定义中指定cache的类,默认开启二级缓存 -->
            <property name="hibernate.cache.provider_class">
                org.hibernate.cache.EhCacheProvider
            </property>
            <property name="hibernate.cache.use_query_cache">true</property>
            <property name="hibernate.cache.provider_configuration_file_resource_path">
                /ehcache.xml
            </property>
            
            <mapping resource="gnnt/MEBS/online/model/MFirm.hbm.xml"/>
        </session-factory>
    </hibernate-configuration>
    

    四、struts.xml

    <?xml version="1.0" encoding="GBK" ?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
    
    <struts>
    <constant name="struts.i18n.encoding" value="GBK"></constant>
    <constant name="struts.objectFactory" value="spring"></constant>
    <constant name="struts.locale" value="zh_CN"></constant>
    <constant name="struts.multipart.maxSize" value="999999"></constant>
    <constant name="struts.multipart.parser" value="jakarta"></constant>
    <constant name="struts.ui.theme" value="simple" />
    <constant name="struts.ui.templateDir" value="template" />
    <constant name="struts.ui.templateSuffix" value="ftl" />
    <constant name="struts.devMode" value="true" />
    <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
    
    <package name="default" namespace="/" extends="struts-default">
    </package>  
    
    <!-- action配置 -->
    <package name="struts2-default" extends="default">
        <action name="testAction" class="testAction" method="test">
            <result name="success">/index.jsp</result>
        </action>
    </package>
    </struts>
    

    五、web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>Temp</display-name>
    <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <!-- spring字符集过滤器 -->
    <filter>
        <filter-name>SetCharacterEncoding</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>GBK</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
    </filter>
        <filter-mapping>
        <filter-name>SetCharacterEncoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    <!-- 初始化spring容器 -->
    <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>
    <listener>
        <listener-class>
            org.springframework.web.context.request.RequestContextListener
        </listener-class>
    </listener>
    <!-- 初始化spring容器 -->
    
    <!-- 为了解决 failed to lazily initialize a collection of role: XXXXXX, no session 
    or session was closed 异常添加的过滤器 -->
    <filter>
        <filter-name>hibernateFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
        <init-param>
            <param-name>sessionFactoryBeanName</param-name>
            <param-value>sessionFactory</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>hibernateFilter</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>*.action</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
    </filter-mapping>
    <!-- struts2配置 -->
    </web-app>
    

    相关文章

      网友评论

          本文标题:Struts2 +Spring+ Hibernate整合实例

          本文链接:https://www.haomeiwen.com/subject/gzrkpxtx.html