简体   繁体   English

Spring Boot 无法连接到 MySql

[英]Spring Boot can't connect to MySql

this is my first exercise with Spring Boot and this is my application.properties:这是我第一次使用 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=*******

These are my classes:这些是我的课程:

  1. DemoMySqlApplication.java DemoMySqlApplication.java

    package com.example.demomysql;包 com.example.demomysql;

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

    @SpringBootApplication(exclude={DataSourceAutoConfiguration.class}) @SpringBootApplication(exclude={DataSourceAutoConfiguration.class})

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

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

package com.example.demomysql;
    
    import org.springframework.data.repository.CrudRepository;
     
    public interface NoteRepository extends CrudRepository <Note, Integer>{
    
    }
  1. NoteController.java 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 (Entity class) Note.java(实体类)
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;
    }
    

}

From the console, I don't see errors.从控制台,我没有看到错误。 Tomcat starts normally. Tomcat 正常启动。 These are the last two information:这是最后两个信息:

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

Started DemoMysqlApplication in 0.731 seconds (JVM running for 480.726)

But on my MySQL database (I created a DB named "notedb" and a table named "note", before to launch this application).但是在我的 MySQL 数据库上(在启动此应用程序之前,我创建了一个名为“notedb”的数据库和一个名为“note”的表)。 I have one row with the data in note.我有一行注释中的数据。 But when I try to connect to: http://localhost:8080/note/debug I see:但是当我尝试连接到: http://localhost:8080/note/debug我看到:

在此处输入图片说明

I think I have a problem with Controller class.我想我的 Controller 类有问题。 Please, can you help me?拜托,你能帮我吗?

Thanks谢谢

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

  • Remove消除

    • exclude={DataSourceAutoConfiguration.class}

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

  • Keep @RestController remove @ResponseBody保持@RestController删除@ResponseBody

  • For @Controller keep @ResponseBody or ResponseEntity<T>对于@Controller保持@ResponseBodyResponseEntity<T>

  • Change return type Iterable<Note> to List<Note>将返回类型Iterable<Note>更改为List<Note>

Application应用

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

Rest Controller休息控制器

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

        @Autowired
        private NoteRepository noteRepository;

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

    }

Controller控制器

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

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM