配置 Spring JPA 连接 MySQL

Posted

技术标签:

【中文标题】配置 Spring JPA 连接 MySQL【英文标题】:Configurate Spring JPA to connect MySQL 【发布时间】:2018-11-21 17:50:32 【问题描述】:

我有一个使用 Jetty 启动的 spring 应用程序。

bean 配置:

   <context:component-scan base-package="com.mypackage"></context:component-scan>
    <mvc:annotation-driven/>
     <tx:annotation-driven />
      <jpa:repositories base-package="com.mypackage.repository" />
    <bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/mydb?useSSL=false" />
    <property name="username" value="root" />
    <property name="password" value="123456" />
    </bean>
  <bean id="hibernateJpaVendorAdapter" 
    class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
  </bean>
  <bean id="entityManagerFactory" 
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/>
    <!-- Set JPA properties -->
            <property name="loadTimeWeaver">
            <bean
    class="org.springframework.instrument.classloading.SimpleLoadTimeWeaver" />
        </property>
    <property name="jpaProperties">
      <props>
        <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
        <prop key="javax.persistence.schemageneration.database.action">none</prop>
        <prop key="hibernate.ejb.use_class_enhancer">true</prop>
      </props>
    </property>

    <property name="packagesToScan" value="com.openstack.dashboard.domain"/>

    <property name="sharedCacheMode" value="ENABLE_SELECTIVE"/>

    <property name="validationMode" value="NONE"/>
  </bean>
  <bean id="transactionManager" 
    class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
  </bean>

启动应用程序时出现此错误:

原因:org.hibernate.service.spi.ServiceException:无法创建请求的服务 [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] 在 org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:264) 在 org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:228) 在 org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:207) 在 org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51) 在 org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94) 在 org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:237) 在 org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:207) 在 org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:352) 在 org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:111) 在 org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:847) 在 org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:874) 在 org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60) 在 org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:343) 在 org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574) ... 67 更多 原因:org.hibernate.HibernateException:无法构造请求的方言 [org.hibernate.dialect.MySQL5Dialect] 在 org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.constructDialect(DialectFactoryImpl.java:84) 在 org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:51) 在 org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:137) 在 org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35) 在 org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88) 在 org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:254) ... 82 更多 引起:java.lang.ClassCastException:org.hibernate.dialect.MySQL5Dialect 不能转换为 org.hibernate.dialect.Dialect 在 org.hibernate

依赖信息:

    <dependencies>
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>       
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>$spring.version</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>$spring.version</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>$spring.version</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-orm</artifactId>
    <version>$spring.version</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>$spring.version</version>
</dependency>
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-core</artifactId>
    <version>$spring.version</version>
</dependency>
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-web</artifactId>
    <version>$spring.version</version>
</dependency>
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-web</artifactId>
    <version>$spring.version</version>
</dependency>
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-config</artifactId>
    <version>$spring.version</version>
</dependency>
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-ldap</artifactId>
    <version>$spring.version</version>
</dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
                <version>1.5.2.RELEASE</version>

    </dependency>
    <dependency>
        <groupId>commons-net</groupId>
        <artifactId>commons-net</artifactId>
        <version>2.0</version>
    </dependency>
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20160810</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-exec</artifactId>
        <version>1.0.1</version>
    </dependency>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.4</version>
    </dependency>
    <dependency>
        <groupId>com.googlecode.json-simple</groupId>
        <artifactId>json-simple</artifactId>
        <version>1.1</version>
    </dependency>

            <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.2.5</version>
    </dependency>
            <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.13</version>
    </dependency>
            <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.0.5</version>
        <scope>runtime</scope>
    </dependency>
</dependencies>

我是 Spring 新手。感谢大家的支持!

【问题讨论】:

【参考方案1】:

您没有在 pom.xml 文件中放置 Hibernate 依赖项:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>5.3.1.Final</version>
</dependency>

检查哪个版本的 Hibernate 可以满足您的需要并且可以工作(取决于您使用的 Spring 版本)。

另外你这里有错字:

<prop key="javax.persistence.schemageneration.database.action">none</prop>

应该是这样的:

<prop key="javax.persistence.schema-generation.database.action">none</prop>

【讨论】:

感谢您的回复!我试过你的建议并得到新的错误:NoSuchMethodError: org.hibernate.boot.model.process.spi.MetadataBuildingProcess.prepare 我认为它是由错误的 Hibernate 版本引起的。尝试使用较旧的版本,例如 5.1.5。您也可以检查其他依赖项(版本)。我检查了你的配置,应该没问题。您可以尝试使用 @Transactional 和 application.properties 使用注解配置。 Check this 也发布并尝试使用那里的依赖项 - spring boot、data jpa 和 starter。

以上是关于配置 Spring JPA 连接 MySQL的主要内容,如果未能解决你的问题,请参考以下文章

两小时上手Spring Boot (开发红包程序———通过JPA 连接Mysql ,事务等内容) 遇到的问题

何时使用 Spring JPA (Hibernate) Entity Manager 将连接返回到连接池?

Spring boot jpa mysql 连接数据库SSL错误

Spring Boot JPA 连接数据库

spring boot JPA 和 数据库连接

为啥 Spring JPA Mysql 数据连接可以在 IntelliJ 中正确初始化,但不能在命令行中初始化?