springMVC与hibernate集成的配置

此为hibernate的配置

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>
 <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
 <property name="connection.url">
  jdbc:oracle:thin:@127.0.0.1 :1521:orcl
 </property>
 <property name="connection.username">scott</property>
 <property name="connection.password">tiger</property>
 <property name="connection.driver_class">
  oracle.jdbc.driver.OracleDriver
 </property>
 <property name="myeclipse.connection.profile">oracle</property>
 <property name="show_sql">true</property>
 <mapping resource="com/xiaoyun/entity/TUser.hbm.xml" />

</session-factory>
</hibernate-configuration>

此为spring配置

<?xml version="1.0" encoding="UTF-8"?>
<beans
 xmlns="http://www.springframework.org/schema/beans"
 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
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:aop="http://www.springframework.org/schema/aop
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context 
 http://www.springframework.org/schema/context/spring-context-3.0.xsd
 http://www.springframework.org/schema/tx 
 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
 http://www.springframework.org/schema/aop 
 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
 http://www.springframework.org/schema/mvc 
 http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
 ">
 <context:annotation-config></context:annotation-config>
 <!-- sessionFactory   -->
 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="configLocation"
   value="/WEB-INF/classes/hibernate.cfg.xml">
  </property>
 </bean>
 <!--transactionManager 事务的定义 -->
 <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory"></property>
 </bean>
   <!-- 扫描注解 -->
 <context:component-scan base-package="com.xiaoyun">
   <context:include-filter type="annotation"
   expression="org.springframework.stereotype.Service" /> 
  <context:include-filter type="annotation"
   expression="org.springframework.stereotype.Repository" />
  <context:include-filter type="annotation"
   expression="org.springframework.stereotype.Component" />
  <context:exclude-filter type="annotation"
   expression="org.springframework.stereotype.Controller" />
 </context:component-scan>
 <context:annotation-config/>
 <!-- 增加 AOP 注解支持 写在此处 SpringMVC框架中的AOP无效,需要将此段配置写到springmvc配置文件中 -->
 <aop:aspectj-autoproxy proxy-target-class="true"/>
 <!-- 注解驱动开启,对注解方式的事物进行支持 --> 
 <tx:annotation-driven mode="aspectj" /> 
</beans>
此处springmvc配置

<?xml version="1.0" encoding="UTF-8"?>
<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"
 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context 
 http://www.springframework.org/schema/context/spring-context-3.0.xsd
 http://www.springframework.org/schema/aop 
 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
 http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
 http://www.springframework.org/schema/mvc 
 http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
 ">
 <!--
  是对包进行扫描,实现注释驱动Bean定义,同时将bean自动注入容器中使用。 即解决了@Controller标识的类的bean的注入和使用。
 -->
 <context:annotation-config />

 <!-- 进行扫描,以完成Controller创建和自动依赖注入的功能 -->
 <context:component-scan base-package="com.xiaoyun">
  <!--
   扫描Controller注解Bean 强烈建议Controller层扫描和其他注解扫描分开 其他类型注解(@Controller,
   @Service , @Configuration, etc.)扫描 由applicationContext-common.xml中配置
  -->
  <context:include-filter type="annotation"
   expression="org.springframework.stereotype.Controller" />
  <context:exclude-filter type="annotation"
   expression="org.springframework.stereotype.Service" />
  <context:exclude-filter type="annotation"
   expression="org.springframework.stereotype.Repository" />
 </context:component-scan>

 <!--
  相当于注册了DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter两个bean,
  配置一些messageconverter。即解决了@Controller注解的使用前提配置。
 -->
 <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
 <!-- Configure the multipart resolver -->
 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
 <!-- Enables the Spring MVC @Controller programming model -->
 <mvc:annotation-driven />
 <mvc:default-servlet-handler/>
 <!--  <mvc:resources location="/WEB-INF/" mapping="/WEB-INF/**"/> -->
 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="/WEB-INF/" />
  <!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 -->
  <property name="suffix" value=".jsp" />
  <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
 </bean>
 <!-- 增加 AOP 注解支持 必须写在此处,否则 SpringMVC框架中的AOP无效 -->
 <aop:aspectj-autoproxy proxy-target-class="true" />
 <!-- 注解驱动开启,对注解方式的事物进行支持 -->
 <tx:annotation-driven mode="aspectj" />
 </beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
 xmlns="http://java.sun.com/xml/ns/javaee
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 
 <!-- Spring上下文加载 -->
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
 </context-param>
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 
  <!-- SpringMVC Servlet 配置  -->
 <servlet>
  <servlet-name>apServlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/resources/springMVC/servlet-context.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>  
 <servlet-mapping>
  <servlet-name>apServlet</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>
 
 <!-- characterEncodingFilter -->
  <filter>
        <filter-name>characterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
     <!-- 实现Hibernate的延迟加载功能 -->
    <filter>
        <filter-name>openSessionInView</filter-name>
        <filter-class>
           org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
        </filter-class>
    </filter>
    <filter-mapping>
        <filter-name>openSessionInView</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    <!-- welcome-file-list -->
   <welcome-file-list>
        <welcome-file>login.jsp</welcome-file>
   </welcome-file-list>

</web-app>

0

评论

发表评论

电子邮件地址不会被公开。 必填项已用*标注

点击更换