无法使用Spring Boot连接到mysql数据库

When I am hitting url(http://localhost:8080/pjt/samples) in Postman for json data,it shows the following error.

{
    "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信息3688 --- [main] com.example.demo.SamplepjtApplication:未设置活动配置文件,回退到默认配置文件:默认 2020-05-17 16:47:58.395信息3688-[[main] .s.d.r.c.RepositoryConfigurationDelegate:以DEFAULT模式引导Spring Data JPA存储库。 2020-05-17 16:47:58.476信息3688--[main] .s.d.r.c.RepositoryConfigurationDelegate:完成Spring数据存储库的扫描时间为49ms。找到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信息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信息3688 --- [main] o.a.c.c.C. [Tomcat]。[localhost]。[/]:初始化Spring嵌入式WebApplicationContext 2020-05-17 16:48:00.497信息3688-[[main] o.s.web.context.ContextLoader:根WebApplicationContext:初始化在4199毫秒内完成 2020-05-17 16:48:00.980信息3688-[[主] o.hibernate.jpa.internal.util.LogHelper:HHH000204:处理PersistenceUnitInfo [名称:默认] 2020-05-17 16:48:01.156信息3688 --- [main] org.hibernate.Version:HHH000412:Hibernate ORM核心版本5.4.15.Final 2020-05-17 16:48:01.579信息3688 --- [main] o.hibernate.annotations.common.Version:HCANN000001:Hibernate Commons注释{5.1.0.Final} 2020-05-17 16:48:01.898信息3688-[[主] com.zaxxer.hikari.HikariDataSource:HikariPool-1-正在启动... 2020-05-17 16:48:02.714信息3688-[main] com.zaxxer.hikari.HikariDataSource:HikariPool-1-开始完成。 2020-05-17 16:48:02.771信息3688--[main] org.hibernate.dialect.Dialect:HHH000400:使用方言:org.hibernate.dialect.MySQL5InnoDBDialect 2020-05-17 16:48:03.635信息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信息3688-[主] 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信息3688 --- [main] o.s.s.concurrent.ThreadPoolTask​​Executor:正在初始化ExecutorService'applicationTaskExecutor' 2020-05-17 16:48:04.845信息3688 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer:Tomcat在具有上下文路径的端口:8081(http)上启动 2020-05-17 16:48:04.849信息3688-[[main] com.example.demo.SamplepjtApplication:在9.906秒内启动SamplepjtApplication(JVM运行12.757) 2020-05-17 16:48:13.367信息3688-[nio-8081-exec-2] o.a.c.c.C. [Tomcat]。[localhost]。[/]:初始化Spring DispatcherServlet'dispatcherServlet' 2020-05-17 16:48:13.369信息3688-[nio-8081-exec-2] o.s.web.servlet.DispatcherServlet:初始化Servlet'dispatcherServlet' 2020-05-17 16:48:13.395信息3688-[nio-8081-exec-2] o.s.web.servlet.DispatcherServlet:25毫秒内完成初始化

谁能帮我解决此问题?

评论