Spring JPA 依赖配置

时间:2022-07-24
本文章向大家介绍Spring JPA 依赖配置,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Spring JPA 依赖配置

翻译原文:Dependencies

​ 由于不同的Spring Data模块的发布日期各有不同,其大多数的主要版本号和次要版本号都不尽相同({name}-{release}就是主要版本号和次要版本号,例如 Neumann-SR3)。查找和项目兼容版本的Spring Data模块的最简单方式就是配置Spring Data Release Train BOM(版本发行清单)附带定义的兼容版本。在Maven项目中,你可以配置POM文件中的<dependencyManagement />来声明此依赖关系,如下:

例1:使用Spring Data release train BOM

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-releasetrain</artifactId>
      <version>Neumann-SR3</version>
      <scope>import</scope>
      <type>pom</type>
    </dependency>
  </dependencies>
</dependencyManagement>

​ 最近的发行版本是Neumann-SR3。此处发行版本用首字母进行排序,版本名称遵循如下格式:{name}-{release},其中release可以是如下之一:

  • BUILD-SNAPSHOT: Current snapshots
  • M1, M2, and so on: Milestones
  • RC1, RC2, and so on: Release candidates
  • RELEASE: GA release
  • SR1, SR2, and so on: Service releases

​ 在我们的Spring Data示例存储库中可以找到使用BOM的工作示例。有了它,您可以在<dependencies />块中声明要使用的Spring Data模块而无需标记版本,如下所示:

例2:声明Spring Data 模块版本

<dependencies>
  <dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-jpa</artifactId>
  </dependency>
<dependencies>

附注

dependencyManagement元素提供了一种管理依赖版本号的方式。在dependencyManagement元素中声明所依赖的jar包的版本号等信息,那么所有子项目再次引入此依赖jar包时则无需显式的列出版本号。Maven会沿着父子层级向上寻找拥有dependencyManagement 元素的项目,然后使用它指定的版本号。

​ 以上面的两个为例子,其效果等同于引入如下形式的配置。

<dependencies>
  <dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-jpa</artifactId>
     <version>Neumann-SR3</version>
     <scope>import</scope>
     <type>pom</type>
  </dependency>
<dependencies>

SpringBoot 项目配置

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

​ 无需配置版本号,只需要导入这个依赖,可以自动匹配版本号。

下一篇:【Spring JPA 核心概念】