我们可以使用Spring数据规范维护过滤器的通用对象吗

时间:2019-08-06 05:42:11

标签: spring spring-boot spring-mvc spring-data-jpa spring-data

我正在使用Spring Data JPA规范来过滤数据。

我们可以维护一个公共对象来过滤数据吗?目前,我只有一个名为Car.java的类。假设我想过滤另一个名为Reservations.java的新类。

我们可以维护一个过滤对象来过滤多个类吗?

我的代码是:

POJO

@Data
@Component
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Setter
@Getter
@Table(name = "student_data_with_projection")
public class Student {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "name")
    private String name;

    @Column(name = "age")
    private int age;

    @Column(name = "salary")
    private Float salary;

    @Column(name = "address")
    private String address;

    @Column(name= "model")
    private String model;

}

控制器

@GetMapping(path = "all")
public @ResponseBody Iterable<Car> getFilterData(@RequestParam(required = false, defaultValue = "") String name,
        @RequestParam(required = false, defaultValue = "") String age, @RequestParam(required = false) Float cost,
        @RequestParam(required = false, defaultValue = "") String address,
        @RequestParam(required = false, defaultValue = "") String model,
        @RequestParam(required = false, defaultValue = "0") int pageNum,
        @RequestParam(required = false, defaultValue = "20") int pageSize) {

    CarFilterSpecification spec1 = new CarFilterSpecification(new SearchCriteria("name", ":", "%" + name + "%"));

    CarFilterSpecification spec2 = new CarFilterSpecification(new SearchCriteria("age", ":", age));

    CarFilterSpecification spec3 = new CarFilterSpecification(new SearchCriteria("address", ":", address));

    CarFilterSpecification spec4 = new CarFilterSpecification(new SearchCriteria("model", ":", model));

    Specification<Car> specGroup = Specification.where(spec1).and(spec2).and(spec3).or(spec4);

    if (cost != null) {
        CarFilterSpecification spec5 = new CarFilterSpecification(new SearchCriteria("cost", ":", cost));
        specGroup = specGroup.and(spec5);
    }

    Page<Car> findAll = carRepository.findAll(specGroup, PageRequest.of(pageNum, pageSize));
    System.err.println("result===" + findAll.toString());

    return findAll;
}

@GetMapping("/car/{model}")
public List<Car> searchUserByModelname(@PathVariable String model) {
    return carRepository.findByModel(model);
}

存储库

public interface CarRepository
        extends PagingAndSortingRepository<Car, Long>, JpaSpecificationExecutor<Car>, JpaRepository<Car, Long> {

    public List<Car> findByModel(String model);
}

规格

  @AllArgsConstructor
public class CarFilterSpecification implements Specification<Car> {

    private SearchCriteria criteria;

    public CarFilterSpecification() {
    }

    public CarFilterSpecification(SearchCriteria searchCriteria) {
        super();
        this.criteria = searchCriteria;
    }

    public SearchCriteria getCriteria() {
        return criteria;
    }

    @Override
    public Predicate toPredicate(Root<Car> root, CriteriaQuery<?> query, CriteriaBuilder builder) {

        if (criteria.getOperation().equalsIgnoreCase(">")) {
            return builder.greaterThanOrEqualTo(root.<String>get(criteria.getKey()), criteria.getValue().toString());
        } else if (criteria.getOperation().equalsIgnoreCase("<")) {
            return builder.lessThanOrEqualTo(root.<String>get(criteria.getKey()), criteria.getValue().toString());
        } else 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());
            }
        }
        return null;
    }
}

条件

@Data
@AllArgsConstructor
public class SearchCriteria {
    private String key;
    private String operation;
    private Object value;

    public SearchCriteria(String key, String operation, Object value) {
        super();
        this.key = key;
        this.operation = operation;
        this.value = value;
    }

    /* Getter and Setter */

}

如何解决此问题。

0 个答案:

没有答案