SpringdataJpa的官方API学习
(将对Springdata JPA的API 第三章之后进行解释)
一 . Core concepts(核心概念)
1.springdata中的中心接口是——Repository。这个接口没有什么重要的功能(原句称没什么惊喜的一个接口)。主要的作用就是标记和管理。其他的接口都是此接口的子类。
Example 1:
public interface CrudRepository<T, ID extends Serializable> extends Repository<T, ID> {
<S extends T> S save(S var1);//保存给定的实体
<S extends T> Iterable<S> save(Iterable<S> var1);//保存指定的实体集合
T findOne(ID var1);查询指定id的实体
boolean exists(ID var1);//判断一个实体是否与给定的ID存在
Iterable<T> findAll();//返回所有实体
Iterable<T> findAll(Iterable<ID> var1);//查询指定的实体集合
long count();//该实体的总数
void delete(ID var1);//根据id删除指定实体
void delete(T var1);//删除指定实体
void delete(Iterable<? extends T> var1);//删除指定的实体集合
void deleteAll();//删除全部
}
除此还提供了一些技术相关的接口,比如 JpaRepository,MongoRepository这两个接口继承了CrudRepository。
2.CrudRepository
1)CrudRepository有个子类PagingAndSortingRepository,增加了一些方法来实现分页。
Example 2:
public interface PagingAndSortingRepository<T, ID extends Serializable> extends CrudRepository<T, ID> {
Iterable<T> findAll(Sort var1);
Page<T> findAll(Pageable var1);
}
Example 3:Sort中创建的是自定义的排序方式,PageRequest中创建的是自定义分页的方式(注意:分页索引是从0开始,所以想要查询第一页二十条数据时,应插入(0,20))。
dao层:

Controller(直接调用,没通过Service):

2)除了普通查询,计数和删除查询都可以使用
Example 4:计数查询
dao:

controller:

Example 5:派生删除查询
dao:

controller:

3.查询数据
Spring Data,将查询数据的方法分成了四个步骤。用处不大,在此不详述。
4.定义dao层的继承接口
通常,dao层接口将会继承Repository,CrudRepository 或者PagingAndSortingRepository 。
或者不想继承Springdata的接口,你也可以定义个自己的接口@RepositoryDefinition,继承CrudRepository 。可以定义一套完整的自己的方法。
Example 6:
@NoRepositoryBean
interface MyBaseRepository<T, ID extends Serializable> extends Repository<T, ID> { Optional<T> findById(ID id); <S extends T> S save(S entity);
} interface UserRepository extends MyBaseRepository<User, Long> {
User findByEmailAddress(EmailAddress emailAddress);
}
5.空库
spring提供了一些注解,用来对付这种空值请情况。
Example 7:
interface UserRepository : Repository<User, String> {
fun findByUsername(username: String): User //该方法当传入一个空的参数时,将会抛出一个 EmptyResultDataAccessException
fun findByFirstname(firstname: String?): User? //该方法接受零作为参数 firstname 并返回 零 在查询执行不会产生结果。
}
6.查询方法
Spring中的基础架构中,有一种查询构建机制的约束。这种约束会忽略像find…By,read…By,query…By,count…By,和
get…By这种前缀,而开始分析其余部分。当然还可以引入进一步的表达式,可以定义实体中的一些条件,用and或者or对他们进行连接。
Example 8:
interface PersonRepository extends Repository<User, Long> {
List<Person> findByEmailAddressAndLastname(EmailAddress emailAddress, String lastname);
// Enables the distinct flag for the query
List<Person> findDistinctPeopleByLastnameOrFirstname(String lastname, String firstname);
List<Person> findPeopleDistinctByLastnameOrFirstname(String lastname, String firstname);
// Enabling ignoring case for an individual property
List<Person> findByLastnameIgnoreCase(String lastname);
// Enabling ignoring case for all suitable properties
List<Person> findByLastnameAndFirstnameAllIgnoreCase(String lastname, String firstname);
// Enabling static ORDER BY for a query
List<Person> findByLastnameOrderByFirstnameAsc(String lastname);
List<Person> findByLastnameOrderByFirstnameDesc(String lastname);
}
IgnoreCase属性表示忽略大小写
OrderBy表示引用属性进行查找时附加子句并提供排序方向(Asc或Desc)
7.关于特殊参数的处理(比如Pageable和Sort in查询方法)
Example 9:
Page<User> findByLastname(String lastname, Pageable pageable); Slice<User> findByLastname(String lastname, Pageable pageable); List<User> findByLastname(String lastname, Sort sort); List<User> findByLastname(String lastname, Pageable pageable);
8.关于限制查询的结果
查询方法的结果可以通过关键字来限制,first或者top都可以互换使用。且后面可以直接加数字指定需要的前几位结果。
Example 10:
User findFirstByOrderByLastnameAsc(); User findTopByOrderByAgeDesc(); Page<User> queryFirst10ByLastname(String lastname, Pageable pageable); Slice<User> findTop3ByLastname(String lastname, Pageable pageable); List<User> findFirst10ByLastname(String lastname, Sort sort); List<User> findTop10ByLastname(String lastname, Pageable pageable)
9.方法中支持的关键字

