org.springframework.data.querydsl.binding.QuerydslPredicate Java Examples

The following examples show how to use org.springframework.data.querydsl.binding.QuerydslPredicate. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DefaultPredicateArgumentResolver.java    From java-platform with Apache License 2.0 6 votes vote down vote up
/**
 * Obtains the domain type information from the given method parameter. Will
 * favor an explicitly registered on through
 * {@link QuerydslPredicate#root()} but use the actual type of the method's
 * return type as fallback.
 * 
 * @param parameter
 *            must not be {@literal null}.
 * @return
 */
static TypeInformation<?> extractTypeInfo(MethodParameter parameter) {

	QuerydslPredicate annotation = parameter.getParameterAnnotation(QuerydslPredicate.class);

	if (annotation != null && !Object.class.equals(annotation.root())) {
		return ClassTypeInformation.from(annotation.root());
	}

	Class<?> containingClass = parameter.getContainingClass();
	if (ClassUtils.isAssignable(EntityController.class, containingClass)) {
		ResolvableType resolvableType = ResolvableType.forClass(containingClass);
		return ClassTypeInformation.from(resolvableType.as(EntityController.class).getGeneric(0).resolve());
	}

	return detectDomainType(ClassTypeInformation.fromReturnTypeOf(parameter.getMethod()));
}
 
Example #2
Source File: UserController.java    From spring-data-examples with Apache License 2.0 5 votes vote down vote up
@RequestMapping(value = "/", method = RequestMethod.GET)
String index(Model model, //
		@QuerydslPredicate(root = User.class) Predicate predicate, //
		@PageableDefault(sort = { "lastname", "firstname" }) Pageable pageable, //
		@RequestParam MultiValueMap<String, String> parameters) {

	ServletUriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentRequest();
	builder.replaceQueryParam("page", new Object[0]);

	model.addAttribute("baseUri", builder.build().toUri());
	model.addAttribute("users", repository.findAll(predicate, pageable));

	return "index";
}
 
Example #3
Source File: SysErrLogController.java    From SuperBoot with MIT License 5 votes vote down vote up
@ApiOperation(value = "获取日志列表信息", notes = "获取日志列表信息,系统管理员默认可以访问")
@RequestMapping(value = "/", method = RequestMethod.GET)
public BaseResponse<List<ResErrLog>> getErrorLogList(@QuerydslPredicate(root = ErrorInfo.class) Predicate predicate, @RequestParam(value = "page", defaultValue = "0") Integer page,
                                                     @RequestParam(value = "size", defaultValue = "15") Integer size) throws BaseException {
    //添加排序 默认按照最后访问时间进行倒排
    Sort sort = new Sort(new Sort.Order(Sort.Direction.DESC, "execTime"));
    Pageable pageable = new PageRequest(page, size, sort);
    return service.getErrorLogList(pageable, predicate);
}
 
Example #4
Source File: SysLogController.java    From SuperBoot with MIT License 5 votes vote down vote up
@ApiOperation(value = "获取日志列表信息", notes = "获取日志列表信息,系统管理员默认可以访问")
@RequestMapping(value = "/", method = RequestMethod.GET)
public BaseResponse<List<ResLog>> getLogList(@QuerydslPredicate(root = DataInfo.class) Predicate predicate, @RequestParam(value = "page", defaultValue = "0") Integer page,
                                             @RequestParam(value = "size", defaultValue = "15") Integer size) throws BaseException {
    //添加排序 默认按照最后访问时间进行倒排
    Sort sort = new Sort(new Sort.Order(Sort.Direction.DESC, "execTime"));
    Pageable pageable = new PageRequest(page, size, sort);
    return service.getLogList(pageable, predicate);
}
 
