【问题标题】:Spring-Boot and MySql unable to connectSpring-Boot 和 MySql 无法连接
【发布时间】:2017-08-02 15:39:43
【问题描述】:

我正在尝试连接到 mysql 数据库,但它无法正常工作。我从 udemy 上了一门课程并遵循了所有步骤,但我总是遇到同样的错误,然后我遵循了 thisthis 指南中的步骤,但不起作用。

到目前为止我尝试过的事情:

  • 新的 IDE
  • 清理并重建项目
  • application.properties 中删除属性spring.datasource.driverClassName = com.mysql.jdbc.Driver
  • 尝试手动添加 Mysql 连接器


我的想法已经用完了。
需要一些帮助

更新: Stacktrace

2017-03-12 19:20:53.388  INFO 7808 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2017-03-12 19:20:53.515  INFO 7808 --- [           main] o.h.e.j.e.i.LobCreatorBuilderImpl        : HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
2017-03-12 19:20:54.046  WARN 7808 --- [           main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
2017-03-12 19:20:54.061  INFO 7808 --- [           main] o.apache.catalina.core.StandardService   : Stopping service Tomcat
2017-03-12 19:20:54.106  INFO 7808 --- [           main] utoConfigurationReportLoggingInitializer : 

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-03-12 19:20:54.130 ERROR 7808 --- [           main] o.s.boot.SpringApplication               : Application startup failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1081) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:856) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.4.5.RELEASE.jar:1.4.5.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762) [spring-boot-1.4.5.RELEASE.jar:1.4.5.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:372) [spring-boot-1.4.5.RELEASE.jar:1.4.5.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-1.4.5.RELEASE.jar:1.4.5.RELEASE]

Packages in eclipse
Application.properties

POM.XML

<?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>com.stopCozi</groupId>
    <artifactId>stopCozi</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>StopCozi</name>
    <description>Stop Cozi </description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.5.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-web</artifactId>
        </dependency>

        <!-- for HTML -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

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

        <!-- Dependecy for database -->


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

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


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

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

用户实体

@Entity 
public class User {

    private String name;
    private String surname;
    private String password;
    @Id
    @GeneratedValue (strategy= GenerationType.AUTO)
    private String userId;
    private String email;
    private String phoneNo;
    private String PIN;
    private List<Appointment> appointmentList;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    //getter and setter

UserDaoRepository

@RepositoryRestResource(collectionResourceRel = "user", path = "user")
public interface UserDaoRepository extends JpaRepository<User, Long>{

    User findByName(@Param("name") String name);
    User findByEmail(@Param("email") String email);

}

【问题讨论】:

  • 都是异常堆栈跟踪吗?我没有看到根本原因。
  • 使用新错误更新。

标签: java mysql eclipse spring-boot


【解决方案1】:

尝试将其添加到您的 maven 项目文件中:

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

您的 application.properties 文件只是告诉 spring 使用什么 jpa 提供程序。您仍然必须包含 jdbc 提供程序实现文件。

【讨论】:

  • 帮助我摆脱那个错误,谢谢 :) 知道为什么我会收到新错误吗?无法构建 Hibernate SessionFactory。 (我会开始寻找答案)
【解决方案2】:

您需要为此字段定义 ORM 关联:

private List<Appointment> appointmentList;

例如,如果User 可能有多个Appointment,而Appointment 可能与零或一个User 相关,您可以在Appointment 实体中以这种方式定义Appointment 关系:

@Entity      
public class User{
  ...
  @OneToMany(mappedBy="user")
  private List<Appointment> appointmentList;
}

您可以在 Appointment 实体中定义这种关系:

@Entity      
public class Appointment{
  ...
  @ManyToOne
  private User user;
 ...
 }

如果您遇到其他映射错误,建议您了解 Hibernate/JPA 基础知识。

这里是 Hibernate (v 5) 的实际文档:http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html

这是关于实体之间关系定义的文档部分:

http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#associations

【讨论】:

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