spring+hibernate(jpa/LocalEntityManagerFactoryBean)+springMVC配置文件

相关参考: http://www.cnblogs.com/beiyeren/archive/2013/01/23/2873210.html

方法一:

屏幕快照 2017-06-21 19.38.05.png

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<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_4_0.xsd"
  version="4.0"
  metadata-complete="true">

    <description>
      Servlet and JSP Examples.
    </description>
    <display-name>Servlet and JSP Examples</display-name>

    <!-- spring监听器: -->
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
     </listener>
     
     <!-- 指定spring文件路径 -->
     <context-param>
         <param-name>contextConfigLocation</param-name>
         <param-value>classpath:spring.xml</param-value>
     </context-param>
     
     <!-- =====================配置spring mvc  Start================================= -->
     <servlet>
         <servlet-name>SpringMVC</servlet-name>
         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
         
         <!--配置springmvc路劲-->
         <init-param>
             <param-name>contextConfigLocation</param-name>
             <param-value>classpath:springmvc.xml</param-value>
         </init-param>
         <!--    >=0代表web容器启动的时候加载servlet(数字代表优先级)    -->
         <load-on-startup>1</load-on-startup>
     </servlet>
     
     <servlet-mapping>
         <servlet-name>SpringMVC</servlet-name>
        <url-pattern>/</url-pattern>
     </servlet-mapping>
     
     <!--    配置编码过滤器    -->
    <!--  <filter>
         <filter-name>encoding</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>encoding</filter-name>
         <url-pattern>/*</url-pattern>
     </filter-mapping> -->
       
   <!-- <welcome-file-list>
     <welcome-file>index.jsp</welcome-file>
     <welcome-file>index.html</welcome-file>
   </welcome-file-list> -->

</web-app>

spring.xml

<?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:aop="http://www.springframework.org/schema/aop"  
    xmlns:tx="http://www.springframework.org/schema/tx" 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-3.2.xsd  
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd  
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd  
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> 

    <context:component-scan base-package="com.spring.*" />  
 
    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" >  
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />  
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/spring" />  
        <property name="username" value="root" />  
        <property name="password" value="123" />   
    </bean>
    
    <!-- 利用Spring的实体管理器工厂来创建JPA实体管理器 -->  
    <bean id="entityManagerFactory"  
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">  
        <property name="dataSource" ref="dataSource" />  
        <property name="jpaVendorAdapter">  
            <bean  
                class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">  
                <property name="database" value="MYSQL" />  
                <property name="showSql" value="true" />  
                <!-- <property name="generateDdl" value="true" /> -->  
            </bean>  
        </property>  
    </bean>  
    <!-- <bean id="sessionFactory"   
        class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">  
        <property name="dataSource" ref="dataSource" />  
        <property name="packagesToScan">  
            <list>  
                <value>com.spring.model</value>  
            </list>  
        </property>  
      
        <property name="hibernateProperties">  
            <props>  
                <prop key="dialect">org.hibernate.dialect.MySQLDialect</prop>  
                <prop key="hibernate.show_sql">true</prop>  
                <prop key="hibernate.format_sql">true</prop>  
                <prop key="hibernate.hbm2ddl.auto">update</prop>  
                  
            </props>  
        </property>  
    </bean>  -->
    
    <!-- <bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">  
          <property name="sessionFactory" ref="sessionFactory"/>  
    </bean> -->  
    
    <!-- 声明一个Spring提供的JPA事务管理器,传入的参数是Spring中的实体管理器工厂 -->   
    <bean id="txManager"  
        class="org.springframework.orm.jpa.JpaTransactionManager">  
        <property name="entityManagerFactory" ref="entityManagerFactory" />  
    </bean>  
    
    <!--统配方式事务管理 -->
    <tx:advice id="txadvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="save" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="transfer*" propagation="REQUIRED" />
            <tx:method name="upgrade*" propagation="REQUIRED" />
            <tx:method name="change*" propagation="REQUIRED" />
            <tx:method name="do*" propagation="REQUIRED" />
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <aop:pointcut expression="execution(* com.spring.manager.*.*(..))"
            id="txpointcut" />
        <aop:advisor advice-ref="txadvice" pointcut-ref="txpointcut" />
    </aop:config>
</beans>

springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">

    <!-- 启动扫描所有的controller -->
    <context:component-scan base-package="com.spring.controller" />

    <!-- 完成请求和注解POJO的映射 -->
    <!-- 这两个类用来启动基于Spring MVC的注解功能,将控制器与方法映射加入到容器中 -->
    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    </bean>
    <bean
        class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    </bean>

    <!-- web请求的拦截器 
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**" />
            <mvc:exclude-mapping path="/register"/>
            <mvc:exclude-mapping path="/test/*"/>
            <mvc:exclude-mapping path="/login"/>
            <mvc:exclude-mapping path="/findPwd"/>
            <mvc:exclude-mapping path="/getTRegion"/>
            <mvc:exclude-mapping path="/getIndustry"/>
            <mvc:exclude-mapping path="/getTRegionByParentId"/>
            <mvc:exclude-mapping path="/getMessageCode"/>
            <mvc:exclude-mapping path="/listRecommend"/>
            <mvc:exclude-mapping path="/news/getNewsDetail"/>
            <mvc:exclude-mapping path="/css/ceos.css"/>
            <bean class="cn.kiiwii.framework.interceptor.MyInterceptor"></bean>
        </mvc:interceptor>
    </mvc:interceptors>

    -->

    <!-- jsp页面解析器,当Controller返回XXX字符串时,先通过拦截器,然后该类就会在/WEB-INF/views/目录下,查找XXX.jsp文件 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    <mvc:view-controller path="/" view-name="login"/>
    <!-- 多段文件上传 -->
    <!-- <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="1073741824" />
    </bean>
    -->
    <!-- <mvc:default-servlet-handler /> -->
</beans>

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence     http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">

    <persistence-unit name="mainPU" transaction-type="RESOURCE_LOCAL">
        
    </persistence-unit>
</persistence>

方法二:

47FEF65A-7364-4B7A-B380-E1C4029F17D7.png

springmvc-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">

    <!-- 启动扫描所有的controller -->
    <context:component-scan base-package="com.spring.controller" />

    <!-- 完成请求和注解POJO的映射 -->
    <!-- 这两个类用来启动基于Spring MVC的注解功能,将控制器与方法映射加入到容器中 -->
    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    </bean>
    <bean
        class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    </bean>
    
    <!-- web请求的拦截器 
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**" />
            <mvc:exclude-mapping path="/register"/>
            <mvc:exclude-mapping path="/test/*"/>
            <mvc:exclude-mapping path="/login"/>
            <mvc:exclude-mapping path="/findPwd"/>
            <mvc:exclude-mapping path="/getTRegion"/>
            <mvc:exclude-mapping path="/getIndustry"/>
            <mvc:exclude-mapping path="/getTRegionByParentId"/>
            <mvc:exclude-mapping path="/getMessageCode"/>
            <mvc:exclude-mapping path="/listRecommend"/>
            <mvc:exclude-mapping path="/news/getNewsDetail"/>
            <mvc:exclude-mapping path="/css/ceos.css"/>
            <bean class="cn.kiiwii.framework.interceptor.MyInterceptor"></bean>
        </mvc:interceptor>
    </mvc:interceptors>
    
    -->

    <!-- jsp页面解析器,当Controller返回XXX字符串时,先通过拦截器,然后该类就会在/WEB-INF/views/目录下,查找XXX.jsp文件 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    <!-- 表示当访问主页时自动转发到index控制器   -->
    <mvc:view-controller path="/" view-name="login"/> 
    <!-- 多段文件上传 -->
    <!-- <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="1073741824" />
    </bean>
    -->
    <!-- <mvc:default-servlet-handler /> -->
</beans>

beans.xml

<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
    ">

    <!-- 载入常用的配置 -->
    <context:property-placeholder location="classpath:jdbc_hibernate.properties,classpath:druid.properties" />
    <!-- 注解扫描所有包 -->
    <!-- <context:annotation-config/> -->
    <context:component-scan base-package="com.spring.*" />
        
        <!-- 定义实体管理器工厂
         Jpa配置   LocalContainerEntityManagerFactoryBean这个选项Spring扮演了容器的角色。完全掌管JPA -->
         <!--  点我查看 spring生成EntityManagerFactory的三种方式  -->
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
          <property name="persistenceUnitName" value="mainPU" />
    </bean>
    
    <!-- Jpa 事务管理器  -->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>
    <!--注解方式事务管理 -->
    <!-- <tx:annotation-driven transaction-manager="transactionManager"/> -->
    <!--统配方式事务管理 -->
    <tx:advice id="txadvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="save" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="transfer*" propagation="REQUIRED" />
            <tx:method name="upgrade*" propagation="REQUIRED" />
            <tx:method name="change*" propagation="REQUIRED" />
            <tx:method name="do*" propagation="REQUIRED" />
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <aop:pointcut expression="execution(* com.spring.manager.*.*(..))"
            id="txpointcut" />
        <aop:advisor advice-ref="txadvice" pointcut-ref="txpointcut" />
    </aop:config>

    <!-- 事务配置 end -->
    
    <!-- 强制使用cglib代理 -->
    <!--<aop:aspectj-autoproxy proxy-target-class="true" />  -->

</beans>

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence     http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">

    <persistence-unit name="mainPU" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
            <!-- 
            <property name="hibernate.connection.url" value="jdbc:mysql://127.0.0.1:3306/yh_gyf?useUnicode=true&characterEncoding=UTF-8" />
            <property name="hibernate.connection.url" value="jdbc:mysql://120.55.117.108:3306/yh_gyf?useUnicode=true&characterEncoding=UTF-8" />
             -->
            <property name="hibernate.connection.url" value="jdbc:mysql://127.0.0.1:3306/apartment?useUnicode=true&characterEncoding=UTF-8" />
            <property name="hibernate.connection.username" value="root" />
            <!-- <property name="hibernate.connection.password" value="123456" /> -->
            <property name="hibernate.connection.password" value="123" />
            <!-- <property name="hibernate.connection.password" value="Rootyetao2015" /> -->
            <property name="hibernate.c3p0.max_size" value="100" />
            <property name="hibernate.c3p0.min_size" value="2" />
            <property name="hibernate.c3p0.timeout" value="1200" />         <!-- 获得连接的超时时间,如果超过这个时间,会抛出异常,单位毫秒 -->
            <property name="hibernate.c3p0.max_statements" value="100" />   <!-- 最大的PreparedStatement的数量 -->
            <property name="hibernate.c3p0.idle_test_period" value="60" />  <!-- 每隔60秒检查连接池里的空闲连接 ,单位是秒 -->
            <property name="hibernate.c3p0.acquire_increment" value="5" />  <!-- 当连接池里面的连接用完的时候,C3P0一下获取的新的连接数 -->
            <property name="hibernate.c3p0.validate" value="true" />        <!-- 每次都验证连接是否可用 -->
            <property name="hibernate.c3p0.testConnectionOnCheckout" value="false" />
            <property name="hibernate.cache.use_query_cache" value="10" />
            <property name="hibernate.current_session_context_class" value="thread" />
            <property name="hibernate.show_sql" value="false" />
        </properties>
    </persistence-unit>
</persistence>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:beans.xml</param-value>
    </context-param>
    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <servlet>
        <servlet-name>Spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    
</web-app>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容