springMVC+hibernate配置

hibernate由spring管理,配置文件如下:

applicationContext.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:aop="http://www.springframework.org/schema/aop"  
  5.        xmlns:tx="http://www.springframework.org/schema/tx"  
  6.        xmlns:context="http://www.springframework.org/schema/context"  
  7.        xsi:schemaLocation="http://www.springframework.org/schema/beans   
  8.        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  9.        http://www.springframework.org/schema/aop   
  10.        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
  11.        http://www.springframework.org/schema/tx   
  12.        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
  13.        http://www.springframework.org/schema/context    
  14.        http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
  15.     <!-- 配置扫瞄注解service,controller -->  
  16.     <context:annotation-config/>  
  17.     <context:component-scan base-package="扫瞄的包名" scoped-proxy="targetClass">  
  18. <!--         <context:exclude-filter type="annotation" expression="cn.bonoon.controllers.*"/> -->  
  19.     </context:component-scan>  
  20.   
  21.     <!-- 配置数据库连接 -->  
  22.     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
  23.         <property name="driverClassName" value="com.mysql.jdbc.Driver"/>  
  24.         <property name="url" value="jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=utf8"/>  
  25.         <property name="username" value="root"/>  
  26.         <property name="password" value=""/>  
  27.     </bean>  
  28.     <!-- 配置hibernate相关信息 -->  
  29.     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">  
  30.         <property name="dataSource" ref="dataSource"/>  
  31.         <property name="hibernateProperties">  
  32.             <props>  
  33.                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>  
  34.                 <prop key="hibernate.show_sql">true</prop>  
  35.                 <prop key="hibernate.hbm2ddl.auto">update</prop>  
  36.             </props>  
  37.         </property>  
  38.           
  39.             <!-- 以下列表写入实体类 -->  
  40.         <property name="annotatedClasses">  
  41.             <list>中间写入hibernate注解的实体</list>  
  42.         </property>  
  43.     </bean>  
  44.     <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  45.         <property name="sessionFactory" ref="sessionFactory"/>  
  46.     </bean>  
  47.     <!-- 使用annotation定义事务 -->  
  48.     <tx:annotation-driven transaction-manager="transactionManager" />  
  49. </beans>  

<?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/aop http://www.springframework.org/schema/aop/spring-aop-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"> <!-- 配置扫瞄注解service,controller --> <context:annotation-config/> <context:component-scan base-package="扫瞄的包名" scoped-proxy="targetClass"> <!-- <context:exclude-filter type="annotation" expression="cn.bonoon.controllers.*"/> --> </context:component-scan> <!-- 配置数据库连接 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=utf8"/> <property name="username" value="root"/> <property name="password" value=""/> </bean> <!-- 配置hibernate相关信息 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactory<wbr>Bean"> <property name="dataSource" ref="dataSource"/> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <!-- 以下列表写入实体类 --> <property name="annotatedClasses"> <list>中间写入hibernate注解的实体</list> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionMana<wbr>ger"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!-- 使用annotation定义事务 --> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>

