当前位置: 首页 > 知识库问答 >
问题:

无法使用spring boot连接到mysql数据库

饶德元
2023-03-14

当我在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 +  "]";
    }
}
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>{

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

}
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引导::(V2.2.7.发布)

有人能帮我解决这个问题吗?

共有1个答案

沈德寿
2023-03-14

您必须将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数据库。然而,当我试图运行它时,它显示出错误。 我如何解决这个问题? 错误 从我的文件中添加代码片段 pom。xml 应用属性 堆栈跟踪 我还没有在sql中手动创建表,因为我认为spring.jpa.hibernate.ddl-Auto=date应该这样做

  • 我无法使用客户机SquirrelSQL连接到MySQL。我以前曾设法连接到Oracle和Derby,但这次,我不知道我做错了什么。 我已经在我的Mac上安装了MySQL,以下步骤: > 要确保安装安全,请执行以下操作: 要创建新数据库: 要知道数据库存储在哪里: 创建一个表 在我遵循这些步骤之后: MySQL安装在/usr/local/ceral/MySQL/5.6.17下 在SQuirreL中,

  • 我设法将我的应用程序与HSQLDB连接起来,并使其运行良好。然而,我在将它连接到MySQL时遇到了一些麻烦。 下面是我的堆栈跟踪: 应用程序运行失败 null 我的pom.xml文件: null 这里是我的application.properties文件: 如果我没有错的话,我将MySQL用户配置为与我在ubuntu中的系统用户相同,这样我就可以用“mysql-u thalysmg”启动MySQL

  • 我创建了一个docker compose文件,将MySql连接到SpringBoot应用程序。但我得到了这个错误: 我尝试在本地机器中使用docker默认ip运行Spring Boot应用程序,同时在这个docker-compose.yml文件中只运行MySql容器,结果运行得非常好。但当我尝试docker编写文件时。我知道这个错误。

  • 我在Spring是全新的,在Spring Hibernate无法连接到我的MySQL数据库。我得到 请求处理失败;嵌套异常为:org.springframework.transaction.CanNotCreateTransactionException: 无法打开事务的JPA EntityManager;嵌套异常为javax.persistence.persistenceException:or

  • 我创建了一个简单的类来测试与我的localhost数据库的通信,这是我用Mysql Workbench创建的。Mysql服务器正在运行。JDBC驱动程序被添加到我的项目的类路径中。 当我运行程序时,我得到以下异常: 线程“main”com.mysql.cj.jdbc.Exceptions.CommunicationsException异常:通信链接失败 最后一个成功发送到服务器的数据包是在0毫秒前