Spring CriteriaBuilder 忽略空值
Spring CriteriaBuilder ignore null values
我为我的地理地图创建了一个 CriteriaBuilder
。但是这些条件不是必需的,可以得到值 null。我尝试使用 if 案例构建不同的搜索语句,但我认为这不是最好的方法。
如果值为空,是否有更好的方法来说明我的 CriteriaBuilder,您应该在搜索中忽略它?
过滤器示例:
UserSpecification specName =
new UserSpecification(new SearchCriteria("name", ":", data.getName()));
UserSpecification specEnv =
new UserSpecification(new SearchCriteria("env", ":", data.getEnv()));
...
if(data.getEnv().isEmpty()){
List list = gisMapRepository.findAll(Specification.where(specName));
}
Userspecification.java:
public class UserSpecification implements Specification<GeoMap> {
private SearchCriteria criteria;
public UserSpecification(SearchCriteria criteria) {
this.criteria = criteria;
}
@Override
public Predicate toPredicate
(Root<GeoMap> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
if (criteria.getOperation().equalsIgnoreCase(":")) {
if (root.get(criteria.getKey()).getJavaType() == String.class) {
return builder.like(
root.<String>get(criteria.getKey()), "%" + criteria.getValue() + "%");
} else {
return builder.equal(root.get(criteria.getKey()), criteria.getValue());
}
}
...
public class UserSpecification implements Specification<GeoMap> {
private SearchCriteria criteria;
public UserSpecification(SearchCriteria criteria) {
this.criteria = criteria;
}
@Override
public Predicate toPredicate
(Root<GeoMap> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
if (criteria.isEmpty()) return null;
//.. search something
}
}
然后
UserSpecification specName =
new UserSpecification(new SearchCriteria("name", ":", data.getName()));
UserSpecification specEnv =
new UserSpecification(new SearchCriteria("env", ":", data.getEnv()));
...
List list = gisMapRepository.findAll(Specification.where(specName).and(specEnv));
我为我的地理地图创建了一个 CriteriaBuilder
。但是这些条件不是必需的,可以得到值 null。我尝试使用 if 案例构建不同的搜索语句,但我认为这不是最好的方法。
如果值为空,是否有更好的方法来说明我的 CriteriaBuilder,您应该在搜索中忽略它?
过滤器示例:
UserSpecification specName =
new UserSpecification(new SearchCriteria("name", ":", data.getName()));
UserSpecification specEnv =
new UserSpecification(new SearchCriteria("env", ":", data.getEnv()));
...
if(data.getEnv().isEmpty()){
List list = gisMapRepository.findAll(Specification.where(specName));
}
Userspecification.java:
public class UserSpecification implements Specification<GeoMap> {
private SearchCriteria criteria;
public UserSpecification(SearchCriteria criteria) {
this.criteria = criteria;
}
@Override
public Predicate toPredicate
(Root<GeoMap> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
if (criteria.getOperation().equalsIgnoreCase(":")) {
if (root.get(criteria.getKey()).getJavaType() == String.class) {
return builder.like(
root.<String>get(criteria.getKey()), "%" + criteria.getValue() + "%");
} else {
return builder.equal(root.get(criteria.getKey()), criteria.getValue());
}
}
...
public class UserSpecification implements Specification<GeoMap> {
private SearchCriteria criteria;
public UserSpecification(SearchCriteria criteria) {
this.criteria = criteria;
}
@Override
public Predicate toPredicate
(Root<GeoMap> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
if (criteria.isEmpty()) return null;
//.. search something
}
}
然后
UserSpecification specName =
new UserSpecification(new SearchCriteria("name", ":", data.getName()));
UserSpecification specEnv =
new UserSpecification(new SearchCriteria("env", ":", data.getEnv()));
...
List list = gisMapRepository.findAll(Specification.where(specName).and(specEnv));