Example #5
Source File: SysUserController.java    From SuperBoot with MIT License 5 votes vote down vote up
@ApiOperation(value = "获取用户信息", notes = "获取系统用户信息,系统管理员默认可以访问")
@RequestMapping(value = "/", method = RequestMethod.GET)
public BaseResponse<List<ResUser>> getUser(@RequestParam(value = "page", defaultValue = "0") Integer page,
                                           @RequestParam(value = "size", defaultValue = "15") Integer size,
                                           @QuerydslPredicate(root = SuperbootUser.class) Predicate user, @QuerydslPredicate(root = SuperbootRole.class) Predicate role, @QuerydslPredicate(root = SuperbootGroup.class) Predicate group) throws BaseException {
    Pageable pageable = new PageRequest(page, size);
    return userService.getUserList(pageable, false, user, role, group);
}
 
Example #6
Source File: PatientController.java    From spring-boot-jpa with Apache License 2.0 5 votes vote down vote up
/**
 * Another alternative to QBE via QueryDsl Predicates.
 *
 * @param predicate the Predicate to use to query with
 * @param pageable a Pageable to restrict results
 * @return A paged result of matching Patients
 */
@GetMapping("/search/predicate")
public Page<Patient> getPatientsByPredicate(
		@QuerydslPredicate(root = Patient.class) final Predicate predicate, final Pageable pageable) {
	Page<Patient> pagedResults = this.patientRepository.findAll(predicate, pageable);

	if (!pagedResults.hasContent()) {
		throw new NotFoundException(Patient.RESOURCE_PATH,
				"No Patients found matching predicate " + predicate);
	}

	return pagedResults;
}
 
Example #7
Source File: SysGroupController.java    From SuperBoot with MIT License 5 votes vote down vote up
@ApiOperation(value = "获取组织信息", notes = "获取组织基本信息,系统管理员默认可以访问")
@RequestMapping(value = "/", method = RequestMethod.GET)
public BaseResponse<List<ResGroup>> getGroupList(@RequestParam(value = "page", defaultValue = "0") Integer page,
                                                 @RequestParam(value = "size", defaultValue = "15") Integer size,
                                                 @QuerydslPredicate(root = SuperbootGroup.class) Predicate predicate) throws BaseException {
    Pageable pageable = new PageRequest(page, size);
    return groupService.getGroupList(pageable, predicate);
}
 
Example #8
Source File: DefaultPredicateArgumentResolver.java    From java-platform with Apache License 2.0 5 votes vote down vote up
@Override
public boolean supportsParameter(MethodParameter parameter) {

	if (Predicate.class.equals(parameter.getParameterType())) {
		return true;
	}

	if (parameter.hasParameterAnnotation(QuerydslPredicate.class)) {
		throw new IllegalArgumentException(
				String.format("Parameter at position %s must be of type Predicate but was %s.",
						parameter.getParameterIndex(), parameter.getParameterType()));
	}

	return false;
}
 
Example #9
Source File: SysRoleController.java    From SuperBoot with MIT License 5 votes vote down vote up
@ApiOperation(value = "获取全部角色,分页查询", notes = "获取全部角色,系统管理员默认可以访问")
@RequestMapping(value = "/", method = RequestMethod.GET)
public BaseResponse<List<ResRole>> getItems(@RequestParam(value = "page", defaultValue = "0") Integer page,
                                            @RequestParam(value = "size", defaultValue = "15") Integer size,
                                            @QuerydslPredicate(root = SuperbootRole.class) Predicate predicate) throws BaseException {
    Pageable pageable = new PageRequest(page, size);
    return roleService.getRoles(-1, pageable, predicate);
}
 
