BeanCreationException: 无法确定用于数据库类型NONE的嵌入式数据库驱动程序类

12

我正在尝试运行我的程序,但总是会出现这个异常:

Caused by: org.springframework.beans.factory.BeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath.
at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.getDriverClassName(DataSourceProperties.java:137)
at org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$NonEmbeddedConfiguration.dataSource(DataSourceAutoConfiguration.java:117)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
... 45 more

我正在通过Gradle导入所有依赖项:

buildscript {
repositories {
    mavenCentral()
}
dependencies {
    classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.7.RELEASE")
}
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'

jar {
baseName = 'flatify-backend-service'
version =  '0.1.0'
}

repositories {
mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile 'org.springframework.boot:spring-boot-starter-data-jpa'
compile 'org.hibernate:hibernate-core:4.3.6.Final'
compile 'javax.servlet:javax.servlet-api:3.1.0'
compile 'org.javassist:javassist:3.15.0-GA'
compile 'mysql:mysql-connector-java:5.1.31'
compile 'commons-dbcp:commons-dbcp:1.4'
testCompile("junit:junit")
testCompile("org.springframework:spring-test")
}

task wrapper(type: Wrapper) {
gradleVersion = '2.5'
}

你可以看到,我正在添加mysql-connector,这不是应该将驱动程序类添加到我的项目中吗? 我有什么遗漏的吗?

我只加了最后一个异常,因为所有其他异常都是由此引起的。 如果您需要任何其他详细信息,请告诉我。

谢谢

我的Config类:

@Configuration
@EnableTransactionManagement
public class PersistenceJPAConfig {

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(dataSource());
    em.setPackagesToScan(new String[] { "at.flatify.persistance.entity" });

    JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    em.setJpaVendorAdapter(vendorAdapter);
    em.setJpaProperties(additionalProperties());

    return em;
}


@Bean(destroyMethod = "close")
public DataSource dataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql://localhost:3306/flatify");
    dataSource.setUsername("user");
    dataSource.setPassword("password");

    return dataSource;
}

@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory emf){
    JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(emf);

    return transactionManager;
}

@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
    return new PersistenceExceptionTranslationPostProcessor();
}

Properties additionalProperties() {
    Properties properties = new Properties();
    properties.setProperty("hibernate.hbm2ddl.auto", "create-drop");
    properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
    return properties;
}

}

3个回答

0
我曾经遇到过同样的问题,我通过在项目的类路径中添加eclipselink-javax.persistence-2.0 jar文件来解决它。 您可以从下面给出的链接下载它。 或者只需添加以下依赖项。
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>2.0.0</version>
</dependency>

Jar下载链接


0

Spring Boot无法确定使用哪个驱动程序。您需要在任何地方指定以下属性: spring.datasource.driverClassName 还有其他需要指定的属性,请查阅文档。


是的,我有一个 Configuration 类,在那里我指定了所有这些属性。 - Mathias Mahlknecht
你能复制相关属性(包括任何连接URL)吗?这会有所帮助。当然,要用虚拟的凭据替换掉任何真实的凭据。 - Daniel Burrell
看着那个堆栈跟踪,似乎 DataSourceAutoConfiguration 正在起作用,无视你在配置中设置的内容。(因为你正在使用 Spring Boot)你尝试过排除 DataSourceAutoConfiguration.class 吗? 除此之外,如果你想使用 Spring Boot,还需要在 application.properties 文件中明确提供 spring.datasource.driverClassName。仅仅在配置中设置驱动类名是不够的。 - Daniel Burrell
我需要在我的类顶部添加"@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})",是吗?但没有任何变化,仍然出现相同的错误。或者我需要将排除放在其他地方吗? - Mathias Mahlknecht
我已经将application.properties文件添加到我的项目根目录中。虽然我不确定为什么需要它,它不是多余的吗?但无论有没有这个文件,我总是得到相同的异常。 - Mathias Mahlknecht

0

你需要禁用(排除)DataSourceAutoConfiguration或删除你的类PersistenceJPAConfig中关于数据源的配置,并使用application.properties进行配置。

在Spring文档中有一个很好的例子:https://spring.io/guides/gs/accessing-data-mysql/


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接