简体   繁体   English

Spring Boot无法从本地MySQL数据库加载实体

[英]Spring Boot not loading entities from local MySQL database

I am trying to configure Spring Boot (version 1.4.2) to use my localhost MySQL database but there is something wrong with my configuration. 我正在尝试配置Spring Boot(1.4.2版)以使用我的本地主机MySQL数据库,但是我的配置有问题。 Could you please take a look at this very simple example and tell me what is going on? 您能否看一下这个非常简单的示例,然后告诉我发生了什么?

I have all the required dependencies in place: 我已经准备好所有必需的依赖项:

<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>
</dependency>

My entity: 我的实体:

@Entity
@Table(name = "greetings")
public class Greeting {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "greeting_id")
    private long id;

    @Column(name = "text")
    private String text;

    // getters and setters here
}

Repository: 仓库:

@Repository
public interface GreetingRepository extends JpaRepository<Greeting, Long> {
}

Controller: 控制器:

@RestController
@RequestMapping("/hello")
public class GreetingsController {

    @Autowired
    private GreetingRepository repo;

    @RequestMapping(method = RequestMethod.GET)
    public Collection<Greeting> getGreetings() {
        return repo.findAll();
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Greeting getGreeting(@PathVariable Long id) {
        return repo.findOne(id);
    }
}

application.properties: application.properties:

spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.properties.hibernate.globally_quoted_identifiers=true

spring.datasource.url=jdbc:mysql://localhost/example
spring.datasource.username=root
spring.datasource.password=secret
spring.datasource.driverClassName=com.mysql.jdbc.Driver

There is a MySQL database running on localhost called 'example' with the following table (and couple of records I inserted in there): 下表在MySQL本地主机上运行了一个名为“ example”的MySQL数据库(以及我在其中插入的几条记录):

mysql> describe greetings;
+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| greeting_id | bigint(20)   | NO   | PRI | NULL    | auto_increment |
| text        | varchar(255) | YES  |     | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

EDIT: I corrected my controller and updated my question. 编辑:我更正了我的控制器,并更新了我的问题。 The problem is that my application uses in-memory datastore instead of my MySQL database. 问题是我的应用程序使用内存中的数据存储而不是MySQL数据库。 IF I create a POST endpoint and insert new entity into the database, I am able to retrieve it but it seems that it is different than my existing database since I am not getting any data from there. 如果我创建POST端点并将新实体插入数据库,则可以检索它,但是由于我没有从那里获取任何数据,因此它似乎与现有数据库不同。

EDIT2: resolved by spring.jpa.hibernate.ddl-auto=update EDIT2:由spring.jpa.hibernate.ddl-auto=update解决

I'm seeing multiple errors on your code. 我在您的代码上看到多个错误。 The first one is with @RestController annotation. 第一个是@RestController批注。 Check out the doc . 查看文档

The value that you put ("/hello") is not what you are expecting...It's not the request mapping handler name as in @RequestMapping annotation. 您输入的值(“ / hello”)不是您所期望的...这不是@RequestMapping批注中的请求映射处理程序名称。 Instead of this use: 代替这种用法:

@RestController
@RequestMapping("/hello")
public class GreetingsController {

    @Autowired
    private GreetingRepository repo;

    @RequestMapping(method = RequestMethod.GET)
    public Collection<Greeting> getGreetings() {
        return repo.findAll();
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Greeting getGreeting(@PathVariable long id) {
        return repo.findOne(id);
    }
}

Actually I think this will resolve your problem . 实际上,我认为这将解决您的问题。 The second one is suggestion for id field - use Long Wrapper type instead of long primitive. 第二个建议是对id字段的建议-使用Long Wrapper类型而不是long基本类型。

Second one problem related to your data is the property: spring.jpa.hibernate.ddl-auto=create-drop this will drop your schema at end of session. 与数据有关的第二个问题是属性: spring.jpa.hibernate.ddl-auto=create-drop这将在会话结束时删除架构。 Use spring.jpa.hibernate.ddl-auto=update 使用spring.jpa.hibernate.ddl-auto=update

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

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