Example #10
Source File: SysMenuController.java    From SuperBoot with MIT License 5 votes vote down vote up
@ApiOperation(value = "获取角色未授权菜单", notes = "获取角色未授权菜单,系统管理员默认可以访问")
@RequestMapping(value = "/getRoleNoMenu/{role_id}", method = RequestMethod.GET)
public BaseResponse<List<ResMenu>> getRoleNoMenu(@PathVariable("role_id") Long pkRole,
                                                 @RequestParam(value = "page", defaultValue = "0") Integer page,
                                                 @RequestParam(value = "size", defaultValue = "15") Integer size,
                                                 @QuerydslPredicate(root = SuperbootMenu.class) Predicate predicate) throws BaseException {
    Pageable pageable = new PageRequest(page, size);
    return menuService.getRoleNoMenu(pkRole, -1L, pageable, predicate);
}
 
Example #11
Source File: SysMenuController.java    From SuperBoot with MIT License 5 votes vote down vote up
@ApiOperation(value = "获取全部菜单", notes = "获取全部菜单,系统管理员默认可以访问")
@RequestMapping(value = "/", method = RequestMethod.GET)
public BaseResponse<List<ResMenu>> getItems(@RequestParam(value = "page", defaultValue = "0") Integer page,
                                            @RequestParam(value = "size", defaultValue = "15") Integer size,
                                            @QuerydslPredicate(root = SuperbootMenu.class) Predicate predicate) throws BaseException {
    Pageable pageable = new PageRequest(page, size);
    return menuService.getMenus(-1, pageable, predicate);
}
 
Example #12
Source File: PersonController.java    From blog-tutorials with MIT License 5 votes vote down vote up
@GetMapping("/simplified")
public Page<Person> getPersonsSimplified(
		@QuerydslPredicate(root = Person.class) Predicate predicate, 
		@RequestParam(name = "page", defaultValue = "0") int page,
		@RequestParam(name = "size", defaultValue = "500") int size) {

	return personRepository.findAll(predicate, PageRequest.of(page, size, Sort.by(Sort.Direction.ASC, "id")));
}
 
Example #13
Source File: ConnectionResource.java    From flair-engine with Apache License 2.0 5 votes vote down vote up
/**
 * GET  /connections/all : get all the connections.
 *
 * @param predicate predicate
 * @return the ResponseEntity with status 200 (OK) and the list of connections in body
 */
@GetMapping("/connections/all")
@Timed
public ResponseEntity<List<ConnectionDTO>> getAllConnections(@QuerydslPredicate(root = Connection.class) Predicate predicate) {
    log.debug("REST request to get a page of Connections");
    return new ResponseEntity<>(connectionService.findAll(predicate), HttpStatus.OK);
}
 
Example #14
Source File: SysGroupController.java    From SuperBoot with MIT License 4 votes vote down vote up
@ApiOperation(value = "获取组织记录总数", notes = "获取组织记录总数,系统管理员默认可以访问")
@RequestMapping(value = "/count", method = RequestMethod.GET)
public BaseResponse<ResCount> getGroupCount(@QuerydslPredicate(root = SuperbootGroup.class) Predicate predicate) throws BaseException {
    return groupService.getGroupCount(predicate);
}
 
Example #15
Source File: QueryController.java    From tutorials with MIT License 4 votes vote down vote up
@GetMapping(value = "/users", produces = MediaType.APPLICATION_JSON_VALUE)
public Iterable<User> queryOverUser(@QuerydslPredicate(root = User.class) Predicate predicate) {
    final BooleanBuilder builder = new BooleanBuilder();
    return personRepository.findAll(builder.and(predicate));
}
 
Example #16
Source File: QueryController.java    From tutorials with MIT License 4 votes vote down vote up
@GetMapping(value = "/addresses", produces = MediaType.APPLICATION_JSON_VALUE)
public Iterable<Address> queryOverAddress(@QuerydslPredicate(root = Address.class) Predicate predicate) {
    final BooleanBuilder builder = new BooleanBuilder();
    return addressRepository.findAll(builder.and(predicate));
}
 
