gpt4 book ai didi

java - SpringBoot数据库嵌入式数据库驱动JAVA异常

转载 作者:行者123 更新时间:2023-11-29 19:21:57 25 4
gpt4 key购买 nike

当我尝试启动我的 Spring Boot 应用程序时,出现此异常。我添加了 mysql-connector-java 和 spring-boot-starter-jdbc 依赖项,但不知道什么不起作用。

Description:

Cannot determine embedded database driver class for database type NONE

Action:

If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active).

编辑 1这是我的 Maven 配置文件。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.springframework</groupId>
<artifactId>test</artifactId>
<version>0.1.0</version>

<properties>
<java.version>1.8</java.version>
</properties>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
</project>

编辑2这是数据源配置

public class DataSourceConfig {

@Bean
public DataSource getDataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/hcharts");
dataSource.setUsername("root");
dataSource.setPassword("root");
return dataSource;
}

@Bean
public JdbcTemplate jdbcTemplate() {
return new JdbcTemplate(getDataSource());
}

}

最佳答案

您的 DatasourceConfig.java 文件中似乎缺少 @Configuration 注释。

我尝试使用注释,mvn spring-boot:run 启动正常。如果没有注释,我会得到您指示的文本,后跟一个巨大的堆栈跟踪:

APPLICATION FAILED TO START > [26/818]


Description:

Cannot determine embedded database driver class for database type NONE

Action:

If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may ne$ d to active it (no profiles are currently active).

这是我的my/DatasourceConfig.java:

package my.config;

import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

@Configuration
public class DatasourceConfig {

@Bean
public DataSource getDataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/hcharts");
dataSource.setUsername("root");
dataSource.setPassword("root");
return dataSource;
}

@Bean
public JdbcTemplate jdbcTemplate() {
return new JdbcTemplate(getDataSource());
}

}

如果您添加对已知嵌入式数据库(如 hsqldb)的依赖项,应用程序也将在没有 @Configuration 的情况下启动:

<dependencies>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
</dependency>
...
</dependencies>

关于java - SpringBoot数据库嵌入式数据库驱动JAVA异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42388816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com