Spring Boot的进阶和高级
一、Repository接口
Repository接口是Spring Data的核心接口,不提供任何方法。
public interface Repository<T,ID extends Serializable>{ }
@RepositoryDefinition注解的使用
二、Repository子接口
CrudRepository:继承Repository,实现了CRUD相关的方法;
PagingAndSortingRepository:继承CrudRepository,实现了分页排序相关的方法;
JpaRepository:继承PagingAndSortingRepository,实现JPA规范相关的方法;
Repository中查询方法定义规则和使用
Spring Data中查询方法名称的定义规则
使用Spring Data完成复杂查询方法名称的命名
eg:where name like ?% and age<?
方法名:findByNameStartingWithAndAgeLessThan(String name,Integer age)
where name in(?,?....) or age<?
findByNameInOrAgeLessThan(List<String>names,Integer age);
对于按照方法命名规则来使用,有弊端:
1)方法名会比较长:约定大于配置
2)对于一些复杂的查询,是很难实现
通过@Query解决该弊端;
三、@Query注解
在Repository方法中使用,不需要遵循查询方法命名规则
只需要将@Query定义在Repository中的方法之上即可
命名参数及索引参数的使用
本地查询
源码:
eg:查询的是实体类,而非库表
@Query("select o from Employee o where id = {select max(id) from Employee t1}")
public Employee getEmployeeByMaxId();
eg:
@Query("select o from Employee o where o.name?1 and o.age=2?")
public List<Employee> queryParams(String name,Integer age);
eg:如果使用 =:方式获取参数,必须添加注解@Param
@Query("select o from Employee o where o.name=:name and o.age=:age")
方法名:queryParams(@Param("name")String name,@Param("age")Integer age);
eg:
@Query("select o from Employee o where o.name like %?1%")
public List<Employee> queryLike(String name);
eg:原生查询方法,需要将原生查询方法设置为true,并且查询的是库表,
@Query(nativeQuery = true , value = "select count(1) from employee")
public long getCount();
四、更新及删除操作整合事物
@Modifying注解使用
@Modifying结合@Query注解执行更新操作
@Transactional在Spring Data中的使用
eg:需添加@Modifying注解,允许修改,这样还是无法操作,新建一个Service进行事物操作
@Modifying
@Query("update Employee o set o.age = :age where o.id = :id")
public void updata(@Param("id")Integer id,@Param("age")Integer age);
Service:需添加事物
@Transactional(javax.transaction)
public void update(Integer id,Integer age){ }
事物在Spring data中的使用:
- 事物一般是在Service层
- @Query、@Modifying、@Transactional的综合使用
五、CrudRepository接口
public interface EmployeeCrudRepository extends CrudRepository<Employee,Integer>{}
需要进行事物操作的,需要在Service层进行操作;
六、PagingAndSortingRepository接口
该接口包含分页和排序的功能;
带排序的查询:findAll(Sort sort)
带排序的分页查询:findAll(PageAble pageable)
eg:分页
eg:排序
七、JPARepository接口
八、JpaSpecificationExecutor接口
Specification封装了JPA Criteria查询条件
Spring Boot的进阶和高级的更多相关文章
- Spring Boot 入门 - 目录
pring Boot 入门 - 进阶篇(3)- 定时任务(@Scheduled) 主要用于定时发送邮件.夜间自动维护等. (1)开启定时任务功能 @Configuration @EnableSched ...
- Springboot 系列(十七)迅速使用 Spring Boot Admin 监控你的 Spring Boot 程序,支持异常邮件通知
1. Spring Boot Admin 是什么 Spring Boot Admin 是由 codecentric 组织开发的开源项目,使用 Spring Boot Admin 可以管理和监控你的 S ...
- spring boot actuator端点高级进阶metris指标详解、git配置详解、自定义扩展详解
https://www.cnblogs.com/duanxz/p/3508267.html 前言 接着上一篇<Springboot Actuator之一:执行器Actuator入门介绍>a ...
- 肝了75天,五万五千字,《Spring Boot 进阶》专栏文章整理成册,分享~
前言 Spring Boot 这个专栏从早期的体系构建到写完,总共花费了七十五天,期间由于工作及个人原因停更了一段时间,没办法,工作实在太忙了. 很多人疑惑了,为什么源码介绍过了就结束了?高级的部分不 ...
- spring boot / cloud (十五) 分布式调度中心进阶
spring boot / cloud (十五) 分布式调度中心进阶 在<spring boot / cloud (十) 使用quartz搭建调度中心>这篇文章中介绍了如何在spring ...
- spring boot / cloud (十二) 异常统一处理进阶
spring boot / cloud (十二) 异常统一处理进阶 前言 在spring boot / cloud (二) 规范响应格式以及统一异常处理这篇博客中已经提到了使用@ExceptionHa ...
- spring boot入门与进阶
视频课程包含: SpringBoot入门.SpringBoot进阶.Spring Cloud微服务.Spring Ecosystem 微服务相关.Spring Boot 入门 IDEA 版本.Spri ...
- Spring Boot高级
Spring Boot高级内容概要一.Spring Boot与缓存二.Spring Boot与消息三.Spring Boot与检索四.Spring Boot与任务五.Spring Boot与安全六.S ...
- Spring Boot进阶系列三
Thymeleaf是官方推荐的显示引擎,这篇文章主要介绍怎么让spring boot整合Thymeleaf. 它是一个适用于Web和独立环境的现代服务器端Java模板引擎. Thymeleaf的主要 ...
随机推荐
- HttpMessageConverter那回事
相信使用过Spring的开发人员都用过@RequestBody.@ResponseBody注解,可以直接将输入解析成Json.将输出解析成Json,但HTTP 请求和响应是基于文本的,意味着浏览器和服 ...
- dp的冗余(选数类)
我们先来看一个例题: 在一个长度为n的序列中选出任意个数的数,要求每m个数中至少一个被选,要求选的数之和最小化. 我们很容易想出用f[i][j]来表示前i个数选的最后一个数是j,也就有 for(int ...
- 不是RESTful不好,是你姿势有问题
文章来源:https://ningyu1.github.io/site/post/01-restful-design-specifications/ 一. 摘要(Abstract) RESTful A ...
- JUC包-原子类(AtomicInteger为例)
目录 JUC包-原子类 为什么需要JUC包中的原子类 原子类原理(AtomicInteger为例) volatile CAS CAS的缺点 ABA问题 什么是ABA问题 ABA问题的解决办法 JUC包 ...
- Linux下nginx反向代理负载均衡几种方式以及配置
下面以ip地址192.168.1.1 和192.168.1.2举例 1.轮询 (1).轮询:每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除. upstream ...
- Spark的dataframe转rdd通用工具类
需求解决问题 当每次读取hive表或者其他数据源,获取数据,相对其进行rdd操作,遇到任何类都需要df.rdd(row>row.getstring(0))去获取,就很麻烦,所以可以实现个通用的转 ...
- 在php里做js操作
'echo <script>js_operation</script>';
- win安装python模块出现依赖问题的解决方法 & No module named 'MySqldb'
前言 一年多了,还在写这种问题,羞愧. 新公司不让用自己的电脑,配的winPC,项目启不起来,之前也出现过这个问题,是py3缺少某个模块,但是自己没记,这次记一下好了. No module named ...
- Java远程下载文件到本地(http协议和ssh2协议)
Java中java.io包为我们提供了输入流和输出流,对文件的读写基本上都依赖于这些封装好的关于流的类中来实现.前段时间遇到了以下两种需求: 1.与某系统对接,每天获取最新的图片并显示在前端页面.该系 ...
- 解决Cannot find module '@angular/compiler-cli'
前言: 今天clone之前做的一个angular项目,使用ng serve一直提示An unhandled exception occurred: Cannot find module '@angul ...