无法将mysql连接到spring启动项目

时间:2018-03-29 00:00:27

标签: mysql spring spring-boot spring-data-jpa spring-tool-suite

我正在按照此https://spring.io/guides/gs/accessing-data-mysql/指南将mysql数据库连接到spring启动项目

但是在运行应用程序时遇到以下错误,我正在生成spring starter项目,只在通过spring工具套件创建项目时选择web,mysql和jpa框

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class HttpSessionConfig {

    private static final Map<String, HttpSession> sessions = new HashMap<>();

    public List<HttpSession> getActiveSessions() {
        return new ArrayList<>(sessions.values());
    }

    @Bean
    public HttpSessionListener httpSessionListener() {
        return new HttpSessionListener() {
            @Override
            public void sessionCreated(HttpSessionEvent hse) {
                sessions.put(hse.getSession().getId(), hse.getSession());
            }

            @Override
            public void sessionDestroyed(HttpSessionEvent hse) {
                sessions.remove(hse.getSession().getId());
            }
        };
    }
} 

以下是application.properties

2018-03-28 16:48:42.125 ERROR 15452 --- [           main] com.zaxxer.hikari.HikariConfig           : Failed to load driver class com.mysql.jdbc.Driver from HikariConfig class classloader jdk.internal.loader.ClassLoaders$AppClassLoader@782830e
2018-03-28 16:48:42.128  WARN 15452 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under '' to com.zaxxer.hikari.HikariDataSource
2018-03-28 16:48:42.130  INFO 15452 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2018-03-28 16:48:42.142  INFO 15452 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-03-28 16:48:42.143 ERROR 15452 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to bind properties under '' to com.zaxxer.hikari.HikariDataSource:

    Property: driverclassname
    Value: com.mysql.jdbc.Driver
    Origin: "driverClassName" from property source "source"
    Reason: Unable to set value for property driver-class-name

Action:

Update your application's configuration

和pom.xml

spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://localhost:3306/world
spring.datasource.username=root
spring.datasource.password=admin

编辑:在application.properties中添加了spring.datasource.driver-class-name = com.mysql.jdbc.Driver,错误仍然存​​在

<?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>proj.mine</groupId>
    <artifactId>training-app-2</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>training-app-2</name>
    <description>traning practive app</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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


</project>

6 个答案:

答案 0 :(得分:11)

尝试添加mysql连接器驱动程序版本并从以下位置删除范围标记:

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

答案 1 :(得分:3)

似乎应用程序可能没有使用正确的类路径运行,因为它似乎很难找到mysql驱动程序。您是否尝试通过IDE运行应用程序?

尝试使用项目目录中的命令行通过maven运行应用程序:

  

./ mvnw spring-boot:run

如果这样可行则会导致IDE出现问题,因为某些原因它在运行应用程序时没有提供正确的类路径。我不熟悉STS,但在IntelliJ中有一些选项,比如&#34; auto import pom&#34;保持IDE的项目视图与pom同步。

答案 2 :(得分:3)

仅供参考,当我将Spring Boot应用程序从H2更改为mySQL时,遇到了类似的问题。

我正在使用Gradle(而不是Maven)。我也正在使用STS。

将它添加到build.gradle文件后,我只是忘记运行Refresh Gradle Project

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    ...
    // Use MySQL Connector-J
    compile 'mysql:mysql-connector-java'

在执行“刷新Gradle项目”并重建/重新启动我的应用程序后,问题消失了。

答案 3 :(得分:0)

也许您缺少MySQL连接器依赖项,所以将其添加到pom.xml文件的依赖项标签中,然后再次运行该项目。它可以解决您的问题。

    `<dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency> `

答案 4 :(得分:0)

使用数据库版本和数据库更新pom.xml文件:

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

答案 5 :(得分:0)

com.zaxxer.hikari。 HikariDataSource 需要 spring-boot-starter-data-jdbc 依赖项,因此请将其添加到您的 pom.xml 中>:

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