springmvc + spring data jpa + hibernate 配置
springmvc + spring data jpa + hibernate 配置
stonehome 发表于3年前
springmvc + spring data jpa + hibernate 配置
  • 发表于 3年前
  • 阅读 191
  • 收藏 7
  • 点赞 0
  • 评论 0

新睿云服务器60天免费使用,快来体验!>>>   

摘要: springmvc + spring data jpa + hibernate 配置 工程包百度云地址:http://pan.baidu.com/s/1pJDRATt
#/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>
  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 1
博文 6
码字总数 5148
×
stonehome
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: