我正在尝试使用 spring boot 连接到 MySQL dB。由于 Spring boot 需要的配置非常少,所以我没有弄错。 我的代码结构就像, 我有启动应用程序的主类, 一个模型客户类, 一个网络 Controller 类。 用于实现 CRUD 操作的客户存储库接口(interface)。
public interface CustomerRepository extends CrudRepository<Customer, Long>
{
List<Customer> findByLastName(String lastName);
}
应用程序.properties 文件
spring.datasource.url=jdbc:mysql://localhost:3306/testdb?useSSL=false
spring.datasource.username=root
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=update
我得到的错误是:
ERROR 7740 --- [ main] o.a.tomcat.jdbc.pool.ConnectionPool :
Unable to create initial connections of pool
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could
not create connection to database server,
and very long error messages.
最佳答案
成功了。实际上,我使用的 spring boot 父版本为 1.5.8,这是较旧的版本,而 MYSQL 的最新版本为 8.0.11,这导致了兼容性问题。我只是添加了依赖于 MySQL 的版本属性,它就连接上了。
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
关于java - 无法使用 Spring Boot JPA 连接 MySql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50295389/