![](/img/trans.png)
[英]Error connecting to local MySQL database from spring boot application
[英]Spring Boot not loading entities from local MySQL database
我正在嘗試配置Spring Boot(1.4.2版)以使用我的本地主機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
下表在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)
編輯:我更正了我的控制器,並更新了我的問題。 問題是我的應用程序使用內存中的數據存儲而不是MySQL數據庫。 如果我創建POST端點並將新實體插入數據庫,則可以檢索它,但是由於我沒有從那里獲取任何數據,因此它似乎與現有數據庫不同。
EDIT2:由spring.jpa.hibernate.ddl-auto=update
解決
我在您的代碼上看到多個錯誤。 第一個是@RestController
批注。 查看文檔 。
您輸入的值(“ / 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.