springMVC+hibernate配置

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

applicationContext.xml

[html] view plain copy print ?
  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.       
  16.     <context:annotation-config/>  
  17.     <context:component-scan base-package="扫瞄的包名" scoped-proxy="targetClass">  
  18.   
  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.       
  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.MySQLDialectprop>  
  34.                 <prop key="hibernate.show_sql">trueprop>  
  35.                 <prop key="hibernate.hbm2ddl.auto">updateprop>  
  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.       
  48.     <tx:annotation-driven transaction-manager="transactionManager" />  
  49. beans>  


	
    
    

    

	
    
        
        
        
        
    
	
    
        
        
            
                org.hibernate.dialect.MySQLDialect
                true
                update
            
        
        
			
        
            中间写入hibernate注解的实体
        
    
    
        
    
    
    

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

[html] view plain copy print ?
  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 configdescription>  
  6.     <param-name>contextConfigLocationparam-name>  
  7.     <param-value>/WEB-INF/applicationContext.xmlparam-value>  
  8.   context-param>  
  9.   <listener>  
  10.     <description>spring listernerdescription>  
  11.     <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>  
  12.   listener>  
  13.     
  14.   
  15.   <servlet>  
  16.     <servlet-name>springmvcservlet-name>  
  17.     <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>  
  18.     <load-on-startup>2load-on-startup>  
  19.   servlet>  
  20.   <servlet-mapping>  
  21.     <servlet-name>springmvcservlet-name>  
  22.   
  23.     <url-pattern>*.dourl-pattern>    
  24.   servlet-mapping>  
  25.   
  26.   
  27.   
  28.     <filter>  
  29.         <filter-name>Spring character encoding filterfilter-name>  
  30.         <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>  
  31.         <init-param>  
  32.             <param-name>encodingparam-name>  
  33.             <param-value>utf-8param-value>  
  34.         init-param>  
  35.     filter>  
  36.     <filter-mapping>  
  37.         <filter-name>Spring character encoding filterfilter-name>  
  38.         <url-pattern>/*url-pattern>  
  39.     filter-mapping>  
  40.     
  41.   
  42.   <error-page>   
  43.       <error-code>404error-code>   
  44.       <location>/error/404.htmllocation>   
  45.   error-page>   
  46.     
  47.   
  48.   <session-config>  
  49.     <session-timeout>30session-timeout>  
  50.   session-config>  
  51.   <welcome-file-list>  
  52.     <welcome-file>login.jspwelcome-file>  
  53.   welcome-file-list>  
  54. web-app>  



  
    spring config
    contextConfigLocation
    /WEB-INF/applicationContext.xml
  
  
    spring listerner
    org.springframework.web.context.ContextLoaderListener
  
  

  
    springmvc
    org.springframework.web.servlet.DispatcherServlet
    2
  
  
    springmvc

    *.do  
  



	
		Spring character encoding filter
		org.springframework.web.filter.CharacterEncodingFilter
		
			encoding
			utf-8
		
	
	
		Spring character encoding filter
		/*
	
  

   
	  404 
	  /error/404.html 
   
  

  
    30
  
  
    login.jsp
  


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

springmvc-servlet.xml

[html] view plain copy print ?
  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.       
  12.     <context:component-scan base-package="cn.bonoon" />  
  13.   
  14.       
  15.     <bean  
  16.         class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
  17.   
  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-8value>  
  25.                         list>  
  26.                     property>  
  27.                 bean>  
  28.             list>  
  29.         property>  
  30.     bean>  
  31.   
  32.   
  33.   
  34.     <bean id="multipartResolver"    
  35.         class="org.springframework.web.multipart.commons.CommonsMultipartResolver">    
  36.         <property name="defaultEncoding">  
  37.             <value>UTF-8value>  
  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>  




	
	

	
	

		
			
				
					
						
							application/json;charset=UTF-8
						
					
				
			
		
	



	  
        
        	UTF-8
        
      

	
	




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

jackson-core-asl-1.4.2.jar

jackson-mapper-asl-1.4.2.jar

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

你可能感兴趣的:(springMVC+hibernate配置)