Spring Boot无法连接到MySql

时间:2019-07-25 07:42:06

标签: java mysql spring spring-boot database-connection

这是我对Spring Boot的第一次练习:

enter image description here

这是我的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=*******

这些是我的课程: 1. DemoMySqlApplication.java

package com.example.demomysql;

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


@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})

//scansiona gli oggetti e li trasmette a Spring Boot
@ComponentScan(basePackages={"com.joydeep.springboot"}) 
public class DemoMysqlApplication {

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


    }
  1. 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
        //quando si digita il path url, ci si deve collegare a ../note per avere le request
        @RequestMapping(value="/note")
        public class NoteController {

            //autowired=si collega in automatico all'oggetto NoteRepository
            @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(实体类)
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”的表)。我在注释中有一行数据。但是当我尝试连接到: http://localhost:8080/note/debug 我看到了:

enter image description here

我认为Controller类有问题。 拜托,你能帮我吗?

谢谢

1 个答案:

答案 0 :(得分:1)

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>

  • 将返回类型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);
        }

    }