005-spring-data-elasticsearch 3.0.0.0使用【三】-spring-data之Spring数据扩展
续
1.8、Spring数据扩展
这些扩展使Spring Data在各种环境下的使用成为可能。目前大部分的整合都是针对Spring MVC。
1.8.1、Querydsl扩展
Querydsl是一个框架,它可以通过流畅的API构建静态类型的SQL查询。
几个Spring Data模块通过QueryDslPredicateExecutor提供与Querydsl的集成。
示例、QueryDslPredicateExecutor接口
public interface QueryDslPredicateExecutor<T> {
//查找并返回与Predicate匹配的单个实体
Optional<T> findById(Predicate predicate);
//查找并返回与谓词匹配的所有实体
Iterable<T> findAll(Predicate predicate);
//返回匹配Predicate的实体的数量
long count(Predicate predicate);
//判断返回与Predicate匹配的实体是否存在
boolean exists(Predicate predicate);
// … more functionality omitted.
}
要使用Querydsl支持,只需在存储库接口上扩展QueryDslPredicateExecutor即可。
interface UserRepository extends CrudRepository<User, Long>, QueryDslPredicateExecutor<User> {
}
以上使用Querydsl谓词可以编写类型安全查询。
Predicate predicate = user.firstname.equalsIgnoreCase("dave").and(user.lastname.startsWithIgnoreCase("mathews"));
userRepository.findAll(predicate);
1.8.2、web支持
如果模块支持存储库编程模型,则Spring Data模块附带各种Web支持。与Web相关的东西需要类路径上的Spring MVC JAR,其中一些甚至提供了与Spring HATEOAS的集成。通常,通过在JavaConfig配置类中使用@EnableSpringDataWebSupport注释来启用集成支持。
启用web支持
@Configuration
@EnableWebMvc
@EnableSpringDataWebSupport
class WebConfiguration {}
或者xml配置
<bean class="org.springframework.data.web.config.SpringDataWebConfiguration" /> <!-- If you're using Spring HATEOAS as well register this one *instead* of the former -->
<bean class="org.springframework.data.web.config.HateoasAwareSpringDataWebConfiguration" />
基本的web支持
上面显示的配置设置将注册一些基本组件:
DomainClassConverter使Spring MVC能够根据请求参数或路径变量来解析存储库管理的域类的实例。
HandlerMethodArgumentResolver实现,以便Spring MVC根据请求参数解析Pageable和Sort实例。
DomainClassConverter
DomainClassConverter允许您直接在Spring MVC控制器方法签名中使用域类型,因此您不必通过存储库手动查找实例:
@Controller
@RequestMapping("/users")
class UserController { @RequestMapping("/{id}")
String showUserForm(@PathVariable("id") User user, Model model) { model.addAttribute("user", user);
return "userForm";
}
}
正如你所看到的,该方法直接接收一个用户实例,不需要进一步查找。通过让Spring MVC首先将路径变量转换为域类的id类型并最终通过在为域类型注册的存储库实例上调用findById(...)来访问实例,可以解决该实例。
目前,存储库必须实施CrudRepository才有资格被发现用于转换。
HandlerMethodArgumentResolvers for Pageable and Sort
上面的配置片段还注册了PageableHandlerMethodArgumentResolver以及SortHandlerMethodArgumentResolver的一个实例。注册使页面和排序成为有效的控制器方法参数
@Controller
@RequestMapping("/users")
class UserController { private final UserRepository repository; UserController(UserRepository repository) {
this.repository = repository;
} @RequestMapping
String showUsers(Model model, Pageable pageable) { model.addAttribute("users", repository.findAll(pageable));
return "users";
}
}
此方法签名将导致Spring MVC尝试使用以下默认配置从请求参数派生Pageable实例:
表1.对Pageable实例评估的请求参数
|
|
您想要检索的页面,索引为0,默认为0。 |
|
|
要检索的页面大小,默认为20。 |
|
|
属性应该按格式属性property(,ASC | DESC)排序。默认排序方向是升序。如果您想切换路线,请使用多个排序参数,例如 |
更多spring扩展以及支持web支持请查看
005-spring-data-elasticsearch 3.0.0.0使用【三】-spring-data之Spring数据扩展的更多相关文章
- spring整合elasticsearch之环境搭建
推荐一个非常好的博客: 点我 // 测试使用docker下启动的es不管用, 在linux下或者windows下运行的es可用 // 进一步测试docker下启动的es链接时, 开启嗅探也链接不上, ...
- ElasticsearchException: java.io.IOException: failed to read [id:0, file:/data/elasticsearch/nodes/0/_state/global-0.st]
from : https://www.cnblogs.com/hixiaowei/p/11213143.html 1.以前装过elasticsearch,重新安装elastic search ,报错 ...
- 001-快速搭建Spring web应用【springboot 2.0.4】-gradle、springboot的启动过程分析、gradle多模块构建
一.概述 学习<精通Spring MVC4>书籍笔记 二.笔记 1.快速构建Spring starter web项目几种方式 1>使用Spring Tool Suite生成Start ...
- 《从0到1学习Flink》—— Data Sink 介绍
前言 再上一篇文章中 <从0到1学习Flink>-- Data Source 介绍 讲解了 Flink Data Source ,那么这里就来讲讲 Flink Data Sink 吧. 首 ...
- Flink 从 0 到 1 学习 —— 如何自定义 Data Sink ?
前言 前篇文章 <从0到1学习Flink>-- Data Sink 介绍 介绍了 Flink Data Sink,也介绍了 Flink 自带的 Sink,那么如何自定义自己的 Sink 呢 ...
- Flink 从 0 到 1 学习 —— 如何自定义 Data Source ?
前言 在 <从0到1学习Flink>-- Data Source 介绍 文章中,我给大家介绍了 Flink Data Source 以及简短的介绍了一下自定义 Data Source,这篇 ...
- 0.9.0.RELEASE版本的spring cloud alibaba nacos实例
简而言之,nacos与eureka的不同之处有三:后台老板.部署方式.功能.nacos是阿里的,eureka是奈飞的:nacos有自己的安装包,需要独立部署,eureka仅作为一个服务组件,引入jar ...
- Spring.Net2.0+NHibernate4.0 +Asp.Net Mvc4 二
6.SN.Controllers 文件夹Config(Controllers.xml) 文件夹Controllers(TestController.cs) Controllers.xml <?x ...
- Enterprise Library - Data Access Application Block 6.0.1304
Enterprise Library - Data Access Application Block 6.0.1304 企业库,数据访问应用程序块 6.0.1304 企业库的数据访问应用程序块的任务简 ...
随机推荐
- 有十个div,怎样实现选中其中一个,改变其背景色,另外九个不变,当选中另一个时又改变另一个的背景色
这个是jq写的,可以自己下载一个js库,配上这个就可以了,里面的div可以用class控制,比如你10个div class为a1 也就是<div class="a1"> ...
- Delphi主消息循环研究(Application.Run和Application.Initialize执行后的情况)
Application.Initialize; Application.CreateForm(TForm1, Form1); Application.Run; 第一步,貌似什么都不做,但如果提前定义I ...
- git init error:Malformed value for push.default: simple
git init error:Malformed value for push.default: simple 1.git config --global push.default matching
- 已知链表头结点指针head,写一个函数把这个链表逆序
Node* ReverseList ( Node *head ) { if ( head == NULL || head->next == NULL ) return head; Node *p ...
- linux下iptables讲解
iptables(netfilter网络过滤器) iptables是linux上特有的防火墙机制,功能非常强大.CentOS默认是没有iptables规则. iptables命令可用于配置Linux的 ...
- Win10 + Ubuntu 安装教程(痛苦踩坑)
今天搞了一天,痛苦万分,本文的教程基本适用大部分情况,现在记录下需要主义的几点: 一.制作ubuntu usb安装盘的时候,格式要选saw的,千万不要用usb-HDD+的 二.安装完后使用EasyBC ...
- MongoDB入门_学习目标
MongoDB的概念 MongoDB mongo 索引 集合 复制集 分片 数据均衡 MongoDB数据库搭建 搭建简单的单机服务 搭建具有冗余容错功能的复制集 搭建大规模数据集群 集群的自动部署 熟 ...
- Insomni'hack teaser 2019 - Misc - curlpipebash
参考链接 https://ctftime.org/task/7454 题目 Welcome to Insomni'hack teaser 2019! Execute this Bash command ...
- CQRS架构下的Saga流程重构
- List常用操作 - List.FindAll / List.Sort
List常用操作 (1) 筛选List中符合条件的项目 var list = FormControlList.FindAll(delegate (FormControlModel obj) { ret ...