Spring 数据 JPA 和 ORM 之间的区别

Difference between Spring Data JPA and ORM

以下是我关于 Spring Data JPA 的问题。

Q1是Spring数据JPA、ORM吗?如果不是那么它是什么?

Q2spring数据JPA有什么优势?

Q1 Is Spring Data JPA, ORM? If not then, what it is?

没有。它是一个在编译时为您创建“自动”数据访问对象 (DAO) 的系统,并且 在这些 DAO 中使用 ORM(如 Hibernate)。

Q2 What is the advantage of spring data JPA?

您不需要编写自己的 DAO

举个例子,你创建一个这样的实体:

@Entity
public class Foo {

  @Id
  private Long id;

  private String name;

  ...
}

和这样的存储库定义:

public interface FooRepository extends CrudRepository<Foo, Long> {
  //that's it, nothing else. no code
}

Spring Data JPA 然后将在编译时创建一个真实的存储库 class,您可以使用它来 select、插入、更新和删除 Foo 对象。

@Controller
public class FooController {
  
  @Autowired
  private FooRepository fooRepository;

  @RequestMapping("/")
  @ResponseBody
  Foo getFoo() {
    return fooRepository.findOne(1L); //look, you didn't need to write a DAO!
  }
}

此存储库 class 在 运行 时使用您的 JPA EntityManager。