Spring JPA 拓展
Spring JPA 拓展
本节记录了一组Spring数据扩展,它们支持在各种上下文中使用Spring数据。目前,大部分集成都是针对Spring MVC的。
1、Querydsl 拓展
Querydsl是一个框架,它支持通过其连贯的的API构造静态类型的sql类查询。
有几个Spring数据模块通过QuerydslPredicateExecutor提供与Querydsl的集成,如下面的示例所示:
例43:QuerydslPredicateExecutor接口
public interface QuerydslPredicateExecutor<T> {
Optional<T> findById(Predicate predicate); //查找并返回与谓词匹配的单个实体。
Iterable<T> findAll(Predicate predicate); //查找并返回与谓词匹配的所有实体。
long count(Predicate predicate); //返回与谓词匹配的实体数量。
boolean exists(Predicate predicate); //返回与谓词匹配的实体是否存在。
// … more functionality omitted.
}
谓词的用法
要利用Querydsl支持,请在您的存储库接口上扩展QuerydslPredicateExecutor,如下面的示例所示:
例44:在存储库中整合Querydsl
interface UserRepository extends CrudRepository<User, Long>, QuerydslPredicateExecutor<User> {
}
前面的示例允许您使用Querydsl谓词实例编写类型安全的查询,如下面的示例所示:
Predicate predicate = user.firstname.equalsIgnoreCase("dave")
.and(user.lastname.startsWithIgnoreCase("mathews"));
userRepository.findAll(predicate);
2、Web支持
本节包含Spring Data web支持的文档,因为它是在Spring Data Commons的当前(及以后)版本中实现的。由于新引入的支持改变了很多东西,所以我们在web.legacy中保留了以前的行为文档。
例45:使Spring数据支持web
@Configuration
@EnableWebMvc
@EnableSpringDataWebSupport
class WebConfiguration {}
@EnableSpringDataWebSupport
注释注册了一些我们稍后将讨论的组件。它还将检测类路径上的Spring HATEOAS,并为其注册集成组件(如果存在的话)。
或者,如果您使用XML配置,将SpringDataWebConfiguration
或HateoasAwareSpringDataWebConfiguration
注册为Spring bean
,如下面的示例所示(例如SpringDataWebConfiguration
)
例46:启用XML中的Spring Data web
支持
<bean class="org.springframework.data.web.config.SpringDataWebConfiguration" />
<!-- If you use Spring HATEOAS, register this one *instead* of the former -->
<bean class="org.springframework.data.web.config.HateoasAwareSpringDataWebConfiguration" />
基础Web支持
上一节中展示@EnableSpringDataWebSupport
的配置注册了几个基本组件:
DomainClassConverter
让Spring MVC
从请求参数或路径变量中解析存储库管理的域类的实例。HandlerMethodArgumentResolver
实现,让Spring MVC
从请求参数中解析可分页和排序实例。
DomainClassConverter
允许您在Spring MVC
控制器方法签名中直接使用域类型,因此您不需要通过存储库手动查找实例,如下面的示例所示:
例47:在方法签名中使用域类型的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首先将path变量转换为域类的id类型,并最终通过调用为域类型注册的存储库实例的findById()
来访问该实例,可以解析该实例。
目前,存储库必须实现CrudRepository才能被发现进行转换。
用于可分页和排序的HandlerMethodArgumentResolvers
上一节中显示的配置片段还注册了一个PageableHandlerMethodArgumentResolver
以及SortHandlerMethodArgumentResolver
的实例。注册使Pageable
和Sort
成为有效的控制器方法参数,如下面的示例所示:
例48:使用分页Pageable
作为控制器参数
@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
尝试使用以下默认配置从请求参数派生一个可分页实例:
表1:Pageable 请求参数配置
参数名称 | 默认配置 |
---|---|
page | 您想要检索的页面,索引为0,默认值为0。 |
size | 要检索的页面的大小,默认为20。 |
sort | 排序属性,遵循property,property(,ASC|DESC)(,IgnoreCase) 的格式,默认的排序是区分大小写的升序排序使用多个排序参数,如果你想切换方向或大小写敏感性,例如sort=firstname&sort=lastname,asc&sort=city,ignorecase 。 |
要自定义这个行为,需要注册一个实现PageableHandlerMethodArgumentResolverCustomizer
接口或SortHandlerMethodArgumentResolverCustomizer
接口的bean。它的customize()
方法将被调用,从而允许您更改设置,如下面的示例所示:
@Bean SortHandlerMethodArgumentResolverCustomizer sortCustomizer() {
return s -> s.setPropertyDelimiter("<-->");
}
Spring JPA 拓展的更多相关文章
- spring jpa 实体互相引用返回restful数据循环引用报错的问题
spring jpa 实体互相引用返回restful数据循环引用报错的问题 Java实体里两个对象有关联关系,互相引用,比如,在一对多的关联关系里 Problem对象,引用了标签列表ProblemLa ...
- spring jpa 自定义查询数据库的某个字段
spring jpa 提供的查询很强大, 就看你会不会用了. 先上代码, 后面在解释吧 1. 想查单个表的某个字段 在repository中 @Query(value = "select i ...
- Hibernate | Spring JPA | MySQL 使用过程遇到的一些问题
1. 使用过程 2. 背景 3. 遇到问题 3.1 不指定Hibernate数据库方言,默认SQL生成方式 3.2 抛出异常Hibernate加入了@Transactional事务不会回滚 3.3 H ...
- Spring JPA 使用@CreatedDate、@CreatedBy、@LastModifiedDate、@LastModifiedBy 自动生成时间和修改者
JPA Audit 在spring jpa中,支持在字段或者方法上进行注解@CreatedDate.@CreatedBy.@LastModifiedDate.@LastModifiedBy,从字面意思 ...
- Spring JPA学习笔记
目录 什么是JPA? 引入配置 新建一个Entity Bean类 JPA的增删改查 新建操作接口 新建测试类 总结 什么是JPA? 什么是JDBC知道吧?数据库有Mysql,SQL Server,Or ...
- Spring JPA实现逻辑源码分析总结
1.SharedEntityManagerCreator: entitymanager的创建入口 该类被EntityManagerBeanDefinitionRegistrarPostProcesso ...
- 使用Spring JPA中Page、Pageable接口和Sort类完成分页排序
显示时,有三个参数,前两个必填,第几页,一页多少个size,第三个参数默认可以不填. 但是发现这个方法已经过时了,通过查看它的源码发现,新方法为静态方法PageRequest of(page,size ...
- spring jpa和mybatis整合
spring jpa和mybatis整合 前一阵子接手了一个使用SpringBoot 和spring-data-jpa开发的项目 后期新加入一个小伙伴,表示jpa相比mybatis太难用,多表联合的查 ...
- 一篇搞定spring Jpa操作数据库
开始之前你必须在项目配置好数据库,本文使用的spring boot,相比spring,spring boot省去了很多各种对以来组件复杂的配置,直接在pom配置组件,完后会自动帮我们导入组件 < ...
随机推荐
- JS 仿京东放大镜
css代码 body{ margin:; } .box { width: 1210px; position: relative; background-color: pink; margin: 0 a ...
- 12、Decorator 装饰器 模式 装饰起来美美哒 结构型设计模式
1.Decorator模式 装饰模式又名包装(Wrapper)模式.装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案. 装饰器模式(Decorator Pattern)允许向一个现 ...
- Bytom侧链Vapor源码浅析-节点出块过程
Bytom侧链Vapor源码浅析-节点出块过程 在这篇文章中,作者将从Vapor节点的创建开始,进而拓展讲解Vapor节点出块过程中所涉及的源码. 做为Vapor源码解析系列的第一篇,本文首先对Vap ...
- Prometheus Operator 教程:根据服务维度对 Prometheus 分片
原文链接:https://fuckcloudnative.io/posts/aggregate-metrics-user-prometheus-operator/ Promtheus 本身只支持单机部 ...
- python新添加excel数据
相关库 import os import xlwt from xlrd import open_workbook from xlutils.copy import copy 1.判断是否存在xls文件 ...
- Linux expect用法介绍
1.expect是linux中一个交互命令,一般在 /usr/bin/expect路径下,如果该路径未加入到环境中需要先添加,其作用场景常用于交互执行输入指令 常用命令: expect 获取上一命令执 ...
- VMware Workstation 15 Pro安装带图形化界面的CentOS7
1.双击打开“VMware Workstation”,然后选择“创建新的虚拟机” 2.在安装向导中,选择“稍后安装操作系统”,然后点击“下一步”继续安装 3.在“客户机操作系统”中选择“Linux(L ...
- leetcode刷题记录——链表
使用java实现链表 单向链表 双向链表 单向循环链表 双向循环链表 题目记录 160.相交链表 例如以下示例中 A 和 B 两个链表相交于 c1: A: a1 → a2 c1 → c2 → c3 B ...
- 记一次 gltf 模型的绘制性能提升:从ppt到dove,丝滑感受
转换思路 同样一个模型,分别取如下转换思路: 原始模型fbxgltf 原始模型objgltf 但是我在打开中间格式fbx和obj时,发现这两者虽然顶点数量一致,三角形数量一致,但是使用 Windows ...
- python setup.py install 报错【Project namexxx was given, but was not able to be found.】
错误信息: [root@wangjq networking-mirror]# python setup.py install /usr/lib64/python2./distutils/dist.py ...