zoukankan      html  css  js  c++  java
  • springMVC+Hibernate配置

    本文描述下 sypro 项目中使用 springMVC+Hibernate配置,初学SpringMVC做下简单整理解。

    1.web项目首先我们要使用 web.xml文件将 spring配置引入进来

    Xml代码  收藏代码
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"  
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
    5.       
    6.     <description>springMvc+hibernate4+easyui</description>  
    7.       
    8.     <display-name>sypro2.02</display-name>  
    9.     <context-param>  
    10.         <param-name>contextConfigLocation</param-name>  
    11.         <param-value>classpath:spring.xml,classpath:spring-hibernate.xml,classpath:spring-ehcache.xml,classpath:spring-apacheFtpServer.xml</param-value>  
    12.     </context-param>  
    13.       
    14.     <filter>  
    15.         <filter-name>openSessionInViewFilter</filter-name>  
    16.         <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>  
    17.         <init-param>  
    18.             <param-name>singleSession</param-name>  
    19.             <param-value>true</param-value>  
    20.         </init-param>  
    21.     </filter>  
    22.       
    23.       
    24.     <filter>  
    25.         <description>字符集过滤器</description>  
    26.         <filter-name>encodingFilter</filter-name>  
    27.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
    28.         <init-param>  
    29.             <description>字符集编码</description>  
    30.             <param-name>encoding</param-name>  
    31.             <param-value>UTF-8</param-value>  
    32.         </init-param>  
    33.     </filter>  
    34.       
    35.     <filter-mapping>  
    36.         <filter-name>openSessionInViewFilter</filter-name>  
    37.         <url-pattern>*.do</url-pattern>  
    38.     </filter-mapping>  
    39.       
    40.     <filter-mapping>  
    41.         <filter-name>encodingFilter</filter-name>  
    42.         <url-pattern>/*</url-pattern>  
    43.     </filter-mapping>  
    44.       
    45.     <listener>  
    46.         <description>spring监听器</description>  
    47.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    48.     </listener>  
    49.       
    50.     <listener>  
    51.         <description>apache ftp server监听器</description>  
    52.         <listener-class>sy.util.ApacheFtpServerListener</listener-class>  
    53.     </listener>  
    54.       
    55.     <listener>  
    56.         <description>Introspector缓存清除监听器</description>  
    57.         <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>  
    58.     </listener>  
    59.       
    60.     <servlet>  
    61.         <description>spring mvc servlet</description>  
    62.         <servlet-name>springMvc</servlet-name>  
    63.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    64.         <init-param>  
    65.             <description>spring mvc 配置文件</description>  
    66.             <param-name>contextConfigLocation</param-name>  
    67.             <param-value>classpath:spring-mvc.xml</param-value>  
    68.         </init-param>  
    69.         <load-on-startup>1</load-on-startup>  
    70.     </servlet>  
    71.       
    72.     <servlet-mapping>  
    73.         <servlet-name>springMvc</servlet-name>  
    74.         <url-pattern>*.do</url-pattern>  
    75.     </servlet-mapping>  
    76.       
    77.       
    78.     <session-config>  
    79.         <session-timeout>3</session-timeout>  
    80.     </session-config>  
    81.       
    82.     <welcome-file-list>  
    83.         <welcome-file>index.jsp</welcome-file>  
    84.         <welcome-file>init.jsp</welcome-file>  
    85.     </welcome-file-list>  
    86.       
    87.     <error-page>  
    88.         <error-code>404</error-code>  
    89.         <location>/error/404.jsp</location>  
    90.     </error-page>  
    91.       
    92.     <error-page>  
    93.         <error-code>500</error-code>  
    94.         <location>/error/500.jsp</location>  
    95.     </error-page>  
    96.       
    97.     <distributable />  
    98.       
    99. </web-app>  



    部署applicationContext的xml文件,如果在web.xml中不写任何参数配置信息,默认的路径是”/WEB-INF /applicationContext.xml,在WEB-INF目录下创建的xml文件的名称必须是applicationContext.xml。 如果是要自定义文件名可以在web.xml里加入contextConfigLocation如下,在<param-value> </param-value>里指定相应的xml文件名,如果有多个xml文件,可以写在一起并一“,”号分隔

    Xml代码  收藏代码
    1. <display-name>sypro2.02</display-name>  
    2.     <context-param>  
    3.         <param-name>contextConfigLocation</param-name>  
    4.         <param-value>classpath:spring.xml,classpath:spring-hibernate.xml,classpath:spring-ehcache.xml,classpath:spring-apacheFtpServer.xml</param-value>  
    5.     </context-param>  
    6.       
    7.     <filter>  
    8.         <filter-name>openSessionInViewFilter</filter-name>  
    9.         <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>  
    10.         <init-param>  
    11.             <param-name>singleSession</param-name>  
    12.             <param-value>true</param-value>  
    13.         </init-param>  
    14.     </filter>  


    加载contextConfigLocation则交给监听来实现

    Xml代码  收藏代码
    1. <listener>  
    2.         <description>spring监听器</description>  
    3.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    4.     </listener>  



    然后配置springMVC处理请求映射

    Xml代码  收藏代码
    1. <servlet>  
    2.         <description>spring mvc servlet</description>  
    3.         <servlet-name>springMvc</servlet-name>  
    4.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    5.         <init-param>  
    6.             <description>spring mvc 配置文件</description>  
    7.             <param-name>contextConfigLocation</param-name>  
    8.             <param-value>classpath:spring-mvc.xml</param-value>  
    9.         </init-param>  
    10.         <load-on-startup>1</load-on-startup>  
    11.     </servlet>  
    12.       
    13.     <servlet-mapping>  
    14.         <servlet-name>springMvc</servlet-name>  
    15.         <url-pattern>*.do</url-pattern>  
    16.     </servlet-mapping>  



    2.applicationContext.xml 文件常用配置,在该项目中使用的自定义写法 将 applicationContext.xml 重命名为: spring-mvc.xml ,注意如果不使用自定义写法applicationContext.xml则存放在 /WEB-INF/applicationContext.xml目录且不可重命名

    applicationContext.xml (spring-mvc.xml) 常用配置如下:

    Xml代码  收藏代码
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <beans xmlns="http://www.springframework.org/schema/beans"  
    3.     xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    4.     xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"  
    5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
    6.       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
    7.        http://www.springframework.org/schema/context  
    8.         http://www.springframework.org/schema/context/spring-context-3.0.xsd  
    9.          http://www.springframework.org/schema/mvc  
    10.         http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">  
    11.   
    12.     <!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 -->  
    13.     <context:component-scan base-package="sy.controller" />  
    14.   
    15.     <!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->  
    16.     <bean id="mappingJacksonHttpMessageConverter"  
    17.         class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">  
    18.         <property name="supportedMediaTypes">  
    19.             <list>  
    20.                 <value>text/html;charset=UTF-8</value>  
    21.             </list>  
    22.         </property>  
    23.     </bean>  
    24.   
    25.     <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->  
    26.     <bean  
    27.         class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
    28.         <property name="messageConverters">  
    29.             <list>  
    30.                 <ref bean="mappingJacksonHttpMessageConverter" /><!-- json转换器 -->  
    31.             </list>  
    32.         </property>  
    33.     </bean>  
    34.   
    35.     <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->  
    36.     <bean  
    37.         class="org.springframework.web.servlet.view.InternalResourceViewResolver"  
    38.         p:prefix="/" p:suffix=".jsp" />  
    39.   
    40.     <bean id="multipartResolver"  
    41.         class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
    42.         <property name="defaultEncoding">  
    43.             <value>UTF-8</value>  
    44.         </property>  
    45.         <property name="maxUploadSize">  
    46.             <value>32505856</value><!-- 上传文件大小限制为31M,31*1024*1024 -->  
    47.         </property>  
    48.         <property name="maxInMemorySize">  
    49.             <value>4096</value>  
    50.         </property>  
    51.     </bean>  
    52.   
    53.     <!-- 拦截器 -->  
    54.     <mvc:interceptors>  
    55.         <mvc:interceptor>  
    56.             <mvc:mapping path="/**" />  
    57.             <bean class="sy.interceptors.EncodingInterceptor" />  
    58.         </mvc:interceptor>  
    59.         <mvc:interceptor>  
    60.             <mvc:mapping path="/**" />  
    61.             <bean class="sy.interceptors.AuthInterceptor" />  
    62.         </mvc:interceptor>  
    63.     </mvc:interceptors>  
    64.   
    65. </beans>  



    spring-xml:

    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" xmlns:context="http://www.springframework.org/schema/context"  
    4.     xsi:schemaLocation="  
    5. http://www.springframework.org/schema/beans  
    6. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
    7. http://www.springframework.org/schema/context  
    8. http://www.springframework.org/schema/context/spring-context-3.0.xsd  
    9. ">  
    10.   
    11.     <!-- 引入属性文件 -->  
    12.     <context:property-placeholder location="classpath:config.properties" />  
    13.   
    14.     <!-- 自动扫描dao和service包(自动注入) -->  
    15.     <context:component-scan base-package="sy.dao,sy.service" />  
    16.   
    17. </beans>  


    Xml代码  收藏代码
    1. <!-- 引入属性文件 -->  
    2.     <context:property-placeholder location="classpath:config.properties" />  


    项目配置映入属性文件在配置hibenrate数据库配置提供了便利
    如下 spring-hibernate.xml 配置

    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" xmlns:tx="http://www.springframework.org/schema/tx"  
    4.     xsi:schemaLocation="  
    5. http://www.springframework.org/schema/beans  
    6. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
    7. http://www.springframework.org/schema/tx  
    8. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
    9. ">  
    10.   
    11.     <!-- JNDI方式配置数据源 -->  
    12.     <!-- <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">   
    13.         <property name="jndiName" value="${jndiName}"></property</bean> -->  
    14.   
    15.     <!-- dbcp数据源 -->  
    16.     <bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource">  
    17.         <property name="driverClassName" value="${driverClassName}"></property>  
    18.         <property name="url" value="${url}"></property>  
    19.         <property name="username" value="${username}"></property>  
    20.         <property name="password" value="${password}"></property>  
    21.     </bean>  
    22.   
    23.     <bean id="sessionFactory"  
    24.         class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
    25.         <property name="dataSource" ref="dataSource" />  
    26.         <property name="hibernateProperties">  
    27.             <props>  
    28.                 <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>  
    29.                 <prop key="hibernate.dialect">${hibernate.dialect}</prop>  
    30.                 <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>  
    31.                 <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>  
    32.             </props>  
    33.         </property>  
    34.   
    35.         <!-- 注解方式配置 -->  
    36.         <!-- <property name="packagesToScan"<list<value>sy.hbm</value</list>   
    37.             </property> -->  
    38.   
    39.         <!-- hbm方式配置 -->  
    40.         <property name="mappingDirectoryLocations">  
    41.             <list>  
    42.                 <value>classpath:sy/hbm</value>  
    43.             </list>  
    44.         </property>  
    45.     </bean>  
    46.   
    47.     <!-- 配置事务 -->  
    48.     <bean name="transactionManager"  
    49.         class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
    50.         <property name="sessionFactory" ref="sessionFactory"></property>  
    51.     </bean>  
    52.     <tx:annotation-driven transaction-manager="transactionManager" />  
    53.   
    54. </beans>  




    spring缓存配置

    Xml代码  收藏代码
      1. <!-- 开启spring缓存 -->  
      2.     <cache:annotation-driven cache-manager="cacheManager" />  
      3.     <bean id="cacheManagerFactory"  
      4.         class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"  
      5.         p:configLocation="classpath:ehcache.xml" p:shared="false" />  
      6.     <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"  
      7.         p:cacheManager-ref="cacheManagerFactory" />  
  • 相关阅读:
    js json 与字符串 转换过程由于书写不统一规范引发的一个问题
    asp.net 运行时,"未能映射路径"
    android html 图片处理类--加载富文本工具类
    System.Web.HttpCompileException (0x80004005): (0): error CS0016: 未能写入输出文件
    “请运行Chkdsk工具”怎么解决
    android studio新项目时提示:Plugin is too old, please update to a more recent version
    mvvm和mvc区别?
    什么是mvvm?
    js去除html标签
    数组的翻转(非reverse)
  • 原文地址:https://www.cnblogs.com/xuqiudong/p/3992179.html
Copyright © 2011-2022 走看看