Example #17
Source File: SysErrLogController.java    From SuperBoot with MIT License 4 votes vote down vote up
@ApiOperation(value = "获取日志记录数", notes = "获取日志记录数,系统管理员默认可以访问")
@RequestMapping(value = "/count", method = RequestMethod.GET)
public BaseResponse<ResCount> getErrorLogCount(@QuerydslPredicate(root = ErrorInfo.class) Predicate predicate) throws BaseException {
    return service.getErrorLogCount(predicate);
}
 
Example #18
Source File: UserController.java    From tutorials with MIT License 4 votes vote down vote up
@RequestMapping(method = RequestMethod.GET, value = "/api/myusers")
@ResponseBody
public Iterable<MyUser> findAllByWebQuerydsl(@QuerydslPredicate(root = MyUser.class) Predicate predicate) {
    return myUserRepository.findAll(predicate);
}
 
Example #19
Source File: UserController.java    From tutorials with MIT License 4 votes vote down vote up
@GetMapping("/filteredusers")
public Iterable<User> getUsersByQuerydslPredicate(@QuerydslPredicate(root = User.class) Predicate predicate) {
    return userRepository.findAll(predicate);
}
 
Example #20
Source File: SysLogController.java    From SuperBoot with MIT License 4 votes vote down vote up
@ApiOperation(value = "获取日志记录数", notes = "获取日志记录数,系统管理员默认可以访问")
@RequestMapping(value = "/count", method = RequestMethod.GET)
public BaseResponse<ResCount> getLogCount(@QuerydslPredicate(root = DataInfo.class) Predicate predicate) throws BaseException {
    return service.getLogCount(predicate);
}
 
Example #21
Source File: SysUserController.java    From SuperBoot with MIT License 4 votes vote down vote up
@ApiOperation(value = "获取用户信息", notes = "获取系统用户信息,系统管理员默认可以访问")
@RequestMapping(value = "/count", method = RequestMethod.GET)
public BaseResponse<ResCount> getCount(@QuerydslPredicate(root = SuperbootUser.class) Predicate user, @QuerydslPredicate(root = SuperbootRole.class) Predicate role, @QuerydslPredicate(root = SuperbootGroup.class) Predicate group) throws BaseException {
    return userService.getUserCount(user, role, group);
}
 
Example #22
Source File: QuerydslPredicateOperationCustomizer.java    From springdoc-openapi with Apache License 2.0 4 votes vote down vote up
@Override
public Operation customize(Operation operation, HandlerMethod handlerMethod) {
	if (operation.getParameters() == null) {
		return operation;
	}

	MethodParameter[] methodParameters = handlerMethod.getMethodParameters();

	int parametersLength = methodParameters.length;
	List<Parameter> parametersToAddToOperation = new ArrayList<>();
	for (int i = 0; i < parametersLength; i++) {
		MethodParameter parameter = methodParameters[i];
		QuerydslPredicate predicate = parameter.getParameterAnnotation(QuerydslPredicate.class);

		if (predicate == null) {
			continue;
		}

		QuerydslBindings bindings = extractQdslBindings(predicate);

		Set<String> fieldsToAdd = Arrays.stream(predicate.root().getDeclaredFields()).map(Field::getName).collect(Collectors.toSet());

		Map<String, Object> pathSpecMap = getPathSpec(bindings, "pathSpecs");
		//remove blacklisted fields
		Set<String> blacklist = getFieldValues(bindings, "blackList");
		fieldsToAdd.removeIf(blacklist::contains);

		Set<String> whiteList = getFieldValues(bindings, "whiteList");
		Set<String> aliases = getFieldValues(bindings, "aliases");

		fieldsToAdd.addAll(aliases);
		fieldsToAdd.addAll(whiteList);
		for (String fieldName : fieldsToAdd) {
			Type type = getFieldType(fieldName, pathSpecMap, predicate.root());
			io.swagger.v3.oas.models.parameters.Parameter newParameter = buildParam(type, fieldName);

			parametersToAddToOperation.add(newParameter);
		}
	}
	operation.getParameters().addAll(parametersToAddToOperation);
	return operation;
}
 
