Spring+SpringMVC+Hibernate

本框架为基于全注解的SpringMVC+Spring4.2+hibernate4.3

开发工具:    myEclipse.

整个工程含完整jar包,可在这里下载: http://www.demodashi.com/demo/10217.html

整体目录结构如下图:

Spring+SpringMVC+Hibernate_第1张图片

最近在鼓捣SpringMVC框架,现将成果都记录下来,免得前学后忘。之前用的框架一直是S2SH,一直苦于要配置一堆的配置文件,自从接触SpringMVC,发现这才是我一直想要的框架,基于全注解,开发过程中零配置,实在快哉。此教程非常适合零基础的人学习回归正题,基于全注解驱动的SpringMVC+Spring4.2+hibernate4.3框架搭建(整合)过程如下,:

开发工具为myEclipse

第一步:新建一个web项目


第二步:加入所需的jar包

Spring+SpringMVC+Hibernate_第2张图片

第三步:接下来我们开始配置SpringMVC容器

为了分工明确,我们将SpringMVC的配置单独写在spring-servlet.xml里,Spring的配置写在spring-common.xml(事务、数据源、sessionFactory等等)里。

spring-common.xml和spring-servlet.xml先加入如下schemal

[html]  view plain  copy
  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  3.     xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"  
  5.     xmlns:task="http://www.springframework.org/schema/task" xmlns:cache="http://www.springframework.org/schema/cache"  
  6.     xmlns:util="http://www.springframework.org/schema/util"  
  7.     xmlns:websocket="http://www.springframework.org/schema/websocket"  
  8.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd  
  9.                     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd  
  10.                     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd  
  11.                     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd  
  12.                     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd  
  13.                     http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd  
  14.                     http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.1.xsd  
  15.                     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">  
然后spring容器的配置先放下,先来配置springMVC(spring-servlet.xml)的配置

在schemal的结尾处加入这一句:default-autowire="byName" ,依赖注入将根据name自动装配。

接下来启动注解驱动的SpringMVC功能:

[html]  view plain  copy
  1. <mvc:annotation-driven />  

扫描注解包(在SpringMVC的容器里,只扫描Controller注解就行了)

[html]  view plain  copy
  1. <context:component-scan base-package="com.mvc.rest"  
  2.         use-default-filters="false">  
  3.         <context:include-filter type="annotation"  
  4.             expression="org.springframework.stereotype.Controller" />  
  5.   context:component-scan>  

use-default-filters默认为true,默认会扫描@Component、@Controller、@Repository、@Service的注解,在这里只扫描@Controller注解是因为,SpringMVC的容器没有事务的能力,所以扫描@Repository、@Service的注解只能放在Spring的容器。也正因为如此,事务的配置要写在Spring的容器。

然后是对模型视图名称的解析,在请求时模型视图名称添加前后缀(前缀是从控制器里返回的视图的父目录,此处配置的是让容器在WEB-INF/view/下找寻对应的视图;后缀是给视图名称追加后缀名,此处配置的是jsp后缀)

[html]  view plain  copy
  1. <bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver"  p:prefix="/WEB-INF/view/" p:suffix=".jsp" />  

配置CommonsMultpartResolver,上传文件的时候要用到CommonsMultpartResolver,maxUploadSize设置上传文件的大小限制,上传文件必须先配置此解析器。

[html]  view plain  copy
  1. <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
  2.      <property name="maxUploadSize" value="10485760" />  
  3. bean>  

配置login视图解析,在登录拦截器里,校验未登录的话,要跳转到登录页面,然后由于login页面放在WEB-INF目录下,所以设置跳转到login.jsp会跳转不过去,在此处设置如下,在返回此view-name的地方,容器便不会当作Controller的路径,当作视图的路径跳转,在拦截器里便可以跳转到login页面(此配置告诉容器,这不是一个controller的方法的路径,而是一个视图的名称,请当作视图处理)。

[html]  view plain  copy
  1. <mvc:view-controller path="/" view-name="login" />  

拦截器的配置也是放在SpringMVC的容器里,拦截器以后的文章里再详细解说。

到此spring-servlet.xml的配置就告一段落了,spring-servlet.xml的全文如下:

[html]  view plain  copy
  1. xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  5.     xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"  
  7.     xmlns:task="http://www.springframework.org/schema/task" xmlns:cache="http://www.springframework.org/schema/cache"  
  8.     xmlns:util="http://www.springframework.org/schema/util"  
  9.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd  
  10.         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd  
  11.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd  
  12.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd  
  13.         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd  
  14.         http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd  
  15.         http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.1.xsd  
  16.         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd"  
  17.     default-autowire="byName">  
  18.     <mvc:annotation-driven />  
  19.       
  20.     <context:component-scan base-package="com.mvc.rest" use-default-filters="false">  
  21.       <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />  
  22.     context:component-scan>  
  23.       
  24.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"  
  25.         p:prefix="/WEB-INF/view/" p:suffix=".jsp" />  
  26.     <mvc:view-controller path="/" view-name="login" />  
  27.     <bean id="multipartResolver"  
  28.         class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
  29.         <property name="maxUploadSize" value="10485760" />  
  30.     bean>  
  31.       
  32. <property name="packagesToScan">  
  33.   <list>  
  34.     <value>com.mvc.rest.entityvalue>  
  35.  list>  
  36. property>  
  37.  bean>  
