大佬教程收集整理的这篇文章主要介绍了Spring Data JPA-忽略空参数的命名查询,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
您需要Specification@H_673_4@像这样的实用程序类
public class EntitySpecifications {
public static Specification<Entity> firstIDEquals(Optional<Long> firstID) {// or Long firstID. it is better to avoID Optional method parameters.
return (root, query, builder) ->
firstID.isPresent() ? // or firstID != null if you use Long method parameter
builder.equal(root.get("firstID"), firstID.get()) :
builder.conjunction(); // to ignore this clause
}
public static Specification<Entity> secondIDEquals(Optional<Long> secondID) {
return (root, query, builder) ->
secondID.isPresent() ?
builder.equal(root.get("secondID"), secondID.get()) :
builder.conjunction(); // to ignore this clause
}
}
@H_673_4@
然后你EntityRepository@H_673_4@必须扩展
JpaSpecificationExecutor@H_673_4@
@Repository
public interface EntityRepository
extends JpaRepository<Entity, Long>, JpaSpecificationExecutor<Entity> {
}
@H_673_4@
用法:
@service
public class Entityservice {
@autowired
EntityRepository repository;
public List<Entity> getEntitIEs(Optional<Long> firstID, Optional<Long> secondID) {
Specification<Entity> spec =
Specifications.where(EntitySpecifications.firstIDEquals(firstID)) //Spring Data JPA 2.0: use Specification.where
.and(EntitySpecifications.secondIDEquals(secondID));
return repository.findAll(spec);
}
}
@H_673_4@
我有以下存储库:
@Repository
public interface EntityRepository extends JpaRepository<Entity,Long> {
List<Entity> findAllByFirstId(Long firstId);
List<Entity> findAllBySecondId(Long secondId);
List<Entity> findAllByFirstIdAndSecondId(Long firstId,Long secondId);
}
@H_673_4@
实现使用生成的接口的构造函数io.swagger:swagger-codegen-maven-
plugin@H_673_4@将
Optional<Long>@H_673_4@用作可选请求参数(基础服务也使用相同的参数):
ResponseEntity<List<Entity>> entities(Optional<Long> firstId,Optional<Long> secondId);
@H_673_4@
我想过滤基于参数实体firstId@H_673_4@和
secondId@H_673_4@它们从不
null@H_673_4@■在数据库中,但可以通过构造函数(用于搜索的可选参数)进行传递。
当null@H_673_4@通过传递参数作为可选参数时,命名查询会带来问题,该参数
JpaReposotory@H_673_4@将
null@H_673_4@用作查询数据库的标准。那就是我所不希望的-
我想忽略基于此参数的过滤,只要它是null@H_673_4@。
我的解决方法基于Optional@H_673_4@:
public List<Entity> entities(Optional<Long> firstId,Optional<Long> secondId) {
return firstId
.or(() -> secondId)
.map(value -> {
if (firstId.isEmpty()) {
return entityRepository.findAllBySecondId(value);
}
if (secondId.isEmpty()) {
return entityRepository.findAllByFirstId(value);
}
return entityRepository.findAllByFirstIdAndSecondId(
firstId.get(),secondId.get());
})
.orElse(entityRepository.findAll())
.stream()
.map(...) // Mapping between DTO and entity. For sake of brevity
// I used the same onject Entity for both controler and repository
// as long as it not related to the question
.collect(Collectors.toList());
}
@H_673_4@
已经问过这个问题:Spring Data-
忽略参数,如果它具有空值并且创建了票据DATAJPA-209。
只要问题已经存在了将近3年,而故障单可以追溯到2012年,我想问一下是否存在一种更舒适和通用的方法来避免处理Optional@H_673_4@和复制存储库方法的开销。2个此类参数的解决方案看起来可以接受,但是我想对4-5个参数实施完全相同的过滤。
以上是大佬教程为你收集整理的Spring Data JPA-忽略空参数的命名查询全部内容,希望文章能够帮你解决Spring Data JPA-忽略空参数的命名查询所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。