spring MVC+spring+hibernate整合Demo

首先是web.xml配置文件。listener spring

  <!-spring配置文件的位置 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:com/config/applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

然后是配置springMVC,还是在web.xml中

 <!--springMVC配置文件的位置-->
    <servlet>
   <servlet-name>springMVC</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>classpath*:com/config/springMVC.xml</param-value>
    </init-param> 
       <!-- <load-on-startup>1</load-on-startup> -->
  </servlet>
   <servlet-mapping>
   <servlet-name>springMVC</servlet-name>
   <url-pattern>/</url-pattern>
  </servlet-mapping>

web容器首先将spring加载进来,初始化spring的配置文件,然后在加载springMVC。再接着是配置spring的文件,把hibernate的配置参数加载进spring里面。对应的配置文件为:

  <bean id="username" class="com.entity.User"></bean>
  <!-- 配置hibernate的文件 -->
 
 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  <property name="url" value="jdbc:mysql://127.0.0.1:3306/hibernate?useUnicode=true&amp;characterEncoding=UTF-8"/>
  <property name="username" value="root"/>
  <property name="password" value="root"/>
 </bean>
 
 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource" ref="dataSource"/>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
    <prop key="hibernate.hbm2ddl.auto">update</prop> 
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hiberante.format_sql">true</prop>
   </props>
  </property>
  <property name="configLocations">
   <list>
    <value>
     classpath*:com/config/hibernateapplication.cfg.xml
    </value>
   </list>
  </property>
 </bean>
 
 <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory"></property>
 </bean>
 
 <bean id="transactionBese" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" lazy-init="true" abstract="true">
  <property name="transactionManager" ref="transactionManager"></property>
  <property name="transactionAttributes">
   <props>
    <prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>
    <prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>
    <prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop>
    <prop key="modify*">PROPAGATION_REQUIRED,-Exception</prop>
    <prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop>
    <prop key="get*">PROPAGATION_NEVER</prop>
   </props>
  </property>
 </bean>
 <import resource="classpath*:com/config/myselfapplicationContext.xml"/>
   <!-- 配置service层的bean -->
   <bean id="userdao" class="com.dao.userDaoimp">
      <property name="sessionfactory" ref="sessionFactory"></property>
   </bean>
   <bean id="userManagerBase" class="com.service.userServiceimp">
     <property name="userdao" ref="userdao"></property>
   </bean>
   <bean id="username" parent="transactionBese">
      <property name="target" ref="userManagerBase"></property>
   </bean>

  对应的springMVC的配置文件

 <!-- 开启注解 -->
  <mvc:annotation-driven/> 
 
  <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
 <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
 <!-- 静态资源访问 -->
  <mvc:resources location="/img/" mapping="/img/**"/>  
  <mvc:resources location="/js/" mapping="/js/**"/>   
 
 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="/"></property>
  <property name="suffix" value=".jsp"></property>
 </bean>
 
 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="defaultEncoding" value="utf-8" />
       <property name="maxUploadSize" value="10485760000" />
       <property name="maxInMemorySize" value="40960" />
 </bean>

  springMVC通过扫描包控制对应的controller注解。spring和hibernate的结合是通过spring的bean的来管理hibernate的配置参数,springMVC和spring的结合是通过springMVC下注解Resource(name="")来管理对应的bean。实现了controller层调用service层,然后调用DAO层,通过hibernate管理DAO层的基本增删改查功能。对应的注解代码如下:

 @Resource(name="username")
 private userService userservice;

你可能感兴趣的:(spring MVC+spring+hibernate整合Demo)