JPA集成queryDSL,灵活查询 (2.实现基本的条件拼接查询)

1.生成查询模版后,就可以初步实现实体对应属性多条件查询,这个只能初步实现等于查询,其他复杂查询需要queryDSL对应API来实现。

Controller层:

 

import com.five.fiveeducation.entity.Student;
import com.five.fiveeducation.service.EducationService;
import com.querydsl.core.types.Predicate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.querydsl.binding.QuerydslPredicate;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
@PostMapping(value = "findSearch")
public List findSearch(@QuerydslPredicate(root = Student.class) Predicate predicate,Pageable pageable){
    return educationService.findSearch(predicate,pageable).getContent();
}

 

Service层:

 

public Page findSearch(Predicate predicate, Pageable pageable) {
    return educationDao.findAll(predicate,pageable);
}

 

实现多条件and查询

 

你可能感兴趣的:(JPA)