2016-11-22 24 views
0

我試圖配置Spring Boot(版本1.4.2)使用我的本地主機MySQL數據庫,但是我的配置有問題。你能否看看這個非常簡單的例子,告訴我發生了什麼?Spring Boot不從本地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數據庫下面的表格與本地主機上稱爲 '示例' 運行(和夫婦的記錄我在那裏插):

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

解決我看到您的代碼多個錯誤。第一個是@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類型而不是長原語。

與您的數據相關的第二個問題是屬性: spring.jpa.hibernate.ddl-auto=create-drop這將在會話結束時刪除您的模式。使用spring.jpa.hibernate.ddl-auto=update

+0

是的,我有這些錯誤在那裏。現在我沒有收到任何異常,但它根本沒有加載任何數據。即使填充數據庫,也只返回空集。 – Smajl

+0

完全同意id的原始包裝器對象。應避免原語,請參閱此處以獲取解釋:http://stackoverflow.com/questions/3535791/primitive-or-wrapper-for-hibernate-primary-keys –

+1

這是錯誤!感謝您花時間和解釋這些基本的東西。乍一看很難發現錯誤。 – Smajl

相關問題