Example #23
Source File: SysRoleController.java    From SuperBoot with MIT License 4 votes vote down vote up
@ApiOperation(value = "获取全部角色", notes = "获取全部角色多用于下拉,系统管理员默认可以访问")
@RequestMapping(value = "/all", method = RequestMethod.GET)
public BaseResponse<List<ResRole>> getAll(@QuerydslPredicate(root = SuperbootRole.class) Predicate predicate) throws BaseException {
    return roleService.getRoleAll(-1, predicate);
}
 
Example #24
Source File: SysRoleController.java    From SuperBoot with MIT License 4 votes vote down vote up
@ApiOperation(value = "获取记录总数", notes = "获取全部角色,系统管理员默认可以访问")
@RequestMapping(value = "/count", method = RequestMethod.GET)
public BaseResponse<ResCount> getCount(@QuerydslPredicate(root = SuperbootRole.class) Predicate predicate) throws BaseException {
    return roleService.getCount(-1, predicate);
}
 
Example #25
Source File: SysMenuController.java    From SuperBoot with MIT License 4 votes vote down vote up
@ApiOperation(value = "获取菜单总数", notes = "获取菜单总数,系统管理员默认可以访问")
@RequestMapping(value = "/count", method = RequestMethod.GET)
public BaseResponse<ResCount> getCount(@QuerydslPredicate(root = SuperbootMenu.class) Predicate predicate) throws BaseException {
    return menuService.getCount(-1, predicate);
}
 
Example #26
Source File: BaseController.java    From SuperBoot with MIT License 4 votes vote down vote up
@ApiOperation(value = "获取组织树", notes = "获取组织树,主要用于构造菜单使用")
@RequestMapping(value = "/group/tree", method = RequestMethod.GET)
public BaseResponse<List<ResTree>> getGroupTree(@QuerydslPredicate(root = SuperbootGroup.class) Predicate predicate) throws BaseException {
    return pubService.getGroupTree(predicate);
}
 
Example #27
Source File: BaseController.java    From SuperBoot with MIT License 4 votes vote down vote up
@ApiOperation(value = "获取全部组织信息", notes = "获取全部组织信息,主要用于下拉使用")
@RequestMapping(value = "/group/all", method = RequestMethod.GET)
public BaseResponse<List<ResGroup>> getGroupAll(@QuerydslPredicate(root = SuperbootGroup.class) Predicate predicate) throws BaseException {
    return pubService.getGroupAll(predicate);
}
 
Example #28
Source File: GreetingController.java    From springdoc-openapi with Apache License 2.0 4 votes vote down vote up
@GetMapping("/test")
public ResponseEntity<?> sayHello2(@QuerydslPredicate(bindings = CountryPredicate.class, root = Country.class) Predicate predicate,
		@RequestParam List<Status> statuses) {
	return ResponseEntity.ok().build();
}
 
Example #29
Source File: QuerydslPredicateOperationCustomizer.java    From springdoc-openapi with Apache License 2.0 4 votes vote down vote up
/**
 * Extract qdsl bindings querydsl bindings.
 *
 * @param predicate the predicate
 * @return the querydsl bindings
 */
private QuerydslBindings extractQdslBindings(QuerydslPredicate predicate) {
	ClassTypeInformation<?> classTypeInformation = ClassTypeInformation.from(predicate.root());
	TypeInformation<?> domainType = classTypeInformation.getRequiredActualType();

	Optional<Class<? extends QuerydslBinderCustomizer<?>>> bindingsAnnotation = Optional.of(predicate)
			.map(QuerydslPredicate::bindings)
			.map(CastUtils::cast);

	return bindingsAnnotation
			.map(it -> querydslBindingsFactory.createBindingsFor(domainType, it))
			.orElseGet(() -> querydslBindingsFactory.createBindingsFor(domainType));
}