spring-springmvc-hibernate整合

最近一直在找工作,面试的时候提到他们公司的项目使用ssh(spring springmvc hibernate)做的,回想起来好像还真没有做过这个整合。其实想了一下,也挺简单的,就是传统ssh的webmvc层面struts2替换为springmvc,或者说是ssm的mybatis替换为hibernate作为orm框架。反正都是spring管理,挺简单的。

做了一个demo学习一下,顺便回顾一下hibernate。

思路:springmvc天然集成spring,难度不大,主要是集成hibernate。

具体版本信息:
spring:4.3.7.RELEASE
hibernate:5.2.11.Final

一,spring+springmvc:天然集成,具体需要配置的有:

  1. 添加依赖
  2. 配置加载spring容器和springmvc的dispatcherServlet
  3. 配置applicationContext.xml文件
  4. 配置springmvc.xml
  5. 配置resource资源文件
    二,spring+hibernate
    采用hbm映射文件的方式,具体需要的配置包含
  6. 添加依赖
  7. 配置数据源(druid)
  8. 配置sqlsessionFactory
  9. 配置一个HibernateTemplate
    4.配置事务相关


spring-springmvc-hibernate整合_第1张图片
捕获.PNG

1.完整的pom:


    4.0.0
    com.tuxianchao
    spring-springmvc-hibernate-demo
    war
    1.0-SNAPSHOT
    spring-springmvc-hibernate-demo Maven Webapp

    
        4.3.7.RELEASE
        5.2.11.Final
        1.6.8
        5.1.32
        1.0.9
        1.2
        3.0-alpha-1
        2.2
        2.7.3
        1.3.1
        1.6.4
    
    
        
        
            org.aspectj
            aspectjweaver
            1.6.8
        
        
            org.springframework
            spring-aop
            ${spring.version}
        
        
            org.springframework
            spring-web
            ${spring.version}
        
        
            org.springframework
            spring-beans
            ${spring.version}
        
        
            org.springframework
            spring-context
            ${spring.version}
        
        
            org.springframework
            spring-core
            ${spring.version}
        
        
            org.springframework
            spring-expression
            ${spring.version}
        
        
            org.springframework
            spring-orm
            ${spring.version}
        
        
            org.springframework
            spring-web
            ${spring.version}
        
        
            org.springframework
            spring-webmvc
            ${spring.version}
        

        
            org.springframework
            spring-aspects
            ${spring.version}
        

        
        
            org.hibernate
            hibernate-core
            ${hibernate.version}
        
        
            org.hibernate
            hibernate-entitymanager
            ${hibernate.version}
        
        
            org.hibernate
            hibernate-validator
            5.2.1.Final
        
        
        
            org.hibernate
            hibernate-ehcache
            ${hibernate.version}
        
        

        
        
            org.slf4j
            slf4j-log4j12
            ${slf4j.version}
        

        
        
            jstl
            jstl
            ${jstl.version}
        
        
            javax.servlet
            servlet-api
            ${servlet-api.version}
            provided
        
        
            javax.servlet.jsp
            jsp-api
            ${jsp-api.version}
            provided
        

        
        
            mysql
            mysql-connector-java
            ${mysql.version}
        
        
        
            com.alibaba
            druid
            ${druid.version}
        

        
        
            com.fasterxml.jackson.core
            jackson-core
            ${jackson.version}
        
        
            com.fasterxml.jackson.core
            jackson-annotations
            ${jackson.version}
        

        
            com.fasterxml.jackson.core
            jackson-databind
            ${jackson.version}
        
        
        
            commons-fileupload
            commons-fileupload
            ${commons-fileupload.version}
        

    
    
        
        
            
                src/main/java
                
                    **/*.hbm.xml
                
            
            
                src/main/resources
                
                    **/*.xml
                    **/*.properties
                
            
        
        spring-springmvc-hibernate-demo
        
            
            
                org.apache.maven.plugins
                maven-resources-plugin
                2.7
                
                    UTF-8
                
            
            
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.2
                
                    1.8
                    1.8
                    UTF-8
                
            
            
            
                org.apache.tomcat.maven
                tomcat7-maven-plugin
                2.2
                
                    /
                    8080
                    UTF-8
                
            
            
            
                org.apache.maven.plugins
                maven-war-plugin
                2.2
            
        
    


  1. web.xml


    easybuy-manager-web
    
        /
    

    
    
        contextConfigLocation
        classpath:spring-config/applicationContext.xml
    
    
        org.springframework.web.context.ContextLoaderListener
    

    
    
    
        CharacterEncodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            utf-8
        
        
            forceRequestEncoding
            true
        
        
            forceResponseEncoding
            true
        
    
    
        CharacterEncodingFilter
        /
    

    
    
        dispatcherServlet
        org.springframework.web.servlet.DispatcherServlet
        
        
            contextConfigLocation
            classpath:spring-config/springmvc.xml
        
        
        1
    
    
        dispatcherServlet
        /
    

3.applicationContext.xml




    
    
        
        
    

    
    

    
    
    
        
        
        
        
        
        

        
        

        
        

        
        

        

        
        
        

        
        
        

        
        
    


    
    
    
        
        
        
            
                ${hibernate.hbm2ddl.auto} 
                ${hibernate.dialect}   
                ${hibernate.show_sql}     
                ${hibernate.format_sql}     
            
        
        
            
                com/tuxianchao/demo/entity/User.hbm.xml
            
        
    


    
    
        
    
    
    
        
    

    


    
    
        
            
            
            
            
            
            
            
            
            
            
        
    
    
    
        
        
        
        
    

4.springmvc.xml




    
    
        
    

    
    
    
    
    


    
    
        
        
    
    
    


    
    
    

    
    
        
        
        
        
    

完成的工程结构
https://github.com/tuxianchao/spring-springmvc-hibernate-demo

你可能感兴趣的:(spring-springmvc-hibernate整合)