【问题标题】:Spring Boot not loading entities from local MySQL databaseSpring Boot 不从本地 MySQL 数据库加载实体
【发布时间】:2017-04-05 19:40:21
【问题描述】:

我正在尝试将 Spring Boot(版本 1.4.2)配置为使用我的 localhost MySQL 数据库,但我的配置有问题。你能看看这个非常简单的例子,然后告诉我发生了什么吗?

我已准备好所有必需的依赖项:

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

我的实体:

@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
public interface GreetingRepository extends JpaRepository<Greeting, Long> {
}

控制器:

@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:

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

在 localhost 上运行了一个名为“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)

编辑:我更正了我的控制器并更新了我的问题。问题是我的应用程序使用内存数据存储而不是我的 MySQL 数据库。如果我创建一个 POST 端点并将新实体插入数据库,我可以检索它,但它似乎与我现有的数据库不同,因为我没有从那里获取任何数据。

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

【问题讨论】:

    标签: java mysql spring-boot spring-data spring-data-jpa


    【解决方案1】:

    我发现您的代码存在多个错误。第一个是带有@RestController 注释的。查看doc

    您输入的值 ("/hello") 不是您所期望的...它不是 @RequestMapping 注释中的请求映射处理程序名称。而不是这种用法:

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

    其实我认为这会解决你的问题。第二个是id 字段的建议 - 使用 Long Wrapper 类型而不是 long 原语。

    与您的数据相关的第二个问题是属性: spring.jpa.hibernate.ddl-auto=create-drop 这将在会话结束时删除您的架构。使用spring.jpa.hibernate.ddl-auto=update

    【讨论】:

    • 没错,我有这些错误。现在我没有收到任何异常,但它根本没有加载任何数据。即使填充了数据库,也只返回空集。
    • 完全同意 id 的原始包装对象。应避免使用基元,请参阅此处了解说明:stackoverflow.com/questions/3535791/…
    • 错了!感谢您花时间解释这些基本的事情。乍一看很难发现错误。
    • 不客气。很高兴能帮助您解决问题。
    猜你喜欢
    • 1970-01-01
    • 2015-12-22
    • 2013-05-04
    • 2018-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-29
    • 1970-01-01
    相关资源
    最近更新 更多