简体   繁体   English

Spring-Boot和MySql无法连接

[英]Spring-Boot and MySql unable to connect

I am trying to connect to mysql database, but it's not working. 我正在尝试连接到mysql数据库,但它无法正常工作。 I took a course from udemy and followed all steps, but i am getting always the same error, then I followed the steps from this and this guides, but doesn't work. 我选了一门从udemy并执行了所有步骤,但我总是得到同样的错误,然后我跟着从台阶上这个这个导游,但不起作用。

Things i tried so far: 到目前为止我尝试的事情:

  • new IDE 新的IDE
  • clean & rebuild project 清洁和重建项目
  • deleting property spring.datasource.driverClassName = com.mysql.jdbc.Driver from application.properties application.properties删除属性spring.datasource.driverClassName = com.mysql.jdbc.Driver
  • tried to add Mysql Connector manually 试图手动添加Mysql Connector


I've ran out of ideas. 我已经没想完了。
Need some assistance 需要一些帮助

Update: Stacktrace 更新: 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 eclipse中的包
Application.properties Application.properties

POM.XML 的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>

User Entity 用户实体

@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 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);

}

Try adding this to your maven project file: 尝试将此添加到您的maven项目文件:

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

Your application.properties file is just telling spring what jpa provider to use. 您的application.properties文件只是告诉我们要使用的jpa提供程序。 You still have to include the jdbc provider implementation files. 您仍然必须包含jdbc提供程序实现文件。

You need to define the ORM association for this field : 您需要为此字段定义ORM关联:

private List<Appointment> appointmentList;

For example if a User may have multiple Appointment and an Appointment may be related to zero or one User , you could define the Appointment relationship in this way in the User entity : 例如,如果User可能有多个Appointment ,并且Appointment可能与零个或一个User ,则可以在User实体中以这种方式定义Appointment关系:

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

And in the Appointment entity you could define this relationship : 在约会实体中,您可以定义此关系:

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

If you encounter other mapping errors, I advise you to understand the Hibernate/JPA basics. 如果您遇到其他映射错误,我建议您了解Hibernate / JPA基础知识。

Here is the actual documentation of Hibernate (v 5): http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html 以下是Hibernate的实际文档(第5版): http//docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html

Here is the documentation part on the relationships definition between entities : 以下是实体之间关系定义的文档部分:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM