springmvc + spring data jpa + hibernate 配置

#/cloudsafer/src/config/applicationContext.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:context="http://www.springframework.org/schema/context"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:jms="http://www.springframework.org/schema/jms"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xmlns:jpa="http://www.springframework.org/schema/data/jpa"
 xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
  http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-4.1.xsd
  http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository-1.8.xsd
  http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.8.xsd
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
 <!-- 引入注解扫描器 -->
 <context:annotation-config />
 <!-- 引入属性文件 -->
 <context:property-placeholder location="classpath*:config/jdbc.properties" />
 <!-- 配置要扫描的包 -->
 <context:component-scan base-package="cn.nudt.cloudsafer" />
 <!-- 配置数据源 -->
 <bean id="dataSource"
  class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
  <property name="targetDataSource">
   <bean class="com.mchange.v2.c3p0.ComboPooledDataSource"
    destroy-method="close">
    <property name="driverClass" value="${jdbc.driverClass}" />
    <property name="jdbcUrl" value="${jdbc.jdbcUrl}" />
    <property name="user" value="${jdbc.user}" />
    <property name="password" value="${jdbc.password}" />
    <property name="maxPoolSize" value="${c3p0.maxPoolSize}" />
    <property name="minPoolSize" value="${c3p0.minPoolSize}" />
    <property name="initialPoolSize" value="${c3p0.initialPoolSize}" />
    <property name="maxIdleTime" value="${c3p0.maxIdleTime}" />
   </bean>
  </property>
 </bean>
 <bean id="entityManagerFactory"
  class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
  lazy-init="true">
  <property name="dataSource" ref="dataSource" />
  <property name="persistenceUnitName" value="myjpa" />
  <property name="jpaVendorAdapter">
   <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    <property name="database" value="MYSQL" />
    <property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect" />
    <property name="generateDdl" value="true" />
    <property name="showSql" value="true" />
   </bean>
  </property>
  <property name="jpaProperties">
   <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>
    <!-- 配置二级缓存 -->
    <prop key="hibernate.cache.use_second_level_cache">true</prop>
    <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
   </props>
  </property>
  <!-- 配置要扫描的实体类 -->
  <property name="packagesToScan">
   <list>
    <value>cn.nudt.cloudsafer.entity</value>
   </list>
  </property>
 </bean>
 <!-- 配置事务 -->
 <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
  <property name="entityManagerFactory" ref="entityManagerFactory" />
 </bean>
 <tx:annotation-driven transaction-manager="transactionManager"
  proxy-target-class="true" />
 <!--Spring Data Jpa配置 -->
 <jpa:repositories base-package="cn.nudt.cloudsafer.repository"
  transaction-manager-ref="transactionManager"
  entity-manager-factory-ref="entityManagerFactory" />
</beans> 


#/cloudsafer/src/config/springMVC-servlet.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:context="http://www.springframework.org/schema/context"
 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
 <context:component-scan base-package="cn.nudt.cloudsafer.controller"
  use-default-filters="false">
  <context:include-filter type="annotation"
   expression="org.springframework.stereotype.Controller" />
 </context:component-scan>
 <mvc:annotation-driven />
 <mvc:default-servlet-handler />
 <!--定义首页 -->
 <mvc:view-controller path="/" view-name="redirect:/index.jsp" />
 <!--定义JSP -->
 <bean
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="/WEB-INF/views/" />
  <property name="suffix" value=".jsp" />
 </bean>
 <!--支持上传文件 -->
 <bean id="multipartResolver"
  class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
  p:defaultEncoding="UTF-8" p:maxUploadSize="5400000" p:uploadTempDir="WEB-INF/fileUpload" />
 <bean id="messageSource"
  class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
  <property name="basename" value="classpath:config/messages" />
  <property name="fileEncodings" value="utf-8" />
  <property name="cacheSeconds" value="120" />
 </bean>
 <!-- 以下 validator ConversionService 在使用 mvc:annotation-driven 会 自动注册 -->
 <!-- ①注册ConversionService -->
 <mvc:annotation-driven conversion-service="conversionService" />
 <bean id="conversionService"
  class="org.springframework.core.convert.support.GenericConversionService" />
 <bean id="validator"
  class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
  <property name="providerClass" value="org.hibernate.validator.HibernateValidator" />
  <!-- 如果不加默认到 使用classpath下的 ValidationMessages.properties -->
  <property name="validationMessageSource" ref="messageSource" />
 </bean>
 <!-- 此处使用Hibernate validator实现: validationMessageSource属性:指定国际化错误消息从哪里取, 
  此处使用之前定义的messageSource来获取国际化消息;如果此处不指定该属性,则默认到classpath下的ValidationMessages.properties取国际化错误消息 -->
 <!-- 通过ConfigurableWebBindingInitializer注册validator: -->
 <bean id="webBindingInitializer"
  class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
  <property name="conversionService" ref="conversionService" />
  <property name="validator" ref="validator" />
 </bean>
</beans>




#/cloudsafer/WebRoot/WEB-INF/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_3_0.xsd"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
 id="WebApp_ID" version="3.0">
 <display-name>Flow</display-name>
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
 
 <context-param>
  <param-name>log4jConfigLocation</param-name>
  <param-value>WEB-INF/classes/config/log4j.properties</param-value>
 </context-param>
 <!-- 定义LOG4J监听器 -->
 <listener>
  <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
 </listener>

 <!-- Spring ApplicationContext配置文件的路径,可使用通配符,多个路径用,号分隔 此参数用于后面的Spring Context 
  Loader -->
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>
   classpath*:config/applicationContext.xml
  </param-value>
 </context-param>
 <!--Spring的ApplicationContext 载入 -->
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 <!--Spring MVC Servlet -->
 <servlet>
  <servlet-name>springServlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath*:config/springMVC-servlet.xml</param-value>
  </init-param>
  <load-on-startup>2</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>springServlet</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>
 <!--Filter 定义 -->
 <!--Character Encoding filter -->
 <filter>
  <filter-name>encodingFilter</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>encodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 <!--Open Entity Manager in View filter -->
 <filter>
  <filter-name>openEntityManagerInViewFilter</filter-name>
  <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
 </filter>
 <filter-mapping>
  <filter-name>openEntityManagerInViewFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 <!-- session超时定义,单位为分钟 -->
 <session-config>
  <session-timeout>20</session-timeout>
 </session-config>
</web-app>

转载于:https://my.oschina.net/nck/blog/402630

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值