zoukankan      html  css  js  c++  java
  • SpringMVCHibernate配置

    Spring MVC的配置

    包含hibernate、log4j、logback的基本配置

    web.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app version="2.5" 
     3 xmlns="http://java.sun.com/xml/ns/javaee" 
     4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
     6     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
     7   <display-name>Test</display-name> 
     8       <context-param>
     9         <param-name>contextConfigLocation</param-name>
    10         <param-value>/WEB-INF/applicationContext.xml</param-value>
    11     </context-param>
    12     <listener>
    13         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    14     </listener>
    15     
    16     <servlet>
    17         <servlet-name>Management Center of Cloud Development Support Platform</servlet-name>
    18         <servlet-class>com.test.OwnDispatcherServlet</servlet-class>
    19         <init-param>
    20             <param-name>contextConfigLocation</param-name>
    21             <param-value>/WEB-INF/springMVC.xml</param-value>
    22         </init-param>
    23         <load-on-startup>1</load-on-startup>
    24     </servlet>
    25     <servlet-mapping>
    26         <servlet-name>Spring Test</servlet-name>
    27         <url-pattern>/mc/*</url-pattern>
    28     </servlet-mapping>
    29   
    30   
    31     <welcome-file-list>
    32         <welcome-file>login.html</welcome-file>
    33     </welcome-file-list>
    34  
    35 
    36  <filter>
    37   <filter-name>Set Character Encoding</filter-name>
    38   <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    39   <init-param>
    40   <param-name>encoding</param-name>
    41   <param-value>utf8</param-value>
    42   </init-param>
    43  </filter>
    44   <filter-mapping>
    45   <filter-name>Set Character Encoding</filter-name>
    46   <url-pattern>/*</url-pattern>
    47   </filter-mapping>
    50     <context-param>  
    51         <param-name>log4jConfigLocation</param-name>  
    52         <param-value>WEB-INF/log4j.properties</param-value>  
    53     </context-param>   
    55     <context-param>  
    56         <param-name>log4jRefreshInterval</param-name>  
    57         <param-value>600000</param-value>  
    58     </context-param>  
    59     <listener>  
    60         <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>  
    61     </listener>
    62     <context-param>  
    63         <param-name>webAppRootKey</param-name>  
    64         <param-value>myWebApp.root</param-value>  
    65     </context-param>
    66     
    67       
    68     <context-param>
    69          <param-name>logbackConfigLocation</param-name>
    70          <param-value>WEB-INF/logback.xml</param-value>
    71      </context-param>
    72      <listener>
    73          <listener-class>ch.qos.logback.ext.spring.web.LogbackConfigListener</listener-class>
    74      </listener>
    75      
    76       
    77    </web-app>
    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:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
           http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
        <context:annotation-config />
        <context:component-scan base-package="com.test.pojo" />
        <context:component-scan base-package="com.test.dao" />
        <context:component-scan base-package="com.test.service" />
        <context:component-scan base-package="com.test.pub" />
        <context:property-placeholder location="classpath:db.properties" />
        <!-- 配置数据源 -->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
            destroy-method="close">
            <property name="driverClass" value="${jdbc.driver}" />
            <property name="jdbcUrl" value="${jdbc.url}" />
            <property name="user" value="${jdbc.username}" />
            <property name="password" value="${jdbc.password}" />
            <!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
            <property name="initialPoolSize" value="${jdbc.initialSize}" />
            <!--连接池中保留的最小连接数。 -->
            <property name="minPoolSize" value="${jdbc.minIdle}" />
            <!--连接池中保留的最大连接数。Default: 15 -->
            <property name="maxPoolSize" value="${jdbc.maxActive}" />
            <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
            <property name="maxIdleTime" value="${jdbc.maxIdleTime}" />
            <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
            <property name="acquireIncrement" value="5" />
            <!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
            <property name="idleConnectionTestPeriod" value="60" />
        </bean>
    
        <!-- 配置Spring的SessionFactory -->
        <bean id="sessionFactory"
            class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <!-- 配置自动扫描包下的实体,也可使用annotatedClasses属性进行单个实体配置 -->
            <property name="packagesToScan" value="com.test.pojo"></property>
            <property name="hibernateProperties">
                <value>
                    hibernate.dialect=${hibernate.dialect}
                    hibernate.show_sql=${hibernate.show_sql}
                    hibernate.format_sql=${hibernate.format_sql}
                    hibernate.hbm2ddl.auto=${hibernate.hbm2ddl.auto}
                    javax.persistence.validation.mode=${javax.persistence.validation.mode}
                    hibernate.cache.use_second_level_cache=${hibernate.cache.use_second_level_cache}
                    hibernate.cache.use_query_cache=${hibernate.cache.use_query_cache}
                    <!-- 高速缓存提供程序 -->
                    hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
    
                </value>
            </property>
    
        </bean>
        <!-- 配置事务管理器 -->
        <bean id="txManager"
            class="org.springframework.orm.hibernate4.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory" />
        </bean>
        <tx:advice id="txAdvice" transaction-manager="txManager">
            <tx:attributes>
                <tx:method name="add*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
                <tx:method name="save*" propagation="REQUIRED"
                    rollback-for="java.lang.Exception" />
                <tx:method name="update*" propagation="REQUIRED"
                    rollback-for="java.lang.Exception" />
                <tx:method name="del*" propagation="REQUIRED" rollback-for="java.lang.Exception" />
                <tx:method name="get*" read-only="true" />
                <tx:method name="login*" read-only="true" />
                <tx:method name="find*" read-only="true" />
                <tx:method name="*" read-only="true" />
            </tx:attributes>
        </tx:advice>
        <aop:config>
            <aop:pointcut id="fooServiceOperation"
                expression="execution(* com.test.dao.*.*(..))" />
            <aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceOperation" />
        </aop:config>
    </beans>
           
    springMVC.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:mvc="http://www.springframework.org/schema/mvc"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.2.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
    
        <context:annotation-config />
        <context:component-scan base-package="com.test.web"/>
        <mvc:annotation-driven />
    
    <bean
      class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
      <property name="messageConverters">
       <list>
        <bean
         class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
         <property name="supportedMediaTypes">
          <list>
           <!--返回字符串格式json-->
           <value>application/json;charset=UTF-8</value>
          </list>
         </property>
        </bean>
       </list>
      </property>
     </bean>
     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
        <property name="defaultEncoding" value="utf-8"></property>   
        <property name="maxUploadSize" value="31457280000"></property>  
        <property name="maxInMemorySize" value="1024"></property>  
      </bean>
    
      <!-- 对静态资源文件的访问  方案一 (二选一) -->
         <mvc:default-servlet-handler/>
    
    </beans>

    db.properties

    #driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
    #url=jdbc:sqlserver://localhost:1432;databaseName=
    #username=sa
    #password=sql2005
    jdbc.driver=com.mysql.jdbc.Driver
    #jdbc:mysql://localhost:3306/mookSpace?createDatabaseIfNotExist=true
    jdbc.url=jdbc:mysql://数据库IP地址:3306/数据库名?createDatabaseIfNotExist=true&amp;autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=utf-8
    jdbc.username=test
    jdbc.password=test123
    jdbc.initialSize=5
    jdbc.minIdle=20
    jdbc.maxActive=300jdbc.maxIdleTime=60
    hibernate.dialect=org.hibernate.dialect.MySQLDialect
    hibernate.show_sql=ture
    hibernate.format_sql=true
    hibernate.hbm2ddl.auto=update
    javax.persistence.validation.mode=none
    hibernate.cache.use_second_level_cache=false
    hibernate.cache.use_query_cache=true
  • 相关阅读:
    Windows8 Metro界面下的StreamSocket连接服务器
    使用CSS控制文字溢出
    Javascript中的恒等运算符与等于运算符的区别
    Sqlserver 中删除表数据的两种方式与区别
    Linux C++ 遇到的错误
    四、Vue Router 设置动态路由
    六、Vue Router 嵌套路由
    一、Vue Router 的使用
    三、Vuex Getter
    五、Vuex Action
  • 原文地址:https://www.cnblogs.com/yiyitong/p/5421221.html
Copyright © 2011-2022 走看看