Spring Data JPA:生成动态查询 [英] Spring Data JPA: Generate dynamic query

查看:53
本文介绍了Spring Data JPA:生成动态查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个保存一些逻辑数据的实体:

@Entity公共类人{私人长 ID.私人字符串名称;私人整数年龄;私有字符串地址;...}

我创建了我的 Spring 数据接口

@Repository公共接口 CardInventoryRepository 扩展 JpaRepository{}

我的目的是基于我的实体的现有值创建一个动态查询,例如如果名称为空,则查询为:

select * from Person p Where p.age=12 AND p.address="adress.."

当地址为空时,查询应该是:

select * from Person p Where p.age=12 AND p.name="ALI"

我只想使用非空字段提取数据?

是否有使用 spring 数据构建动态查询的解决方案?提前致谢

解决方案

基于 Spring doc https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#query-by-example<块引用>

示例查询 (QBE) 是一种用户友好的查询技术,具有简单的界面.它允许动态查询创建,而不是要求您编写包含字段名称的查询.其实查询by Example 不要求您通过使用编写查询商店特定的查询语言.

定义:示例采用数据对象(通常是实体对象或其子类型)和如何匹配属性的规范.您可以将 Query by Example 与 JPA 一起使用存储库.

为此,让您的存储库接口扩展QueryByExampleExecutor,例如:

public interface PersonRepository extends CrudRepository, QueryByExampleExecutor{}

以下是 QueryByExampleExecutor 中可用的方法:

公共接口QueryByExampleExecutor;{<S 扩展 T>S findOne(Exampleexample);<S 扩展 T>可迭代的<S>findAll(示例示例);//... 省略了更多功能.}

用法:

示例<人>示例 = Example.of(new Person("Jon", "Snow"));repo.findAll(例子);ExampleMatcher 匹配器 = ExampleMatcher.matching()..withMatcher("名字",endsWith()).withMatcher("lastname", startsWith().ignoreCase());示例<人>示例 = Example.of(new Person("Jon", "Snow"), matcher);回购.计数(示例);

<小时>

更多信息

<块引用>

I have an entity that hold some logic data :

@Entity
public class Person {
  private Long id.
  private String name;
  private int age;
  private String address;
  ...
}

I create my Spring data interface

@Repository
public interface CardInventoryRepository extends JpaRepository<Person , Long> {
}

My purpose is to create a dynamic query based on the exist values of my entity for example if the name is null the query is :

select * from Person p  Where p.age=12 AND p.address="adress.."

When the address is null the query should be :

select * from Person p  Where p.age=12 AND p.name="ALI"

I want to extract data using only the non empty fields ?

is there any solution suing spring data for building dynamic queries ? Thanks in advance

解决方案

Based on Spring doc https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#query-by-example

Query by Example (QBE) is a user-friendly querying technique with a simple interface. It allows dynamic query creation and does not require you to write queries that contain field names. In fact, Query by Example does not require you to write queries by using store-specific query languages at all.

DEFINITION: An Example takes a data object (usually the entity object or a sub-type of it) and a specification how to match properties. You can use Query by Example with JPA Repositories.

To do so, let your repository interface extend QueryByExampleExecutor<T>, for example:

public interface PersonRepository extends CrudRepository<Person, String>, QueryByExampleExecutor<Person> {
}

Here are the available methods in QueryByExampleExecutor :

public interface QueryByExampleExecutor<T> {

  <S extends T> S findOne(Example<S> example);

  <S extends T> Iterable<S> findAll(Example<S> example);

  // … more functionality omitted.
}

USAGES:

Example<Person> example = Example.of(new Person("Jon", "Snow"));
repo.findAll(example);


ExampleMatcher matcher = ExampleMatcher.matching().
    .withMatcher("firstname", endsWith())
    .withMatcher("lastname", startsWith().ignoreCase());

Example<Person> example = Example.of(new Person("Jon", "Snow"), matcher); 
repo.count(example);


MORE INFO

这篇关于Spring Data JPA:生成动态查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