如何在Spring Boot中使用Spring Data JPA? - DZone Java

栏目: Java · 发布时间: 6年前

内容简介:您可能已经知道,Spring Data JPA是更大的Spring Data系列的一部分。在本文中,我们将使用Spring Data JPA和Spring Boot与MariaDB数据库进行通信。依赖:application.properties配置:

您可能已经知道,Spring Data JPA是更大的Spring Data系列的一部分。在本文中,我们将使用Spring Data JPA和Spring Boot与MariaDB数据库进行通信。

依赖:

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

application.properties配置:

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql:<font><i>//192.168.99.100:3306/test1</i></font><font>
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.database-platform=org.hibernate.dialect.MariaDBDialect
spring.jpa.show-sql=<b>true</b>
</font>

扫描或加载JPA存储库

  • 如果存储库包是Spring Boot主包的子包,那么@SpringBootApplication就足够了,因为它包含了@EnableAutoConfiguration。
  • 但是如果存储库包不是Spring主类包的子包,那么在这种情况下,我们需要声明存储库包,如下所示:@EnableJpaRepositories(basePackages = "com.springbootdev.examples.jpa.repositories")这必须在配置类或  SpringBootApplication 类中提供。
  • 类似地,@EntityScan 如果实体包不是主Spring应用程序包的子包,  则可以使用它。

创建存储库

要创建存储库,只需扩展  JapRepository 接口即可。它默认提供了很多方法。

这是一些有用的 示例代码 供参考。

<b>import</b> org.springframework.data.jpa.repository.JpaRepository;
<b>import</b> org.springframework.stereotype.Repository;
<b>import</b> com.mysql.demo.entity.User;
<b>public</b> <b>interface</b> UserJpaRepository <b>extends</b> JpaRepository<User, Integer>{
}

此接口无需实现。它可以直接注入并在服务类中使用。但如果它不提供方法,我们可以在其中定义并使用它 - 不需要实现。

<b>public</b> <b>interface</b> UserJpaRepository <b>extends</b> JpaRepository<User, Integer>{
List<User> findByName(String name);
}

如何在Spring Boot中使用Spring Data JPA? - DZone Java

查询DSL

好处

  1. 利用在创建JPA实体上花费的工作。
  2. 减少代码维护
  3. 在启动时而不是在运行时检查查询。例如,我们可在 在接口中定义了一个方法 findByNames ,它是一个扩展  JpaRepository。但是我们的实体类并没有一个参数名称name。因此,当我们尝试运行应用程序时,会收到以下错误:“找不到属性名称name....”

查询方法

查询解析器将匹配以下内容:

  1. findBy.. : returns a listfindBy..Is, findBy..Equals, findBy..Not, findBy..Like, findBy..NotLike, findBy..StartingWith, findBy..EndingWith, findBy..ContainingFor number data types: findBy..LessThan, findBy..LessThanEquals, findBy..GreaterThan, findBy..GreaterThanEqualDate comparison: findBy..Before, findBy..After, findBy..BetweenFor boolean comparison: findBy..True, findBy..FalseNull checks: findBy..IsNull, findBy..IsNotNullFor collection comparison, In, notIn: findBy..In(Collection str), findBy..NotIn(Collection str)Ignore case: findBy..IgnoreCase, findBy..StartingWithIgnoreCaseOrder : findBy..OrderByCountryAsc, findBy..OrderByCountryDescTo limit the results:findFirstBy.., findTop5By.., findDistinct..By..s
  2. queryBy..
  3. readBy..
  4. countBy..
  5. getBy..

此标准使用JPA实体属性名称。并且,这包括多个标准连接词'And'和'Or',比如findByStateAndCount(String sate, String countrys).

查询注释

有时,查询dsl方法名称变得太长。或者有时,我们希望使用现有的JPQL。在这些场景中,我们可以使用查询注释。

@Query(<font>"select u from User u where u.age > :age1 and u.age < :age2"</font><font>)
List<User> queryByAgeRange(@Param(</font><font>"age1"</font><font>) <b>int</b> age1, @Param(</font><font>"age2"</font><font>) <b>int</b> age2);
</font>

命名查询

命名查询在Entity类中使用@NamedQuery 注释定义  。例如:

@NamedQuery(name="Model.namedFindAllModelsByType", query="select m from Model m where m.modelType.name= :name")

要在JpaRepository 界面中使用命名查询  ,我们需要执行以下操作:

  • 一种方法是使用命名查询的名称定义 JpaRepository 接口方法
  • 另一种方法是在JapRepository 定义的方法上使用 @Query 注释  。

如何在Spring Boot中使用Spring Data JPA? - DZone Java

原生查询

要将查询标记为本机,请在查询注释中使用原生native param = true。

命名原生查询

这与命名查询的工作方式类似。

分页

有时,对于大量数据,我们可能希望以块的形式获取数据。然后,我们可以去分页。分页由  PagingAndSortingRepository 接口和  findAll(Pageable pag)  方法提供。

<b>public</b> Page<User> getAllUsers(<b>int</b> page, <b>int</b> size) {
<b>return</b> userRepo.findAll(PageRequest.of(page, size));
}

排序

PagingAndSortingRepository --> findAll(Sort sort)

<b>public</b> List<User> getAllUsersSorted(String paramname) {
<b>return</b> userRepo.findAll(Sort.by(Sort.Direction.ASC,paramname));
}

Aulditing校订

在配置类激活使用@EnableJpaAuditing,一些相关注解:

  • @CreatedBy 
  •  @CreatedDate 
  •  @LastModifiedBy 
  •  @LastModifiedDate 

锁Locking

锁定策略有两种类型:

  1. 乐观锁定:使用实体中的version参数实现并使用注释  @Version。如果版本号不匹配,请抛出OptimisticLockingException。
  2. 悲观锁定:长期锁定事务持续时间的数据,防止其他人在事务提交之前访问数据。

使用@Lock 注释在存储库方法上声明锁定策略  。

@Lock(LockModeType.PESSIMISTIC_WRITE)
List<User> findByAgeOrName(<b>int</b> age, String name);

以上所述就是小编给大家介绍的《如何在Spring Boot中使用Spring Data JPA? - DZone Java》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Computers and Intractability

Computers and Intractability

M R Garey、D S Johnson / W. H. Freeman / 1979-4-26 / GBP 53.99

This book's introduction features a humorous story of a man with a line of people behind him, who explains to his boss, "I can't find an efficient algorithm, but neither can all these famous people." ......一起来看看 《Computers and Intractability》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

随机密码生成器
随机密码生成器

多种字符组合密码