2017-12-27 537 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

谢谢,我用依赖关系更新了它。 –