Spring MVC+Hibernate JPA搭建的博客系統項目中所遇到的坑
阿新 • • 發佈:2018-04-20
phoenix min ews googl exc ssl dia one UC
標簽: springmvc hibernate 2016年12月21日 21:48:035133人閱讀 評論(0) 收藏 舉報 分類:
版權聲明:本文為博主原創文章,未經博主允許不得轉載,如需轉載,請註明文章出處為 http://www.54tianzhisheng.cn/,否則考慮法律追究責任,謝謝合作! https://blog.csdn.net/tzs_1041218129/article/details/53791765
目錄(?)[+]
項目代碼地址:https://github.com/zhisheng17/springmvc
如果覺得不錯的話,歡迎給個 star , 如果你想完善這個項目的話,你也可以 fork 後修改然後推送給我。
最近在學習 Spring MVC ,其中在做一個簡單的博客系統demo,是使用 SpringMVC 集成
Spring Data JPA(由 Hibernate JPA 提供),來進行強大的數據庫訪問。結果其中遇到的坑不
是一點點啊,我差點崩潰了,其中最大的原因就是由於 Hibernate JPA 中的bug了,反正一開始
還不知道是這個問題,導致折騰了快一天的時間。想想都可怕啊。
mvc-dispatch-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:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="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.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--指明 controller 所在包,並掃描其中的註解-->
<context:component-scan base-package="cn.zhisheng.controller"/>
<!-- 靜態資源(js、image等)的訪問 -->
<mvc:default-servlet-handler/>
<!-- 開啟註解 -->
<mvc:annotation-driven/>
<!--ViewResolver 視圖解析器-->
<!--用於支持Servlet、JSP視圖解析-->
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 表示JPA Repository所在的包 -->
<jpa:repositories base-package="cn.zhisheng.repository"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="defaultPersistenceUnit"/>
<property name="packagesToScan" value="cn.zhisheng.model" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>
<prop key="hibernate.connection.url">jdbc:mysql://localhost:3306/springdemo?useSSL=false</prop>
<prop key="hibernate.connection.username">root</prop>
<prop key="hibernate.connection.password">root</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.connection.useUnicode">true</prop>
<prop key="hibernate.connection.characterEncoding">UTF-8</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.use_sql_comments">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.connection.autoReconnect">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="connection.autoReconnectForPools">true</prop>
<prop key="connection.is-connection-validation-required">true</prop>
<prop key="hibernate.c3p0.validate">true</prop>
<prop key="hibernate.connection.provider_class">org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider</prop>
<prop key="hibernate.c3p0.min_size">5</prop>
<prop key="hibernate.c3p0.max_size">600</prop>
<prop key="hibernate.c3p0.timeout">1800</prop>
<prop key="hibernate.c3p0.max_statements">50</prop>
<prop key="hibernate.c3p0.preferredTestQuery">SELECT 1;</prop>
<prop key="hibernate.c3p0.testConnectionOnCheckout">true</prop>
<prop key="hibernate.c3p0.idle_test_period">3000</prop>
<prop key="javax.persistence.validation.mode">none</prop>
</props>
</property>
</bean>