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的主要 ...
随机推荐
- 美团关于分布式ID实践方案细节
摘自https://tech.meituan.com/2019/03/07/open-source-project-leaf.html Leaf是美团基础研发平台推出的一个分布式ID生成服务,名字取自 ...
- erlang开发环境(IDE)搭建
1.首先推荐:intellij idea http://www.open-open.com/news/view/44c90e 2.webstorm: http://plugins.jetbrains. ...
- Spring中ApplicationContextAware接口的用法
1.为什么使用AppplicationContextAware? ApplicationContext的BeanFactory 的子类, 拥有更强大的功能,ApplicationContext可以在服 ...
- ElasticSearch教程——分片、扩容以及容错机制(转学习使用)
一.Primary shard和replica shard机制 1.index包含多个shard; 2.每个shard都是一个最小的工作单元,承载部分的数据,Lucene实例,完整的简历索引和处理请求 ...
- 【项目实践】SpringBoot三招组合拳,手把手教你打出优雅的后端接口
以项目驱动学习,以实践检验真知 前言 一个后端接口大致分为四个部分组成:接口地址(url).接口请求方式(get.post等).请求数据(request).响应数据(response).如何构建这几个 ...
- MySQL/MariaDB二进制安装
本文说明MySQL/MariaDB二进制安装的过程 mysql和mariadb的安装方式基本一致,唯一初始化方式有点不一样 1.规划: 数据文件存储位置 /data/my ...
- C#处理医学图像(一):基于Hessian矩阵的血管肺纹理骨骼增强对比
在医院实际环境中,经常遇到有问题的患者,对于一些特殊的场景,比如骨折,肺结节,心脑血管问题 需要图像对比增强来更为清晰的显示病灶助于医生确诊,先看效果: 肺纹理增强: 肺结节增强: 血管对比增强: 骨 ...
- SQL Server On Linux:基于实际项目案例,总结功能支持情况及相关问题解决方案,讲如何快速完成迁移
上个月,有个朋友问我说Sql Sever向Mysql迁移有什么好的经验分享,他们公司客户明确提出不再提供Windows服务器,现在计划Mysql迁移.我说Mysql迁移成本太高了,不妨可以了解一下SQ ...
- PAT天梯赛练习 L3-004 肿瘤诊断 (30分) 三维BFS
题目分析: 可能是我的理解能力比较差,在读题的时候一直以为所有的切片是可以排列组合的,并不是按照输入顺序就定死的,那么这题就变得十分的复杂啦~~~~~,查看的题解之后发现所有的切片并没有所谓的自由组合 ...
- stat filename
查看文件的mtime,atime,ctime 3个时间