packagesToScan扫描我们的hibernate实体文件。

最后配置事务

[html]  view plain  copy
  1. <bean id="txManager"  
  2.         class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
  3.         <property name="sessionFactory" ref="sessionFactory" />  
  4.     bean>  
  5.     <tx:advice id="txAdvice" transaction-manager="txManager">  
  6.         <tx:attributes>  
  7.             <tx:method name="save*" propagation="REQUIRED" />  
  8.             <tx:method name="add*" propagation="REQUIRED" />  
  9.             <tx:method name="edit*" propagation="REQUIRED" />  
  10.             <tx:method name="update*" propagation="REQUIRED" />  
  11.             <tx:method name="delete*" propagation="REQUIRED" />  
  12.             <tx:method name="register*" propagation="REQUIRED" />  
  13.             <tx:method name="all" propagation="REQUIRED" />  
  14.             <tx:method name="changePassword*" propagation="REQUIRED" />  
  15.             <tx:method name="restPassword*" propagation="REQUIRED" />  
  16.             <tx:method name="authorize*" propagation="REQUIRED" />  
  17.             <tx:method name="send*" propagation="REQUIRED" />  
  18.             <tx:method name="init*" propagation="REQUIRED" />  
  19.               
  20.         tx:attributes>  
  21.     tx:advice>  
  22.     <aop:config>  
  23.         <aop:pointcut id="serviceOperation"  
  24.             expression="execution(* com.mvc.rest.service.impl.*.*(..))" />  
  25.         <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />  
  26. aop:config>  
完整的spring-common.xml的配置如下:

[html]  view plain  copy
  1. xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  5.     xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"  
  7.     xmlns:task="http://www.springframework.org/schema/task" xmlns:cache="http://www.springframework.org/schema/cache"  
  8.     xmlns:util="http://www.springframework.org/schema/util"  
  9.     xmlns:websocket="http://www.springframework.org/schema/websocket"  
  10.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd  
  11.         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd  
  12.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd  
  13.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd  
  14.         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd  
  15.         http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd  
  16.         http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.1.xsd  
  17.         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">  
  18.     <context:component-scan base-package="com.mvc.rest" />  
  19.       
  20.     <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  21.         <property name="locations">  
  22.             <value>classpath:jdbc.propertiesvalue>  
  23.         property>  
  24.     bean>  
  25.       
  26.     <bean id="dataSource" destroy-method="close"  
  27.         class="org.apache.commons.dbcp.BasicDataSource">  
  28.         <property name="driverClassName" value="${jdbc.driverClassName}" />  
  29.         <property name="url" value="${jdbc.url}" />  
  30.         <property name="username" value="${jdbc.username}" />  
  31.         <property name="password" value="${jdbc.password}" />  
  32.     bean>  
  33.     <bean id="sessionFactory"  
  34.         class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
  35.         <property name="dataSource" ref="dataSource" />  
  36.         <property name="hibernateProperties">  
  37.             <props>  
  38.                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialectprop>  
  39.                 <prop key="hibernate.hbm2ddl.auto">updateprop>  
  40.                 <prop key="hibernate.show_sql">trueprop>  
  41.                 <prop key="hibernate.format_sql">trueprop>  
  42.             props>  
  43.         property>  
  44.           
  45.         <property name="packagesToScan">  
  46.             <list>  
  47.                 <value>com.mvc.rest.entityvalue>  
  48.             list>  
  49.         property>  
  50.     bean>  
  51.     <bean id="txManager"  
  52.         class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
  53.         <property name="sessionFactory" ref="sessionFactory" />  
  54.     bean>  
  55.     <tx:advice id="txAdvice" transaction-manager="txManager">  
  56.         <tx:attributes>  
  57.             <tx:method name="save*" propagation="REQUIRED" />  
  58.             <tx:method name="add*" propagation="REQUIRED" />  
  59.             <tx:method name="edit*" propagation="REQUIRED" />  
  60.             <tx:method name="update*" propagation="REQUIRED" />  
  61.             <tx:method name="delete*" propagation="REQUIRED" />  
  62.             <tx:method name="register*" propagation="REQUIRED" />  
  63.             <tx:method name="all" propagation="REQUIRED" />  
  64.             <tx:method name="changePassword*" propagation="REQUIRED" />  
  65.             <tx:method name="restPassword*" propagation="REQUIRED" />  
  66.             <tx:method name="authorize*" propagation="REQUIRED" />  
  67.             <tx:method name="send*" propagation="REQUIRED" />  
  68.             <tx:method name="init*" propagation="REQUIRED" />  
  69.               
  70.         tx:attributes>  
  71.     tx:advice>  
  72.     <aop:config>  
  73.         <aop:pointcut id="serviceOperation"  
  74.             expression="execution(* com.mvc.rest.service.impl.*.*(..))" />  
  75.         <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />  
  76.     aop:config>  
  77. beans>  

到此,基于全注解的SpringMVC+Spring4.2+hibernate4.3框架搭建大功告成。

你可能感兴趣的:(spring,spring,mvc,hibernate)