SSH:SSH(Struts1 + Spring + hibernate)整合之配置文件

Web.xml代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <web-app version="2.4"    
  3.     xmlns="http://java.sun.com/xml/ns/j2ee"    
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee    
  6.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">   
  7.   
  8.  <!-- 指定spring的配置文件,默认从web根目录寻找配置文件,我们可以通过spring提供的classpath:前缀指定从类路径下寻找 -->   
  9.     <context-param>   
  10.        <param-name>contextConfigLocation</param-name>   
  11.        <param-value>classpath:beans.xml</param-value>   
  12.     </context-param>   
  13.     
  14.     <!-- 对Spring容器进行实例化,并把实例存放在application的属性里 -->   
  15.     <listener>   
  16.           <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>   
  17.     </listener>   
  18.   
  19.     <!-- 配置字符编码过滤器(解决乱码问题) -->    
  20.     <filter>   
  21.     <filter-name>encoding</filter-name>   
  22.     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>   
  23.     <init-param>   
  24.         <param-name>encoding</param-name>   
  25.         <param-value>UTF-8</param-value>   
  26.     </init-param>   
  27.     </filter>   
  28.     <filter-mapping>   
  29.         <filter-name>encoding</filter-name>   
  30.         <url-pattern>/*</url-pattern>   
  31.     </filter-mapping>    
  32.        
  33.     <!-- 解决因session关闭而导致的延迟加载例外的问题 -->   
  34.     <filter>   
  35.             <filter-name>OpenSessionInViewFilter</filter-name>   
  36.             <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>   
  37.     </filter>   
  38.     <filter-mapping>   
  39.             <filter-name>OpenSessionInViewFilter</filter-name>   
  40.             <url-pattern>/*</url-pattern>   
  41.     </filter-mapping>   
  42.   
  43.     <!-- 配置Struts1的中心控制器ActionServlet-->   
  44.   <servlet>   
  45.     <servlet-name>struts</servlet-name>   
  46.     <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>   
  47.     <init-param>   
  48.         <param-name>config</param-name>   
  49.         <param-value>/WEB-INF/struts-config.xml</param-value>   
  50.     </init-param>   
  51.     <load-on-startup>0</load-on-startup>   
  52.    </servlet>   
  53.    <servlet-mapping>   
  54.         <servlet-name>struts</servlet-name>   
  55.         <url-pattern>*.do</url-pattern>   
  56.    </servlet-mapping>   
  57.      
  58.      
  59.   <welcome-file-list>   
  60.     <welcome-file>index.jsp</welcome-file>   
  61.   </welcome-file-list>   
  62. </web-app>  

 

 

2.struts-config.xml

 

Struts-config.xml代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <!DOCTYPE struts-config PUBLIC   
  3.           "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"  
  4.           "http://struts.apache.org/dtds/struts-config_1_3.dtd">   
  5. <struts-config>   
  6.   
  7.     <!-- 配置formbean -->   
  8.     <form-beans>   
  9.         <form-bean name="personForm" type="cn.itcast.formbean.PersonForm"/>   
  10.     </form-beans>   
  11.   
  12.     <!-- 配置全局跳转 -->   
  13.     <global-forwards>   
  14.         <forward name="message" path="/WEB-INF/page/message.jsp"/>   
  15.     </global-forwards>   
  16.   
  17.     <!-- Action映射配置,这里Action交给Spring去管理 -->   
  18.     <action-mappings>   
  19.         <action path="/personlist" validate="false">   
  20.             <forward name="list" path="/WEB-INF/page/persons.jsp"/>   
  21.         </action>   
  22.         <action path="/personmanage" name="personForm" scope="request" validate="false" parameter="method">   
  23.             <forward name="add" path="/WEB-INF/page/addperson.jsp"/>   
  24.         </action>   
  25.     </action-mappings>   
  26.        
  27.     <!-- 将请求交给Spring容器处理 -->   
  28.     <controller>   
  29.      <set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor"/>   
  30.     </controller>    
  31.   
  32. </struts-config>  

 

 

3.beans.xml配置如下:

 

Beans.xml代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:context="http://www.springframework.org/schema/context"  
  5.        xmlns:aop="http://www.springframework.org/schema/aop"  
  6.        xmlns:tx="http://www.springframework.org/schema/tx"  
  7.        xsi:schemaLocation="http://www.springframework.org/schema/beans   
  8.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
  9.            http://www.springframework.org/schema/context   
  10.            http://www.springframework.org/schema/context/spring-context-2.5.xsd   
  11.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
  12.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">   
  13.        
  14.         <context:annotation-config/>    
  15.     <!-- 配置数据源 -->   
  16.         <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">   
  17.         <property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>   
  18.         <property name="url" value="jdbc:mysql://localhost:3306/xwuxintwo?useUnicode=true&amp;characterEncoding=UTF-8"/>   
  19.         <property name="username" value="root"/>   
  20.         <property name="password" value="123456"/>   
  21.          <!-- 连接池启动时的初始值 -->   
  22.          <property name="initialSize" value="1"/>   
  23.          <!-- 连接池的最大值 -->   
  24.          <property name="maxActive" value="500"/>   
  25.          <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接释放一部分,一直减少到maxIdle为止 -->   
  26.          <property name="maxIdle" value="2"/>   
  27.          <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请一些连接,以避免洪峰来时再申请而造成的性能开销 -->   
  28.          <property name="minIdle" value="1"/>   
  29.     </bean>   
  30.        
  31.     <!-- 使用Spring提供的sessionFactory 指定它要使用的数据源以及数据库相关信息 -->   
  32.     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">   
  33.          <property name="dataSource" ref="dataSource"/>   
  34.          <property name="mappingResources">   
  35.             <list>   
  36.               <value>cn/xwuxin/bean/Person.hbm.xml</value>   
  37.             </list>   
  38.          </property>   
  39.          <property name="hibernateProperties">   
  40.             <value>   
  41.             <!-- 方言 -->   
  42.                 hibernate.dialect=org.hibernate.dialect.MySQL5Dialect   
  43.   
  44.             <!-- 这里第一次使用时可以把update改为create让其自动生成表-->   
  45.                 hibernate.hbm2ddl.auto=update   
  46.   
  47.             <!-- 配置是否显示sql语句 -->   
  48.                 hibernate.show_sql=false   
  49.                
  50.             <!-- 是否格式化sql语句 -->   
  51.                 hibernate.format_sql=false   
  52.   
  53.             <!-- 是否使用二级缓存 -->   
  54.                 hibernate.cache.use_second_level_cache=true   
  55.   
  56.             <!-- 是否使用查询缓存 -->   
  57.                     hibernate.cache.use_query_cache=false   
  58.   
  59.             <!-- 设置二级缓存的提供者 (OSCache或EHCache)-->   
  60.                 hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider   
  61.               </value>   
  62.         </property>   
  63.     </bean>   
  64.   
  65.     <!-- 配置事务管理器指定其作用的sessionFactory 这样就把事务交给Spring去处理,减轻程序员负担-->   
  66.     <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">   
  67.         <property name="sessionFactory" ref="sessionFactory"/>   
  68.     </bean>   
  69.        
  70.     <!-- aop的配置 -->   
  71.     <aop:config>   
  72.         <aop:pointcut id="tran" expression="execution(* cn.xwuxin.service..*.*(..))"/>   
  73.         <aop:advisor advice-ref="txAdvice" pointcut-ref="tran"/>   
  74.     </aop:config>   
  75.     <tx:advice id="txAdvice" transaction-manager="txManager">   
  76.           <tx:attributes>   
  77.             <tx:method name="get*" read-only="true" propagation="NOT_SUPPORTED"/>   
  78.             <tx:method name="*"/>   
  79.           </tx:attributes>   
  80.     </tx:advice>   
  81.        
  82.     <!-- 其他要交给Spring管理的bean -->   
  83.     <bean id="personService" class="cn.xwuxin.service.impl.PersonServiceBean"/>   
  84.     <bean name="/personlist" class="cn.xwuxin.action.PersonListAction"/>   
  85.     <bean name="/personmanage" class="cn.xwuxin.action.PersonManageAction"/>   
  86.   
  87. </beans>  

 

 

4.ehcache.xml配置如下:

 

Ehcache.xml代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <!--    
  3.     defaultCache节点为缺省的缓存策略   
  4.      maxElementsInMemory 内存中最大允许存在的对象数量   
  5.      eternal 设置缓存中的对象是否永远不过期   
  6.      overflowToDisk 把溢出的对象存放到硬盘上   
  7.      timeToIdleSeconds 指定缓存对象空闲多长时间就过期,过期的对象会被清除掉   
  8.      timeToLiveSeconds 指定缓存对象总的存活时间   
  9.      diskPersistent 当jvm结束是是否持久化对象   
  10.      diskExpiryThreadIntervalSeconds 指定专门用于清除过期对象的监听线程的轮询时间   
  11.  -->   
  12. <ehcache>   
  13.     <diskStore path="D:/cache"/>   
  14.     <defaultCache maxElementsInMemory="1000" eternal="false" overflowToDisk="true"  
  15.         timeToIdleSeconds="120"  
  16.         timeToLiveSeconds="180"  
  17.         diskPersistent="false"  
  18.         diskExpiryThreadIntervalSeconds="60"/>   
  19.            
  20.     <cache name="cn.itcast.bean.Person" maxElementsInMemory="100" eternal="false"  
  21.     overflowToDisk="true" timeToIdleSeconds="300" timeToLiveSeconds="600" diskPersistent="false"/>   
  22. </ehcache>  

 

 

5.Person.hbm.xml配置如下:

 

Person.hbm.xml代码 复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <!DOCTYPE hibernate-mapping PUBLIC   
  3.         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4.         "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">   
  5. <hibernate-mapping package="cn.itcast.bean">   
  6.     <class name="Person" table="person">   
  7.         <cache usage="read-write" region="cn.itcast.bean.Person"/>   
  8.         <id name="id">   
  9.             <generator class="native"/>   
  10.         </id>   
  11.         <property name="name" length="10" not-null="true"/>   
  12.     </class>   
  13. </hibernate-mapping>  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值