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

时间:2016-11-22 08:05:22

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

我正在尝试配置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上运行的MySQL数据库名为&#39; example&#39;使用下表(以及我插入的一对记录):

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

解决

1 个答案:

答案 0 :(得分: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 primitive。

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

相关问题