SpringMVC +Spring + Hibernate4 整合

整理一下 SpringMVC + Spring + hibernate4 整合基本结构,遇到一些坑,记录一下,以便下次使用,不用网上到处翻资料,我使用的是Maven 工程管理 jar

1、首先是项目的基本 jar 已经尽量少了,如果还有其他需要的可以补上,pom.xml 如下:


<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>testgroupId>
    <artifactId>mavenDemoTestartifactId>
    <version>1.0-SNAPSHOTversion>

    <properties>
        <maven.compiler.source>1.8maven.compiler.source>
        <maven.compiler.target>1.8maven.compiler.target>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <spring.version>4.1.4.RELEASEspring.version>
        <hibernate.version>4.3.8.Finalhibernate.version>
        <jackson.version>2.5.0jackson.version>
    properties>


    <dependencies>

        
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.12version>
            <scope>testscope>
        dependency>

        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-coreartifactId>
            <version>${spring.version}version>
        dependency>

        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-beansartifactId>
            <version>${spring.version}version>
        dependency>

        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
            <version>${spring.version}version>
        dependency>

        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-txartifactId>
            <version>${spring.version}version>
        dependency>

        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webartifactId>
            <version>${spring.version}version>
        dependency>

        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-testartifactId>
            <version>${spring.version}version>
            <scope>testscope>
        dependency>

        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>${spring.version}version>
        dependency>

        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-ormartifactId>
            <version>${spring.version}version>
        dependency>

        
        <dependency>
            <groupId>org.hibernategroupId>
            <artifactId>hibernate-coreartifactId>
            <version>${hibernate.version}version>
        dependency>
        <dependency>
            <groupId>org.hibernategroupId>
            <artifactId>hibernate-ehcacheartifactId>
            <version>${hibernate.version}version>
        dependency>

        
        <dependency>
            <groupId>log4jgroupId>
            <artifactId>log4jartifactId>
            <version>1.2.17version>
        dependency>

        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.34version>
        dependency>

        
        <dependency>
            <groupId>com.mchangegroupId>
            <artifactId>c3p0artifactId>
            <version>0.9.5-pre10version>
        dependency>

        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>fastjsonartifactId>
            <version>1.2.3version>
        dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-annotationsartifactId>
            <version>${jackson.version}version>
        dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-coreartifactId>
            <version>${jackson.version}version>
        dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-databindartifactId>
            <version>${jackson.version}version>
        dependency>

        
        <dependency>
            <groupId>org.aspectjgroupId>
            <artifactId>aspectjweaverartifactId>
            <version>1.8.4version>
        dependency>
        <dependency>
            <groupId>org.aspectjgroupId>
            <artifactId>aspectjrtartifactId>
            <version>1.8.4version>
        dependency>

        
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>servlet-apiartifactId>
            <version>3.0-alpha-1version>
            <scope>providedscope>
        dependency>

        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>jstlartifactId>
            <version>1.2version>
        dependency>

        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-jdk14artifactId>
            <version>1.6.4version>
        dependency>

        <dependency>
            <groupId>javassistgroupId>
            <artifactId>javassistartifactId>
            <version>3.12.1.GAversion>
        dependency>
    dependencies>

project>

2、jar 准备好后,我们可以开始整合框架了,项目的基本结构是一个标准的maven工程结构,java目录下是主要的项目分层结构,最基本的是:Conctroller、Dao、Service、entity;而resource下放项目的配置文件,webapp主要是放一些静态资源(js、img、css等)和前台页面(html、jsp等),大概如下图所示:


SpringMVC +Spring + Hibernate4 整合_第1张图片

3、接着,我们准备一下数据库连接的配置文件,主要是给数据库连接池使用的,config.properties 文件如下:

#jdbc c3p0 config
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/shiro_work?useUnicode=true&characterEncoding=utf-8
jdbc.username = root
jdbc.password = root

#hibernate config
hibernate.dialect = org.hibernate.dialect.MySQLDialect
hibernate.show_sql = true
hibernate.format_sql = false
hibernate.hbm2ddl.auto = update

这里特别说明一下,#hibernate config 配置的是hibernate 使用的数据库,以及项目启动时是否自动创建或更新数据表,前提必须要先创建好数据库

4、接着,我们配置项目的日志,这里使用的是log4j,这个我也不熟,网上找的配置文件,log4j.properties 文件如下:

### set log levels ###
log4j.rootLogger = INFO , C , D , E   

### console ###
log4j.appender.C = org.apache.log4j.ConsoleAppender
log4j.appender.C.Target = System.out
log4j.appender.C.layout = org.apache.log4j.PatternLayout
log4j.appender.C.layout.ConversionPattern = [springmvc_hibernate_demo][%p] [%-d{yyyy-MM-dd HH:mm:ss}] %C.%M(%L) | %m%n

### log file ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = ../logs/springmvc_hibernate_demo.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = INFO
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = [springmvc_hibernate_demo][%p] [%-d{yyyy-MM-dd HH:mm:ss}] %C.%M(%L) | %m%n

### exception ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File = ../logs/springmvc_hibernate_demo_error.log
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = [sspringmvc_hibernate_demo][%p] [%-d{yyyy-MM-dd HH:mm:ss}] %C.%M(%L) | %m%n

