2017-12-27 579 views
1

我嘗試使用以下技術堆棧來構建REST應用:春天 - JPA - Hibernate的如何使用EntityManager的

  • VueJs
  • JPA(休眠)

這是我第一次編寫Sping應用程序和Web應用程序開發的經驗。

我有4代表在我的數據庫:

  • 語言
  • 句子
  • 規則
  • 用戶

例如,在規則有:

Rule create(EntityManagerFactory factory, String type, String hint, String help, Language language); 
List<Rule> readAll(EntityManagerFactory factory); 
Rule readID(EntityManagerFactory factory, int id); 
void update(EntityManagerFactory factory, String type, String hint, String help, Language language); 

所以有我的問題:

  1. 當我創建的每個表控制器,我用的CRUD方法來修改(或沒有)我的數據庫,我返回一個觀點我HTMLVueJS一部分。但我的方法需要一個EntityManagerFactory,我應該在每個控制器類中創建一個字段,或者這不是我應該怎麼做?
  2. 我需要創建一個bean文件並配置它,或者persistence.xmlpom.xml夠了嗎?

感謝

+2

你應該看看Spring Boot。你將不需要這些CRUD方法。 https://spring.io/guides/gs/accessing-data-jpa/ –

+0

即使你不使用Spring Boot,也應該使用spring數據jpa。即使你不這樣做,你一定需要閱讀關於數據訪問和JPA集成的Spring文檔:https://docs.spring.io/spring/docs/current/spring-framework-reference/data-access。 HTML。閱讀文檔是該工作的一部分。從長遠來看,它將爲您節省大量時間。 –

回答

2

好像你的第一個問題可以分解成多個問題。

當我爲每個表創建控制器時,我使用CRUD方法來修改(或不)我的數據庫,並且爲HTML和VueJS部分返回一個視圖。但我的方法需要一個EntityManagerFactory,我應該在每個控制器類中創建一個字段,或者這不是我應該怎麼做?

由於您已經接受了建議使用spring-data-jpa的答案。您將處理實體和存儲庫。

實體是將與您的數據庫交互的JPA託管bean。

@Entity 
@Table(name = "rule") 
public class Rule { 
    @Id 
    @GeneratedValue(strategy=GenerationType.AUTO) 
    long id; 
    String type; 
    ... 
    @OneToOne 
    @JoinColumn(...) 
    Language language; 
} 

存儲庫將提供對數據庫執行操作所需的所有必要操作。使用JPA,您可以創建一個擴展CrudRepository的接口,它將爲您提供一些免費的CRUD操作。 findOne(/* id */)delete()save()

@Repository 
public interface RuleRepository extends CrudRepository<Rule, Long> { 

    // You can easily specify custom finders 
    public List<Rule> findByType(String type); 

} 

但我的方法需要一個EntityManagerFactory的,我應該在每個控制器類中創建一個字段或這不是我應該怎麼辦?

它通常被抱怨有一個請求/響應對象是JPA實體。請參閱鏈接的答案should i use jpa entity in rest request and/or response

您可以採取多種方法來接收控制器請求並將響應發送到客戶端項目。

@Controller 
public class RuleController { 
    @Autowired 
    private RuleRepository ruleRepository; 

    // Approach 1: Use Request Parameters - enforce inputs 
    @PostMapping("/rule/:id") 
    public Rule postWithRequestParams(@PathParam("id") Long id, 
        @RequestParam("type") String type, 
        @RequestParam("hint") String hint, 
        @RequestParam("languageField1") String languageField1) { 
     Rule inputRule = new Rule(id, type, hint, new Language(languageField1)); 

     Rule responseRule = ruleRepository.save(inputRule); 
     return responseRule; // I would imagine you would want to set up a model for the response itself 
    } 



    // Approach 2: Use RequestBody - serialize Rule from the request 
    @PostMapping("/rule/:id") 
    public Rule postWithRequestParams(@PathParam("id") Long id, @RequestBody Rule inputRule) { 
     Rule responseRule = ruleRepository.save(inputRule); 
     return responseRule; 
    } 

我需要創建一個bean文件,並配置它或persistence.xml中和pom.xml的是夠不夠?

如果已經添加spring-boot-starter-data-jpa作爲依賴,很多bean配置中已經爲你做。

在你main/src/resources(你應該有一個application.propertiesapplication.yml

spring.datasource.url= # JDBC url of the database. 
spring.datasource.username= # Login user of the database. 
spring.datasource.password= # Login password of the database. 

春天做了很多的魔術和繁重的你。

+0

我還有一個問題,在我的方法控制器中,是否必須返回關聯的視圖或ModelMap?(如果我不返回它,視圖可以訪問ModelMap)因爲我不知道是否讓控制器猜測他指的是哪個視圖是一種好方法。 – Unconnu

+0

這可能需要另一個問題。 – shinjw

0

你一定要對Spring Boothttp://start.spring.io)一看,使得更容易啓動Web應用程序的開發。至於持久層,您可以使用Spring Data JPA(已包含Hibernate)模塊,該模塊也可以輕鬆與Spring Boot集成。春天數據的美麗已經寫在默認情況下大多數查詢如save(), remove(), find()等等。你只需要定義將被Spring Data使用的對象。

更新:如果您使用的春天啓動,那麼你就不需要實體管理器中看到我的春節,引導REST API例如here

2

。你所要做的就是在你的屬性文件中定義數據源。而在我們的配置類中創建一個Bean就像:

@Bean 
@Primary 
@ConfigurationProperties(prefix = "spring.datasource") 
public DataSource datasource() { 
    return DataSourceBuilder.create().build(); 
} 

現在的你可以用repositories.They處理剩下的事情會像:

import org.springframework.data.repository.CrudRepository; 

public interface RuleRepository extends CrudRepository<Rule, Long> { 

} 

在你的控制器,你會用它喜歡:

@Autowired 
private RuleRepository ruleRepository; 

@Get 
@Path("/getRule/:id") 
public Rule find(@PathParam("id")Long id){ 
    return ruleRepository.findOne(id); 
} 

這些依賴關係我在我的gradle項目中使用。你會發現相同的Maven版本:

compile('org.springframework.boot:spring-boot-starter-data-jpa') 
compile group: 'mysql', name: 'mysql-connector-java' 
+0

這是spring-data-jpa。您可能希望將相關依賴項包含在您的答案中。 – shinjw

+0

謝謝,我用依賴關係更新了它。 –