Spring 启动无法连接到 MySql

Spring Boot can't connect to MySql

这是我使用 Spring Boot 进行的第一次练习,这是我的 application.properties:

spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto=update
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc.mysql://localhost:3306/notedb
spring.datasource.username=root
spring.datasource.password=*******

这些是我的 classes:

  1. DemoMySqlApplication.java

    包 com.example.demomysql;

    进口org.springframework.boot.SpringApplication; 导入 org.springframework.boot.autoconfigure.SpringBootApplication; 导入 org.springframework.boot.autoconfigure.domain.EntityScan; 导入 org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; 导入 org.springframework.context.annotation.ComponentScan; 导入 org.springframework.data.jpa.repository.config.EnableJpaRepositories;

    @SpringBootApplication(排除={DataSourceAutoConfiguration.class})

    @ComponentScan(basePackages={"com.joydeep.springboot"}) public class DemoMysqlApplication {

     public static void main(String[] args) {
         SpringApplication.run(DemoMysqlApplication.class, args);
    
    
     }
    
  2. NoteRepository.java(接口):

package com.example.demomysql;
    
    import org.springframework.data.repository.CrudRepository;
     
    public interface NoteRepository extends CrudRepository <Note, Integer>{
    
    }
  1. NoteController.java
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    
       @RestController
        @RequestMapping(value="/note")
        public class NoteController {
            
            
            @Autowired
            private NoteRepository noteRepository;
            
            @GetMapping(value="/all")
            public String getAllNotes(Model model) {
                model.addAttribute("notes", noteRepository.findAll());
                return "list";
            }
            
            @GetMapping(value="/debug")
            public @ResponseBody Iterable<Note> getNotes(){
                return noteRepository.findAll();
            }
            
        }
  1. Note.java(实体class)
package com.example.demomysql;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import org.springframework.data.repository.CrudRepository;

@Entity
public class Note {
    
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer id;
    
    private String title;
    private String description;
    private Boolean done;
    
    public Integer getId() {
        return id;
    }
    
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public Boolean getDone() {
        return done;
    }
    public void setDone(Boolean done) {
        this.done = done;
    }
    

}

在控制台中,我没有看到错误。 Tomcat 正常启动。这是最后两个信息:

Tomcat started on port(s): 8080 (http) with context path ''

Started DemoMysqlApplication in 0.731 seconds (JVM running for 480.726)

但是在我的 MySQL 数据库上(在启动此应用程序之前,我创建了一个名为“notedb”的数据库和一个名为“note”的 table)。我有一行记录了数据。但是当我尝试连接到: http://localhost:8080/note/debug 我看到了:

我想我的控制器有问题 class。 拜托,你能帮帮我吗?

谢谢

pring.jpa.hibernate.ddl-auto=update
spring.datasource.platform=mysql
spring.datasource.url=jdbc:mysql://localhost:3306/notedb?createDatabaseIfNotExist=true
spring.datasource.username=root
spring.datasource.password=****
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect

  • 删除

    • exclude={DataSourceAutoConfiguration.class}

    • @ComponentScan(basePackages={"com.joydeep.springboot"})

  • 保留@RestController删除@ResponseBody

  • 对于@Controller保持@ResponseBodyResponseEntity<T>

  • 将 return 类型 Iterable<Note> 更改为 List<Note>

申请

@SpringBootApplication
public class DemoMysqlApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoMysqlApplication.class, args);
    }

休息控制器

    @RestController
    @RequestMapping(value="/note")
    public class NoteController {

        @Autowired
        private NoteRepository noteRepository;

        @GetMapping(value="/debug")
        public List<Note> getNotes(){
            return noteRepository.findAll();
        }

    }

控制器

@Controller
    @RequestMapping(value="/note")
    public class NoteController {

        @Autowired
        private NoteRepository noteRepository;

        @GetMapping(value="/debug")
        public ResponseEntity<List<Note>> getNotes(){
             return new ResponseEntity<List<Note>>(noteRepository.findAll(),HttpStatus.OK);
        }

    }