lile在使用时,需要在字符串中加上“%%”
Distinct去重
10.使用注解 @Query
Example 11:
public interface UserRepository extends JpaRepository<User, Long> {
@Query("select u from User u where u.emailAddress = ?1")
User findByEmailAddress(String emailAddress);
}
Example 12:使用命名参数
public interface UserRepository extends JpaRepository<User, Long> {
@Query("select u from User u where u.firstname = :firstname or u.lastname = :lastname")
User findByLastnameOrFirstname(@Param("lastname") String lastname,
@Param("firstname") String firstname);
}
Example 13:声明操作查询
@Modifying
@Query("update User u set u.firstname = ?1 where u.lastname = ?2")
int setFixedFirstnameFor(String firstname, String lastname);
SpringdataJpa的官方API学习的更多相关文章
- springDataJpa的官方API
一 . Core concepts(核心概念) 1.springdata中的中心接口是——Repository.这个接口没有什么重要的功能(原句称没什么惊喜的一个接口).主要的作用就是标记和管理.其 ...
- 国内值得关注的官方API集合
项目地址:https://github.com/marktony/Awesome_API 本页仅收集国内部分官方API,如需查看其他版本,请点击这里. 目录 笔记 出行 词典 电商 地图 电影 后端云 ...
- electron之API学习
学习一个新框架或者技术,最深入最全面的方法就是通过官方API,例如我们学习electron: 例如我们需要学习electron的BrowserWindow对象的使用,以及在创建她时我们可以配置的参数: ...
- Vue学习笔记-Django REST framework3后端接口API学习
一 使用环境 开发系统: windows 后端IDE: PyCharm 前端IDE: VSCode 数据库: msyql,navicat 编程语言: python3.7 (Windows x86- ...
- Openstack api 学习文档 & restclient使用文档
Openstack api 学习文档 & restclient使用文档 转载请注明http://www.cnblogs.com/juandx/p/4943409.html 这篇文档总结一下我初 ...
- 关于基本类型值和引用类型值以及Vue官方API的array.$remove(reference)
今天又是孟哥解惑. 数组里的元素也是指向内存地址么? 这个要分情况的. 无论a[0],a[2]在什么地方,只要其值是基本类型值,就是值的比较,只要其值是引用类型(对象),就是内存地址的比较. Vue官 ...
- ASP.NET MVC Web API 学习笔记---第一个Web API程序
http://www.cnblogs.com/qingyuan/archive/2012/10/12/2720824.html GetListAll /api/Contact GetListBySex ...
- Openstack python api 学习文档 api创建虚拟机
Openstack python api 学习文档 转载请注明http://www.cnblogs.com/juandx/p/4953191.html 因为需要学习使用api接口调用openstack ...
- html5游戏引擎phaser官方示例学习
首发:个人博客,更新&纠错&回复 phaser官方示例学习进行中,把官方示例调整为简明的目录结构,学习过程中加了点中文注释,代码在这里. 目前把官方的完整游戏示例看了一大半, brea ...
随机推荐
- RabbitMQ、Redis、Memcache
Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度 ...
- 安卓——BroadcastReceiver
package com.example.administrator.myapplication_reciver; import android.content.BroadcastReceiver; i ...
- jquery 学习(一):jQuery 简介
jQuery 库 - 特性: jQuery 是一个 JavaScript 函数库. jQuery 库包含以下特性: html 元素获取, html 元素操作, css 操作, html 事件函数, J ...
- SQL - 数据查询
数据查询是数据库的核心操作.SQL 提供了 select 语句进行数据查询,该语句的一般格式为: select [ ALL | distinct ] <目标列表达式> [ ,<目 ...
- pycham相关+Python基础
pycham 1.设置pycham和线上服务器同步代码 a.Tools--->Deployment--->Configuration b.设置连接的服务器IP c.设置连接的用户名+密 ...
- Spring Cloud之路:(七)SpringBoot+Shiro实现登录认证和权限管理
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/sage_wang/article/details/79592269一.Shiro介绍1.Shiro是 ...
- SpringBoot的Profile文件
1.多Profile文件我们在主配置文件编写的时候,文件名可以是application-{profile}.properties/yml默认使用application.properties的配置 2. ...
- Ubuntu 下matlab 查看memory函数
%Copyright (c) 2012, Michael Hirsch%All rights reserved.%%Redistribution and use in source and binar ...
- CRF(Conditional Random Field)
条件随机场是近几年自然语言处理领域常用的算法之一,常用于句法分析.命名实体识别.词性标注等.在我看来,CRF就像一个反向的隐马尔可夫模型(HMM),两者都是用了马尔科夫链作为隐含变量的概率转移模型,只 ...
- 【框架】用excel管理测试用例需要的参数数据(二)
一.总体思路 以类为excel名,测试方法名为sheet名,建立excel文件.用jxl包里的方法去读取excel文件里的内容,然后用testng里的dataprovider,将数据传递给测试用例 二 ...