将MySQL连接到Spring应用程序

我想使用MySQL数据库,而不是像hsqldb那样使用运行时数据库。 我克隆了这个存储库 ,它使用hsqldb作为其数据库。

因为我想学习如何使用基于rest的spring应用程序的关系数据库。 所以我对pom.xml进行了以下更改:更改了pom.xml:

  4.0.0 org.springsource.restbucks restbucks war 1.0.0.BUILD-SNAPSHOT Spring RESTBucks  org.springframework.boot spring-boot-starter-parent 1.1.5.RELEASE   Evans-RC1 8.0.9     org.springframework.boot spring-boot-starter-data-rest   org.springframework.boot spring-boot-starter-data-jpa   org.hibernate hibernate-entitymanager     org.springframework.boot spring-boot-starter-test    org.jadira.usertype usertype.extended 3.2.0.GA   com.fasterxml.jackson.datatype jackson-datatype-jsr310   org.hibernate hibernate-entitymanager    mysql mysql-connector-java    org.springframework spring-tx-events 1.0.0.BUILD-SNAPSHOT   org.projectlombok lombok 1.14.4 provided   com.jayway.jsonpath json-path      org.apache.maven.plugins maven-compiler-plugin  1.8 1.8    org.springframework.boot spring-boot-maven-plugin      spring-libs-snapshot http://repo.spring.io/libs-snapshot  true      spring-libs-snapshot http://repo.spring.io/libs-snapshot  true     

在application.properties(spring-restbucks / src / main / resources / application.properties)中,我进行了以下更改:

 # JPA spring.jpa.show-sql=true server.port=8080 spring.datasource.url=jdbc:mysql://localhost/restBucks spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.datasource.username=root spring.datasource.password= 

但是我面临着大约15个错误:

 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'orderInitializer' defined in file [/home/jimish/projects/spring_projects/spring/spring-restbucks/target/classes/org/springsource/restbucks/order/OrderInitializer.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springsource.restbucks.order.OrderInitializer]: Constructor threw exception; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet 

因此,如果任何人都可以建议如何将mysql连接到spring应用程序的路径,这将很棒。 谢谢。

在classpath中的application.properties中仅包含以下配置,它将像魅力一样工作:

  spring.datasource.url=jdbc:mysql://localhost/jpa_example spring.datasource.username=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect 

对@M进行了阐述。 Deinum的评论……你需要指定JPA配置信息,因为Spring RestBucks App正在使用Spring Data JPA 。

添加标准JPA属性并指定数据库平台( 公共应用程序属性 )应该可以使JPA连接正常工作。

 spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=create-drop spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect # Enable spring data repos spring.data.jpa.repositories.enabled=true # Replace with your connection string spring.datasource.url=jdbc:mysql://localhost:3306/restbucks # Replace with your credentials spring.datasource.username=sa spring.datasource.password= spring.datasource.driverClassName=com.mysql.jdbc.Driver 

数据库服务器IP,端口号和数据库名称

spring.datasource.url = JDBC:MySQL的://本地主机/样品

数据库服务器用户名

spring.datasource.username =根

数据库服务器密码

spring.datasource.password = root123

每个Time Server启动时更新数据库

spring.jpa.hibernate.ddl-AUTO =更新

将MySQL Connector / J添加到类路径:

  mysql mysql-connector-java 6.0.5  

这是基于Spring的应用程序中的文件application.properties的示例:

 datasource.mine.poolSize=30 spring.datasource.url=jdbc:mysql://127.0.0.1/vy?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=update 

您可以validation您的pom文件,并确保可以使用以下最低限度的jar子

   org.springframework spring-beans   org.springframework spring-webmvc   org.springframework.data spring-data-jpa   org.springframework spring-context    org.springframework spring-aspects   org.hibernate hibernate-entitymanager    mysql mysql-connector-java  **Make sure you are reading your db.properties file from application-context.xml**    classpath:db.properties    **Make sure you use proper datasource and entity manager in application-context.xml**                  **Make sure you are adding minimum of below properties in db.properties** dialect=org.hibernate.dialect.MySQL5InnoDBDialect url=jdbc:mysql://localhost:3306/dbName driver=com.mysql.jdbc.Driver username=root password=test