有了applicationContext再在web.xml在配置一下便可同时使用spring和hibernate了,但是要使用spring mvc ,还得在web里做一点手脚,具体如下:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <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" version="2.5">  
  3. <!-- 上下文配置文件 -->  
  4.   <context-param>  
  5.     <description>spring config</description>  
  6.     <param-name>contextConfigLocation</param-name>  
  7.     <param-value>/WEB-INF/applicationContext.xml</param-value>  
  8.   </context-param>  
  9.   <listener>  
  10.     <description>spring listerner</description>  
  11.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  12.   </listener>  
  13.     
  14. <!--   配置spring mvc的相关内容,此处的servlet-name任意,但必须有<your servlet-name>-servlet.xml与之对应 -->  
  15.   <servlet>  
  16.     <servlet-name>springmvc</servlet-name>  
  17.     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  18.     <load-on-startup>2</load-on-startup>  
  19.   </servlet>  
  20.   <servlet-mapping>  
  21.     <servlet-name>springmvc</servlet-name>  
  22. <!--     配置对应以.do结尾的请求 -->  
  23.     <url-pattern>*.do</url-pattern>    
  24.   </servlet-mapping>  
  25.   
  26.   
  27. <!-- 配置过滤器,同时把所有的请求都转为utf-8编码 -->  
  28.     <filter>  
  29.         <filter-name>Spring character encoding filter</filter-name>  
  30.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  31.         <init-param>  
  32.             <param-name>encoding</param-name>  
  33.             <param-value>utf-8</param-value>  
  34.         </init-param>  
  35.     </filter>  
  36.     <filter-mapping>  
  37.         <filter-name>Spring character encoding filter</filter-name>  
  38.         <url-pattern>/*</url-pattern>  
  39.     </filter-mapping>  
  40.     
  41. <!--     配置找不到页面时返回的页面 -->  
  42.   <error-page>   
  43.       <error-code>404</error-code>   
  44.       <location>/error/404.html</location>   
  45.   </error-page>   
  46.     
  47. <!--   配置项目主页 -->  
  48.   <session-config>  
  49.     <session-timeout>30</session-timeout>  
  50.   </session-config>  
  51.   <welcome-file-list>  
  52.     <welcome-file>login.jsp</welcome-file>  
  53.   </welcome-file-list>  
  54. </web-app>  

 <?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" version="2.5"> <!-- 上下文配置文件 --> <context-param> <description>spring config</description> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <description>spring listerner</description> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置spring mvc的相关内容,此处的servlet-name任意,但必须有<your servlet-name>-servlet.xml与之对应 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!-- 配置对应以.do结尾的请求 --> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- 配置过滤器,同时把所有的请求都转为utf-8编码 --> <filter> <filter-name>Spring character encoding filter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>Spring character encoding filter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置找不到页面时返回的页面 --> <error-page> <error-code>404</error-code> <location>/error/404.html</location> </error-page> <!-- 配置项目主页 --> <session-config> <session-timeout>30</session-timeout> </session-config> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> </web-app>


如上面配置文件的注释所说,由于配置了spring mvc ,还得有相关的文件,以上面的配置对应的文件名为:

springmvc-servlet.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:p="http://www.springframework.org/schema/p"  
  4.     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-2.5.xsd  
  7.     http://www.springframework.org/schema/context  
  8.     http://www.springframework.org/schema/context/spring-context-2.5.xsd">  
  9.   
  10.   
  11.     <!-- 对定义包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->  
  12.     <context:component-scan base-package="cn.bonoon" />  
  13.   
  14.     <!-- 启动spring mvc的注解功能,完成请求和注解POJO的映射 -->  
  15.     <bean  
  16.         class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
  17. <!--         配置信息转换,将用@responsebody注解的返回值转换为json返回前台,编码为utf-8-->  
  18.         <property name="messageConverters">  
  19.             <list>  
  20.                 <bean  
  21.                     class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">  
  22.                     <property name="supportedMediaTypes">  
  23.                         <list>  
  24.                             <value>application/json;charset=UTF-8</value>  
  25.                         </list>  
  26.                     </property>  
  27.                 </bean>  
  28.             </list>  
  29.         </property>  
  30.     </bean>  
  31.   
  32.   
  33. <!-- 上传文件时需要用到的分解器,默认将编码转为utf-8 -->  
  34.     <bean id="multipartResolver"    
  35.         class="org.springframework.web.multipart.commons.CommonsMultipartResolver">    
  36.         <property name="defaultEncoding">  
  37.             <value>UTF-8</value>  
  38.         </property>  
  39.     </bean>    
  40.   
  41.     <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->  
  42.     <bean  
  43.         class="org.springframework.web.servlet.view.InternalResourceViewResolver"  
  44.         p:prefix="" p:suffix=".jsp" />  
  45.   
  46.   
  47. </beans>  

<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" 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"> <!-- 对定义包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 --> <context:component-scan base-package="cn.bonoon" /> <!-- 启动spring mvc的注解功能,完成请求和注解POJO的映射 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerA<wbr>dapter"> <!-- 配置信息转换,将用@responsebody注解的返回值转换为json返回前台,编码为utf-8--> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessag<wbr>eConverter"> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> </list> </property> </bean> </list> </property> </bean> <!-- 上传文件时需要用到的分解器,默认将编码转为utf-8 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver<wbr>"> <property name="defaultEncoding"> <value>UTF-8</value> </property> </bean> <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewReso<wbr>lver" p:prefix="" p:suffix=".jsp" /> </beans>



按照本文所提到的配置文件配置的话,三个文件全部都放在web-inf里面,其中springmvc-servlet.xml里面配置的信息转换用到了两个外加的包

jackson-core-asl-1.4.2.jar

jackson-mapper-asl-1.4.2.jar

本文章主要是配置文件的编写,所以不涉及JAVA代码

转载于:https://www.cnblogs.com/yl2755/archive/2012/07/06/2579176.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值