spring-mvc和hibernate与mysql数据库集成

时间:2018-06-09 17:37:09

标签: mysql spring hibernate spring-mvc

我是spring和hibernate的新手,我正在尝试从spring + hibernate应用程序连接mysql数据库。但不幸的是我遇到了错误。试图解决这个问题,但没有得到正确的解决方案。

Spring - Hibernate版本信息

<properties>
    <spring.version>4.2.1.RELEASE</spring.version>
    <security.version>4.0.3.RELEASE</security.version>
    <jdk.version>1.8</jdk.version>
    <hibernate.version>4.3.5.Final</hibernate.version>
    <org.aspectj-version>1.7.4</org.aspectj-version>
</properties>

<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <beans:property name="url"
        value="jdbc:mysql://localhost:3306/springmvchibernate"/>
    <beans:property name="username" value="root" />
    <beans:property name="password" value="mysql12345" />
</beans:bean>

<!-- Hibernate 4 SessionFactory Bean definition -->
<beans:bean id="hibernate4AnnotatedSessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <beans:property name="dataSource" ref="dataSource" />
    <beans:property name="annotatedClasses">
        <beans:list>
            <beans:value>**</beans:value>
        </beans:list>
    </beans:property>
    <beans:property name="hibernateProperties">
        <beans:props>
            <beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
            </beans:prop>
            <beans:prop key="hibernate.show_sql">true</beans:prop>
        </beans:props>
    </beans:property>
</beans:bean>

<context:component-scan base-package="org.**" />

<tx:annotation-driven transaction-manager="transactionManager" />

<beans:bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <beans:property name="sessionFactory"
        ref="hibernate4AnnotatedSessionFactory" />
</beans:bean>

消息请求处理失败;嵌套异常是org.springframework.transaction.CannotCreateTransactionException:无法打开Hibernate Session进行事务处理;嵌套异常是org.hibernate.exception.GenericJDBCException:无法打开连接

描述服务器遇到意外情况,导致无法完成请求。

异常

org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是org.springframework.transaction.CannotCreateTransactionException:无法打开Hibernate Session进行事务处理;嵌套异常是org.hibernate.exception.GenericJDBCException:无法打开连接     org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)     org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)     javax.servlet.http.HttpServlet.service(HttpServlet.java:635)     org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)     javax.servlet.http.HttpServlet.service(HttpServlet.java:742)     org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 根本原因

org.springframework.transaction.CannotCreateTransactionException:无法打开Hibernate Session进行事务处理;嵌套异常是org.hibernate.exception.GenericJDBCException:无法打开连接     org.springframework.orm.hibernate4.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:544)     org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:373)     org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:427)     org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:276)     org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)     org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)     org.springframework.aop.framework.CglibAopProxy $ DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)     org.arpit.java2blog.service.CountryService $$ EnhancerBySpringCGLIB $$ 77d6eaf4.getAllCountries()     org.arpit.java2blog.controller.CountryController.getCountries(CountryController.java:26)     sun.reflect.NativeMethodAccessorImpl.invoke0(原生方法)     sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     java.lang.reflect.Method.invoke(Method.java:498)

1 个答案:

答案 0 :(得分:1)

我得到了这个问题的解决方案我已经尝试了很多选项改变了hibernate版本,spring版本尝试了每个可能的选项来解决这个问题。经过几个小时的奋斗,我终于发现它是由于mySQL连接器版本从5.1。**变为8.0.11。它工作正常。即使在官方他们提到下面的价值观,他们也没有工作。

Connector / J version驱动程序类型JDBC版本MySQL服务器版本状态 5.1 4 3.0,4.0,4.1,4.2 5.5,5.6 *,5.7 *,8.0 *推荐版本 8.0 4 4.2 5.5,5.6,5.7,8.0发展里程碑