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实例评估的请求参数

page

您想要检索的页面,索引为0,默认为0。

size

要检索的页面大小,默认为20。

sort

属性应该按格式属性property(,ASC | DESC)排序。默认排序方向是升序。如果您想切换路线,请使用多个排序参数,例如 ?sort=firstname&sort=lastname,asc.

更多spring扩展以及支持web支持请查看

005-spring-data-elasticsearch 3.0.0.0使用【三】-spring-data之Spring数据扩展的更多相关文章

  1. spring整合elasticsearch之环境搭建

    推荐一个非常好的博客: 点我 // 测试使用docker下启动的es不管用, 在linux下或者windows下运行的es可用 // 进一步测试docker下启动的es链接时, 开启嗅探也链接不上, ...

  2. 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 ,报错 ...

  3. 001-快速搭建Spring web应用【springboot 2.0.4】-gradle、springboot的启动过程分析、gradle多模块构建

    一.概述 学习<精通Spring MVC4>书籍笔记 二.笔记 1.快速构建Spring starter web项目几种方式 1>使用Spring Tool Suite生成Start ...

  4. 《从0到1学习Flink》—— Data Sink 介绍

    前言 再上一篇文章中 <从0到1学习Flink>-- Data Source 介绍 讲解了 Flink Data Source ,那么这里就来讲讲 Flink Data Sink 吧. 首 ...

  5. Flink 从 0 到 1 学习 —— 如何自定义 Data Sink ?

    前言 前篇文章 <从0到1学习Flink>-- Data Sink 介绍 介绍了 Flink Data Sink,也介绍了 Flink 自带的 Sink,那么如何自定义自己的 Sink 呢 ...

  6. Flink 从 0 到 1 学习 —— 如何自定义 Data Source ?

    前言 在 <从0到1学习Flink>-- Data Source 介绍 文章中,我给大家介绍了 Flink Data Source 以及简短的介绍了一下自定义 Data Source,这篇 ...

  7. 0.9.0.RELEASE版本的spring cloud alibaba nacos实例

    简而言之,nacos与eureka的不同之处有三:后台老板.部署方式.功能.nacos是阿里的,eureka是奈飞的:nacos有自己的安装包,需要独立部署,eureka仅作为一个服务组件,引入jar ...

  8. Spring.Net2.0+NHibernate4.0 +Asp.Net Mvc4 二

    6.SN.Controllers 文件夹Config(Controllers.xml) 文件夹Controllers(TestController.cs) Controllers.xml <?x ...

  9. Enterprise Library - Data Access Application Block 6.0.1304

    Enterprise Library - Data Access Application Block 6.0.1304 企业库,数据访问应用程序块 6.0.1304 企业库的数据访问应用程序块的任务简 ...

随机推荐

  1. 说说无线路由器后门的那些事儿(1)-D-Link篇

    [原创]说说无线路由器后门的那些事儿(1)-D-Link篇 作 者: gamehacker 时 间: 2013-11-29,11:29:19 链 接: http://bbs.pediy.com/sho ...

  2. mvvm实现一个简单的vue

    vue,基于mvvm模式下的一个前端框架 mvvm模式下简单的实现数据代理,数据劫持 1.是用Object.defineProperty 实现数据代理 2.使用发布订阅者模式,配合 Object.de ...

  3. elasticsearch——Rest Client

    https://www.jianshu.com/p/66b91bec12e3 elasticsearch——Rest Client 0.2372018.05.10 15:23:03字数 1287阅读 ...

  4. python--线程锁,队列

    #线程数据安全处理--同步锁 import time def sub(): global num print("ok") lock.acquire()#获取这把锁--->只有 ...

  5. PAT Advanced 1009 Product of Polynomials (25 分)(vector删除元素用的是erase)

    This time, you are supposed to find A×B where A and B are two polynomials. Input Specification: Each ...

  6. Python chardet字符编码的判断

    使用 chardet 可以很方便的实现字符串/文件的编码检测.尤其是中文网页,有的页面使用GBK/GB2312,有的使用UTF8,如果你需要去爬一些页面,知道网页编码很重要的,虽然HTML页面有cha ...

  7. 神经风格转换 (Neural-Style-Transfer-Papers)

    原文:https://github.com/ycjing/Neural-Style-Transfer-Papers Neural-Style-Transfer-Papers Selected pape ...

  8. 网络流 最大流SAPkuangbin模板

    hdu 1532 求1~n的最大流 #include<stdio.h> #include<string.h> #include<algorithm> #includ ...

  9. php内置函数分析之array_fill_keys()

    PHP_FUNCTION(array_fill_keys) { zval *keys, *val, *entry; if (zend_parse_parameters(ZEND_NUM_ARGS(), ...

  10. Springboot配置文件占位符

    一.配置文件占位符 1.application.properties server.port=8088 debug=false product.id=ID:${random.uuid} product ...