无法使用spring boot连接到mysql数据库
Posted
技术标签:
【中文标题】无法使用spring boot连接到mysql数据库【英文标题】:cant connect to mysql databse using spring boot 【发布时间】:2020-09-03 03:55:54 【问题描述】:当我在 Postman 中点击 url(http://localhost:8080/pjt/samples) 获取 json 数据时,它显示以下错误。
"timestamp": "2020-05-17T10:54:26.705+0000",
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/pjt/samples"
1)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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>samplepjt</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>samplepjt</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2)型号:
package model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "samples")
public class Sample
private Long id ;
private String name;
private String city;
public Sample(String name, String city)
this.name = name;
this.city = city;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId()
return id;
public void setId(Long id)
this.id = id;
@Column(name = "name", nullable = false)
public String getName()
return name;
public void setName(String name)
this.name = name;
@Column(name = "city", nullable = false)
public String getCity()
return city;
public void setCity(String city)
this.city = city;
@Override
public String toString()
return "Sample [id=" + id + ",name=" + name + ", city=" + city + "]";
3)存储库
package repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import model.Sample;
@Repository
public interface SampleRepository extends JpaRepository<Sample,Long>
4)控制器
package controller;
import java.util.List;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import model.Sample;
import repository.SampleRepository;
@RestController
@RequestMapping("/pjt")
public class SampleController
@Autowired
private SampleRepository sampleRepository;
@PostMapping("/samples")
public Sample createSample(@Valid @RequestBody Sample sample)
return sampleRepository.save(sample);
@GetMapping("/samples")
public List<Sample> getAllSample()
return sampleRepository.findAll();
5)主函数
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SamplepjtApplication
public static void main(String[] args)
SpringApplication.run(SamplepjtApplication.class, args);
6)application.properties
spring.datasource.url = jdbc:mysql://localhost:3306/sampledb?useSSL=false
spring.datasource.username = root
spring.datasource.password = dali
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto = update
。 ____ _ __ _ _ /\ / ' __ _ ()_ __ __ _ \ \ \ \ ( ( )_ | '_ | '| | ' / ` | \ \ \ \ \/ )| |)| | | | | || (||))))) ' |____| .|| ||| |__, | / / / / =========|_|===============|___/=///_/ :: Spring Boot :: (v2.2.7.RELEASE)
2020-05-17 16:47:56.159 INFO 3688 --- [main] com.example.demo.SamplepjtApplication:未设置活动配置文件,回退到默认配置文件:默认 2020-05-17 16:47:58.395 INFO 3688 --- [main] .s.d.r.c.RepositoryConfigurationDelegate:在默认模式下引导 Spring Data JPA 存储库。 2020-05-17 16:47:58.476 INFO 3688 --- [main] .s.d.r.c.RepositoryConfigurationDelegate:在 49 毫秒内完成 Spring 数据存储库扫描。找到 0 个 JPA 存储库接口。 2020-05-17 16:48:00.119 INFO 3688 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer:Tomcat 使用端口初始化:8081 (http) 2020-05-17 16:48:00.142 INFO 3688 --- [main] o.apache.catalina.core.StandardService:启动服务 [Tomcat] 2020-05-17 16:48:00.143 INFO 3688 --- [main] org.apache.catalina.core.StandardEngine:启动 Servlet 引擎:[Apache Tomcat/9.0.34] 2020-05-17 16:48:00.496 INFO 3688 --- [main] o.a.c.c.C.[Tomcat].[localhost].[/]:初始化 Spring 嵌入式 WebApplicationContext 2020-05-17 16:48:00.497 INFO 3688 --- [main] os.web.context.ContextLoader:根 WebApplicationContext:初始化在 4199 毫秒内完成 2020-05-17 16:48:00.980 INFO 3688 --- [main] o.hibernate.jpa.internal.util.LogHelper:HHH000204:处理 PersistenceUnitInfo [名称:默认值] 2020-05-17 16:48:01.156 INFO 3688 --- [main] org.hibernate.Version : HHH000412: Hibernate ORM 核心版本 5.4.15.Final 2020-05-17 16:48:01.579 INFO 3688 --- [main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations 5.1.0.Final 2020-05-17 16:48:01.898 INFO 3688 --- [main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - 开始... 2020-05-17 16:48:02.714 INFO 3688 --- [main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - 开始完成。 2020-05-17 16:48:02.771 INFO 3688 --- [main] org.hibernate.dialect.Dialect:HHH000400:使用方言:org.hibernate.dialect.MySQL5InnoDBDialect 2020-05-17 16:48:03.635 INFO 3688 --- [main] o.h.e.t.j.p.i.JtaPlatformInitiator:HHH000490:使用 JtaPlatform 实现:[org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform] 2020-05-17 16:48:03.660 INFO 3688 --- [main] j.LocalContainerEntityManagerFactoryBean:为持久性单元“默认”初始化了 JPA EntityManagerFactory 2020-05-17 16:48:04.042 WARN 3688 --- [main] JpaBaseConfiguration$JpaWebConfiguration:spring.jpa.open-in-view 默认启用。因此,可以在视图渲染期间执行数据库查询。显式配置 spring.jpa.open-in-view 以禁用此警告 2020-05-17 16:48:04.388 INFO 3688 --- [main] os.s.concurrent.ThreadPoolTaskExecutor:初始化 ExecutorService 'applicationTaskExecutor' 2020-05-17 16:48:04.845 INFO 3688 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer:Tomcat 在端口:8081 (http) 上启动,上下文路径为“” 2020-05-17 16:48:04.849 INFO 3688 --- [main] com.example.demo.SamplepjtApplication:在 9.906 秒内启动 SamplepjtApplication(JVM 运行时间为 12.757) 2020-05-17 16:48:13.367 INFO 3688 --- [nio-8081-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/]:初始化 Spring DispatcherServlet 'dispatcherServlet' 2020-05-17 16:48:13.369 INFO 3688 --- [nio-8081-exec-2] os.web.servlet.DispatcherServlet:初始化 Servlet 'dispatcherServlet' 2020-05-17 16:48:13.395 INFO 3688 --- [nio-8081-exec-2] os.web.servlet.DispatcherServlet:在 25 毫秒内完成初始化
谁能帮我解决这个问题?
【问题讨论】:
ComponentScan
不会接您的任何课程。移动根包 com.example.demo
下的类/包。例如:com.example.demo.controller
。 @SpringBootApplication
注解会以当前包为根包进行组件扫描,除非另有说明。
或者您可以在@ComponentScan(basePackageClasses = model.Sample.class, repository.SampleRepository.clas, ...) 中添加所有这些bean。但这不是一个好的解决方案,项目层次结构要好得多。
【参考方案1】:
您必须将SampleController.java
放入com.example.demo.controller
和SampleRepository.java
放入com.example.demo.repository
。或者所有文件都应该在com.example.demo
文件夹中。
因为@SpringBootApplication
必须扫描所有组件。阅读此文档以更好地理解https://docs.spring.io/spring-boot/docs/2.0.0.RELEASE/reference/html/using-boot-structuring-your-code.html。
【讨论】:
以上是关于无法使用spring boot连接到mysql数据库的主要内容,如果未能解决你的问题,请参考以下文章
Docker MySQL - 无法从 Spring Boot 应用程序连接到 MySQL 数据库
无法从 Spring Boot Docker 容器连接到本地 MySQL 数据库服务器
无法从Spring Boot Docker容器连接到本地MySQL数据库服务器
无法将 spring-boot 2 服务连接到不同容器中的 mysql