5、 接着,配置的web项目的核心文件web.xml,web.xml文件如下:


<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <filter>
        <filter-name>encodingFilterfilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        <init-param>
            <param-name>encodingparam-name>
            <param-value>utf-8param-value>
        init-param>
        <init-param>
            <param-name>forceEncodingparam-name>
            <param-value>trueparam-value>
        init-param>
    filter>
    <filter-mapping>
        <filter-name>encodingFilterfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>


    
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:spring.xml,classpath:spring-hibernate.xmlparam-value>
    context-param>

    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>

    
    <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListenerlistener-class>
    listener>

    <servlet>
        <description>spring mvc servletdescription>
        <servlet-name>springmvcservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            
            <param-value>classpath:spring-mvc.xmlparam-value>
        init-param>
        <load-on-startup>1load-on-startup>
    servlet>

    <servlet-mapping>
        <servlet-name>springmvcservlet-name>
        <url-pattern>/url-pattern>
    servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.jspwelcome-file>
    welcome-file-list>

web-app>


这里说明一下:
< url-pattern > / 不会匹配到*.jsp,即:*.jsp 不会进入spring的 DispatcherServlet类 。
< url-pattern > /* 会匹配 *.jsp,会出现返回jsp视图时再次进入spring的DispatcherServlet 类,导致找不到对应的controller所以报404错。
所以,在配置视图的时候用/这种方式。

6、配置SpringMVC,主要是整合springMVC的基本配置,springmvc.xml 文件如下:


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc" 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-4.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">

    
    <context:component-scan base-package="com.shiro.controller" />

    
    <mvc:annotation-driven>
        
        <mvc:message-converters register-defaults="true">
            <bean
                    class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/html;charset=UTF-8value>
                        <value>application/json;charset=UTF-8value>
                    list>
                property>
            bean>
        mvc:message-converters>
    mvc:annotation-driven>

    
    <mvc:resources location="/static/" mapping="/static/**">mvc:resources>
    
    <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
                  value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/views" />
        <property name="suffix" value=".jsp" />
    bean>
beans>

这是特别说明一下,我们在web.xml文件配置的DispatcherServlet 的拦截路径是 / 即是项目的所有请求都会经过该控制器,然而事实上我们的静态资源(js、img、css等)如果被拦截了,那么前台页面显示就会出错,所以需要使用SpringMVC提供的 为这些资源放行

7、整合Spring,spring.xml文件如下:


<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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.1.xsd">

    
    
    <context:annotation-config />
    
    <context:component-scan base-package="com.shiro">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" />
    context:component-scan    
beans>

这里需要说明一下:
1、我们在使用 @Autowired 等注入服务需要把Service、Dao等注册成bean,所以需要配置扫描
2、 扫描指定的文件, 排除指定的文件,扫描除此之外的文件
3、 指定扫描包下有@Controller 注解标记的文件
指定扫描包下有@Service 注解标记的文件
指定扫描包下有 Repository 注解标记的文件
4、所以,SpringMVC的Controller层、Service层,Dao层,必须(一般是在实现类上,不是接口)加上这三个注解,才能被扫描到。

7、整合hibernate,连接数据库,这里是使用hibernate纯注解开发,不用写实体类的映射文件,spring-hibernate.xml 配置文件如下:


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.1.xsd ">

    
    <context:property-placeholder location="classpath:config.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}" />

        
        <property name="checkoutTimeout" value="30000" />
        
        <property name="idleConnectionTestPeriod" value="30" />
        
        <property name="maxIdleTime" value="30" />
        
        <property name="initialPoolSize" value="5" />
        <property name="minPoolSize" value="5" />
        <property name="maxPoolSize" value="20" />
        
        <property name="acquireIncrement" value="5" />
    bean>

    
    <bean id="sessionFactory"
          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        
        <property name="dataSource" ref="dataSource" />
        
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}prop>
                <prop key="hibernate.format_sql">${hibernate.format_sql}prop>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}prop>
            props>
        property>
        
        <property name="packagesToScan">
            <list>
                <value>com.shiro.entityvalue>
            list>
        property>      
    bean>

    
    <bean id="transactionManager"
          class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    bean>

    
    <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
        
        <tx:attributes>
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />

            <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="select*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="load*" propagation="SUPPORTS" read-only="true" />

            
            <tx:method name="*" />
        tx:attributes>
    tx:advice>

    
    <aop:config>
        
        <aop:pointcut id="transactionPointcut"
                      expression="execution(* com.shiro.service.*impl.*(..))" />
        
        <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
    aop:config>

beans>

这里有一点需要注意的,那就是在配置 sessionFactory 的时候,hibernate3和hibernate4 所使用的类是不一样的,在hibernate3 是使用 org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean,而在hibernate4 则是使用org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean,我这里使用的是hibernate4,具体的不同可以去查阅网上资料。

到这里,基本的框架已经整合完毕,前台页面的只是路径问题,注意一下不要写错就好了,还有不要忘记的就是,这里的 Entity 实体类是要使用hibernate的注解,和数据库对应映射。

你可能感兴趣的:(SpringMVC)