🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## Jpa内置查询 jpa内置多种动态查询,常用的有:Example实例查询、Specification复杂查询。 使用文章推荐:https://blog.csdn.net/qq_30054997/article/details/79420141 ## QuerySpec查询 QuerySpec是基于Specification复杂查询封装的一套查询工具,语法类似于Example实例查询,dao层需要继承JpaSpecificationExecutor\<T>接口。示例如下: ```java public Page<User> getPageList(User user, Integer pageIndex, Integer pageSize) { // 创建分页对象 Sort sort = new Sort(Sort.Direction.ASC, "createDate"); PageRequest page = PageRequest.of(pageIndex-1, pageSize, sort); QuerySpec querySpec = QuerySpec.matching() .withMatcher("nickname", QuerySpec.LIKE) .withIgnorePaths("password"); return userRepository.findAll(QuerySpec.of(user, querySpec), page); } ``` 1. static matching() --------------------------------------------------创建QuerySpec实例 2. withMatcher("字段名", 查询规则) -----------------------------添加查询匹配器 3. withMatcherIn("字段名", 值列表) ------------------------------添加In查询方式匹配器 4. withMatcherBetween("字段名", 值1, 值2) -------------------添加Between查询方式匹配器 5. withIgnorePaths("忽略字段"...) ---------------------------------设置需要忽略查询的字段 6. static of(实体对象 ,QuerySpec实例) --------------------------获取Specification复杂查询实例 **注意**:QuerySpec查询不支持关联的字段,必须忽略关联的字段!