一、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中的使用:

  1. 事物一般是在Service层
  2. @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的进阶和高级的更多相关文章

  1. Spring Boot 入门 - 目录

    pring Boot 入门 - 进阶篇(3)- 定时任务(@Scheduled) 主要用于定时发送邮件.夜间自动维护等. (1)开启定时任务功能 @Configuration @EnableSched ...

  2. Springboot 系列(十七)迅速使用 Spring Boot Admin 监控你的 Spring Boot 程序,支持异常邮件通知

    1. Spring Boot Admin 是什么 Spring Boot Admin 是由 codecentric 组织开发的开源项目,使用 Spring Boot Admin 可以管理和监控你的 S ...

  3. spring boot actuator端点高级进阶metris指标详解、git配置详解、自定义扩展详解

    https://www.cnblogs.com/duanxz/p/3508267.html 前言 接着上一篇<Springboot Actuator之一:执行器Actuator入门介绍>a ...

  4. 肝了75天,五万五千字,《Spring Boot 进阶》专栏文章整理成册,分享~

    前言 Spring Boot 这个专栏从早期的体系构建到写完,总共花费了七十五天,期间由于工作及个人原因停更了一段时间,没办法,工作实在太忙了. 很多人疑惑了,为什么源码介绍过了就结束了?高级的部分不 ...

  5. spring boot / cloud (十五) 分布式调度中心进阶

    spring boot / cloud (十五) 分布式调度中心进阶 在<spring boot / cloud (十) 使用quartz搭建调度中心>这篇文章中介绍了如何在spring ...

  6. spring boot / cloud (十二) 异常统一处理进阶

    spring boot / cloud (十二) 异常统一处理进阶 前言 在spring boot / cloud (二) 规范响应格式以及统一异常处理这篇博客中已经提到了使用@ExceptionHa ...

  7. spring boot入门与进阶

    视频课程包含: SpringBoot入门.SpringBoot进阶.Spring Cloud微服务.Spring Ecosystem 微服务相关.Spring Boot 入门 IDEA 版本.Spri ...

  8. Spring Boot高级

    Spring Boot高级内容概要一.Spring Boot与缓存二.Spring Boot与消息三.Spring Boot与检索四.Spring Boot与任务五.Spring Boot与安全六.S ...

  9. Spring Boot进阶系列三

    Thymeleaf是官方推荐的显示引擎,这篇文章主要介绍怎么让spring boot整合Thymeleaf.  它是一个适用于Web和独立环境的现代服务器端Java模板引擎. Thymeleaf的主要 ...

随机推荐

  1. java 深拷贝与浅拷贝机制详解

    概要: 在Java中,拷贝分为深拷贝和浅拷贝两种.java在公共超类Object中实现了一种叫做clone的方法,这种方法clone出来的新对象为浅拷贝,而通过自己定义的clone方法为深拷贝. (一 ...

  2. 二进制格式mysql

    1.二进制MySQL安装 #下载二进制格式的mysql软件包 wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.31- ...

  3. JAVA_基础反射机制

    Java反射机制概述 Reflection(反射)是被视为动态语言的关键,反射机制允许程序在执行期 借助于Reflection API取得任何类的内部信息,并能直接操作任意对象的内 部属性及方法. 加 ...

  4. 网络设置-指定ip

    1 克隆centos6.设置网卡 vim /etc/udev/rules.d/70-persistent-net.rules 确定光标所在的位置 d3d删除以下三行,更改NAME为0 vim /etc ...

  5. CS系统中分页控件的制作

    需求:在一个已有的CS项目(ERP中),给所有的列表加上分页功能. 分页的几个概念: 总记录数  totalCount (只有知道了总记录数,才知道有多少页) 每页记录数  pageSize (根据总 ...

  6. 在Termux(非root的安卓Linux模拟器)中安装和使用ftp服务器(pure-ftpd)(原创)[简单极致]

    Termux是单用户的linux模拟器,所以应用的专属文件夹下面.usr/etc/下面没有passwd和group文件对多用户组配置,只在.usr/bin/下面有passwd二进制应用可以改变当前用户 ...

  7. 【环境搭建】SSM 整合使用

    SSM 整合使用 文章源码 搭建整合环境 整合说明 SSM 整合可以使用多种方式,但是选择 XML + 注解 的方式最为合适. 整合思路 搭建整合环境 Spring 环境搭建并测试 Spring 整合 ...

  8. AI智能皮肤测试仪助力美业数字化营销 实现门店与用户双赢局面

    当皮肤遇到AI智能,会有怎么样的火花呢?随着生活水平的提升,人们对肌肤保养护理的需求也越来越高,人要美,皮肤养护也要更精准,数字化必将成为美业发展的新契机.新机遇下肌肤管家SkinRun为美业客户提供 ...

  9. Invalid bound statement (not found): com.xxx.xxx.dao.ShopMapper.insertShop

    mybatis在编写完SQL,进行测试的时候出现了错误,显示 org.apache.ibatis.binding.BindingException: Invalid bound statement ( ...

  10. 【Oracle】常见等待事件处理

    1.查看数据库中需要关注的等待事件: select sw.seq#,sw.sid||','||s.serial# sids,s.username,sw.event,sw.P1,sw.p2,sw.p3, ...