2017-03-12 94 views
4

我想連接到mysql數據庫,但它不工作。我採取了從udemy課程,並遵循所有步驟,但我總是有同樣的錯誤,然後我遵循thisthis指南的步驟,但不起作用。Spring-Boot和MySql無法連接

東西,我試過至今:

  • 新的IDE
  • 乾淨&改造項目
  • application.properties
  • 刪除屬性spring.datasource.driverClassName = com.mysql.jdbc.Driver嘗試添加MySQL連接手動


我已經跑出了想法。
需要一些援助

更新:堆棧跟蹤

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

} 
+0

它是所有異常堆棧跟蹤?我沒有看到根本原因。 – davidxxx

+0

用新錯誤更新。 – agata

回答

2

嘗試增加給你的Maven項目文件:

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

你application.properties文件只是告訴春什麼jpa供應商給我們即您仍然必須包含jdbc提供程序實現文件。

+0

幫助我擺脫那個錯誤,謝謝:)任何線索爲什麼我得到新的錯誤?無法構建Hibernate SessionFactory。 (我會開始尋找答案) – agata

1

您需要定義該字段的ORM協會:

private List<Appointment> appointmentList; 

例如,如果一個User可能有多個AppointmentAppointment可能與零個或一個User,您可以定義在Appointment關係這種方式在User實體:

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

而在約會實體您可以定義這種關係船:

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

如果您遇到其他映射錯誤,我建議您瞭解Hibernate/JPA基礎知識。

這裏是休眠的實際文件(V5):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

+0

非常感謝:)它的工作。 – agata