【问题标题】:Spring Boot can not connect to MysqlSpring Boot无法连接Mysql
【发布时间】:2020-12-15 15:55:40
【问题描述】:

使用 spring boot 连接数据库有问题

我是已经在工作的开发人员的续集。我已经获得了源代码,但我无法使用 IntelliJ 启动 Spring Boot Java 项目。我为我的数据库使用服务器 Xampp。但是我有以下错误

     :: Spring Boot ::        (v2.3.3.RELEASE)

    2020-12-15 16:39:28.411  INFO 2384 --- [           main] c.a.myapp.myappApplication  : Starting myappApplication on DESKTOP-DRP2JSE with PID 2384 (C:\Users\Admin\Downloads\myapp 2.0 Final\myappBack 2.0 - Final\myappBack\out\production\myappBack started by Admin in C:\Users\Admin\Downloads\myapp 2.0 Final\myappBack 2.0 - Final\myappBack)
    2020-12-15 16:39:28.415  INFO 2384 --- [           main] c.a.myapp.myappApplication  : No active profile set, falling back to default profiles: default
    2020-12-15 16:39:29.382  INFO 2384 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFERRED mode.
    2020-12-15 16:39:29.496  INFO 2384 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 108ms. Found 8 JPA repository interfaces.
    2020-12-15 16:39:30.032  INFO 2384 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
    2020-12-15 16:39:30.040  INFO 2384 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
    2020-12-15 16:39:30.040  INFO 2384 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.37]
    2020-12-15 16:39:30.396  INFO 2384 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
    2020-12-15 16:39:30.396  INFO 2384 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1922 ms
    2020-12-15 16:39:30.498  WARN 2384 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is 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.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
    2020-12-15 16:39:30.500  INFO 2384 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
    2020-12-15 16:39:30.513  INFO 2384 --- [           main] ConditionEvaluationReportLoggingListener : 

    Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
    2020-12-15 16:39:30.519 ERROR 2384 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

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

    Description:

    Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

    Reason: Failed to determine a suitable driver class


    Action:

    Consider the following:
        If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
        If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).


    Process finished with exit code 1
    server.port=8081

    server.servlet.session.timeout=1200
    # JDBC URL of the database.
    spring.datasource.url=jdbc:mysql://localhost:3306/myapp?zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=UTC
    # Login username of the database.
    spring.datasource.username= root
    # Login password of the database.
    spring.datasource.password=
    # Fully qualified name of the JDBC driver. Auto-detected based on the URL by default.
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    # Whether to enable logging of SQL statements.
    spring.jpa.show-sql=true
    # DDL mode. This is actually a shortcut for the "hibernate.hbm2ddl.auto" property. Defaults to "create-drop" when using an embedded database and no schema manager was detected. Otherwise, defaults to "none".
    spring.jpa.hibernate.ddl-auto=update
    # Additional native properties to set on the JPA provider.
    spring.jpa.database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
    spring.jpa.properties.hibernate.storage.storage_engine=innodb



    # Avoid to restart server (only during dev phase) if DevTools are uninstall
    # spring.thymeleaf.cache=false
    # spring.security.user.name="root"
    # spring.security.user.password="123"
    spring.resources.add-mappings=true

【问题讨论】:

  • 你没有在application.yml文件中指定数据源URL

标签: java mysql spring spring-boot


【解决方案1】:

问题写在Reason: Failed to determine a suitable driver class。类加载器在你的类路径中找不到driver-class-name。可能在 maven / gradle 配置中缺少 MySQL 库定义。

马文:

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

分级:

runtimeOnly 'mysql:mysql-connector-java'

如果存在 MySQL 库,请尝试更改 driver-class-name 的定义,如下所示。

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

...而不是

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

【讨论】:

  • 如果驱动是8版本,CJ可以工作,否则,它应该没有CJ
【解决方案2】:

检查你的 mysql-connector-java 版本 jar 如果是 5 应该是

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

如果是 8 应该是

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

另外我不认为你应该在等号后有空格,你的用户名和密码在哪里

【讨论】:

    【解决方案3】:

    它可以工作,只需强制重新加载maven依赖就可以了;

    只是警告要修复

    SLF4J: Class path contains multiple SLF4J bindings.
    SLF4J: Found binding in [jar:file:/C:/Users/Admin/Downloads/myapp%202.0%20Final/myappBack%202.0%20-%20Final/myappBack/target/myappService/WEB-INF/lib/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
    SLF4J: Found binding in [jar:file:/C:/Users/Admin/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
    SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
    SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]

    【讨论】:

      【解决方案4】:

      尝试从配置文件中删除 spring.datasource.class-name 属性。 url 应该足以说明要使用哪个类名。

      还在属性中设置“logging.level.root=debug”以获得更多详细信息。看看有没有帮助。

      【讨论】:

        猜你喜欢
        • 2019-12-03
        • 2017-07-08
        • 2017-08-02
        • 2020-07-12
        • 2020-07-24
        • 2021-10-23
        • 2018-10-22
        • 1970-01-01
        • 2018-09-07
        相关资源
        最近更新 更多