• Repository 接口是 Spring Data 的一个核心接口,它不提供任何方法,开发者需要在自己定义的接口中声明需要的方法  public interface Repository<T, ID extends Serializable> { }

  • Spring Data可以让我们只定义接口,只要遵循 Spring Data的规范,就无需写实现类。
  • 与继承 Repository 等价的一种方式,就是在持久层接口上使用 @RepositoryDefinition 注解,并为其指定 domainClass 和 idClass 属性。如下两种方式是完全等价的

Repository 的子接口

  • 基础的 Repository 提供了最基本的数据访问功能,其几个子接口则扩展了一些功能。它们的继承关系如下:

1.Repository: 仅仅是一个标识,表明任何继承它的均为仓库接口类

2.CrudRepository: 继承 Repository,实现了一组 CRUD 相关的方法

3.PagingAndSortingRepository: 继承 CrudRepository,实现了一组分页排序相关的方法

4.JpaRepository: 继承 PagingAndSortingRepository,实现一组 JPA 规范相关的方法

5.自定义的 XxxxRepository 需要继承 JpaRepository,这样的 XxxxRepository 接口就具备了通用的数据访问控制层的能力。

6.JpaSpecificationExecutor: 不属于Repository体系,实现一组 JPA Criteria 查询相关的方法

SpringData 方法定义规范

  • 简单条件查询:查询某一个实体类或者集合
  • 按照 Spring Data 的规范,查询方法以 find | read | get 开头, 涉及条件查询时,条件的属性用条件关键字连接,要注意的是:条件属性以首字母大写。
  • 例如:定义一个 Entity 实体类 class User{ 	private String firstName; 	private String lastName; } 使用And条件连接时,应这样写: findByLastNameAndFirstName(String lastName,String firstName); 条件的属性名称与个数要与参数的位置与个数一一对应
    

      支持的关键字

  Sample JPQL snippet

And

findByLastnameAndFirstname

… where x.lastname = ?1 and x.firstname = ?2

Or

findByLastnameOrFirstname

… where x.lastname = ?1 or x.firstname = ?2

Is,Equals

findByFirstname,findByFirstnameIs,findByFirstnameEquals

… where x.firstname = ?1

Between

findByStartDateBetween

… where x.startDate between ?1 and ?2

LessThan

findByAgeLessThan

… where x.age < ?1

LessThanEqual

findByAgeLessThanEqual

… where x.age <= ?1

GreaterThan

findByAgeGreaterThan

… where x.age > ?1

GreaterThanEqual

findByAgeGreaterThanEqual

… where x.age >= ?1

After

findByStartDateAfter

… where x.startDate > ?1

Before

findByStartDateBefore

… where x.startDate < ?1

IsNull

findByAgeIsNull

… where x.age is null

IsNotNull,NotNull

findByAge(Is)NotNull

… where x.age not null

Like

findByFirstnameLike

… where x.firstname like ?1

NotLike

findByFirstnameNotLike

… where x.firstname not like ?1

StartingWith

findByFirstnameStartingWith

… where x.firstname like ?1(parameter bound with appended %)

EndingWith

findByFirstnameEndingWith

… where x.firstname like ?1(parameter bound with prepended %)

Containing

findByFirstnameContaining

… where x.firstname like ?1(parameter bound wrapped in %)

OrderBy

findByAgeOrderByLastnameDesc

… where x.age = ?1 order by x.lastname desc

Not

findByLastnameNot

… where x.lastname <> ?1

In

findByAgeIn(Collection<Age> ages)

… where x.age in ?1

NotIn

findByAgeNotIn(Collection<Age> age)

… where x.age not in ?1

True

findByActiveTrue()

… where x.active = true

False

findByActiveFalse()

… where x.active = false

IgnoreCase

findByFirstnameIgnoreCase

… where UPPER(x.firstame) = UPPER(?1)

Table 4. Supported keywords inside method names
Keyword Sample JPQL snippet

And

findByLastnameAndFirstname

… where x.lastname = ?1 and x.firstname = ?2

Or

findByLastnameOrFirstname

… where x.lastname = ?1 or x.firstname = ?2

Is,Equals

findByFirstname,findByFirstnameIs,findByFirstnameEquals

… where x.firstname = ?1

Between

findByStartDateBetween

… where x.startDate between ?1 and ?2

LessThan

findByAgeLessThan

… where x.age < ?1

LessThanEqual

findByAgeLessThanEqual

… where x.age <= ?1

GreaterThan

findByAgeGreaterThan

… where x.age > ?1

GreaterThanEqual

findByAgeGreaterThanEqual

… where x.age >= ?1

After

findByStartDateAfter

… where x.startDate > ?1

Before

findByStartDateBefore

… where x.startDate < ?1

IsNull

findByAgeIsNull

… where x.age is null

IsNotNull,NotNull

findByAge(Is)NotNull

… where x.age not null

Like

findByFirstnameLike

… where x.firstname like ?1

NotLike

findByFirstnameNotLike

… where x.firstname not like ?1

StartingWith

findByFirstnameStartingWith

… where x.firstname like ?1(parameter bound with appended %)

EndingWith

findByFirstnameEndingWith

… where x.firstname like ?1(parameter bound with prepended %)

Containing

findByFirstnameContaining

… where x.firstname like ?1(parameter bound wrapped in %)

OrderBy

findByAgeOrderByLastnameDesc

… where x.age = ?1 order by x.lastname desc

Not

findByLastnameNot

… where x.lastname <> ?1

In

findByAgeIn(Collection<Age> ages)

… where x.age in ?1

NotIn

findByAgeNotIn(Collection<Age> age)

… where x.age not in ?1

True

findByActiveTrue()

… where x.active = true

False

findByActiveFalse()

… where x.active = false

IgnoreCase

findByFirstnameIgnoreCase

… where UPPER(x.firstame) = UPPER(?1)

好了废话了这么多直接上代码:

1.首先我们需要定义一个实体Person:

package com.fxr.springdata;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table; @Table(name="JPA_PERSONS")
@Entity
public class Person { private Integer id;
private String lastName;
private String email;
private Date birth;
@GeneratedValue
@Id
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
@Override
public String toString() {
return "Person [id=" + id + ", lastName=" + lastName + ", email="
+ email + ", birth=" + birth + "]";
} }

  2.我们需要写一个PersonRepsotory接口来实现JpaRepository<Person,Integer>,传的两个泛型第一个是实体类,第二个是主键的类型

package com.fxr.springdata;

import org.springframework.data.jpa.repository.JpaRepository;

public interface PersonRepsotory extends JpaRepository<Person,Integer>{

	//根据lastName来获取对应的Person
Person getByLastName(String lastName); }

  3.编写测试类

package com.fxr.test;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.fxr.springdata.Person;
import com.fxr.springdata.PersonRepsotory; public class SpringDataTest { private ApplicationContext ctx = null;
private PersonRepsotory personRepsotory; {
ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
personRepsotory = ctx.getBean(PersonRepsotory.class);
} //测试配置的数据源成功没有成功
@Test
public void testDataSource() throws SQLException{
DataSource dataSource = ctx.getBean(DataSource.class);
System.out.println(dataSource.getConnection());
} @Test
public void testHelloWorldSpringData(){
System.out.println(personRepsotory.getClass().getName()); Person person = personRepsotory.getByLastName("sunwukong");
System.out.println(person);
} }

  打印出结果,就说明我们的程序运行成功

SpringData_Repository接口概述的更多相关文章

  1. 网络IPC:套接字接口概述

    网络IPC:套接字接口概述 套接字接口实现了通过网络连接的不同计算机之间的进程相互通信的机制. 套接字描述符(创建套接字) 套接字是通信端点的抽象,为创建套接字,调用socket函数 #include ...

  2. Java基础学习-接口-概述以及成员特点

    package interfaceclass; /*接口的概述: * 接口解决的问题: * 因为java中的继承的单一局限性(子类只能继承一个父类),为了打破这个局限,java语言提供了一个机制,接口 ...

  3. Java基础系列--06_抽象类与接口概述

    抽象类 (1)如果多个类中存在相同的方法声明,而方法体不一样,我们就可以只提取方法声明. 如果一个方法只有方法声明,没有方法体,那么这个方法必须用抽象修饰. 而一个类中如果有抽象方法,这个类必须定义为 ...

  4. java SE基础(Collection接口概述)

    Collection接口相关集成关系例如以下图 1. 关于可迭代接口(Iterable)             可迭代接口仅包括一个方法,返回一个在一组T类型元素上进行迭代的迭代器: public ...

  5. Linux内核驱动之GPIO子系统API接口概述

    1.前言 在嵌入式Linux开发中,对嵌入式SoC中的GPIO进行控制非常重要,Linux内核中提供了GPIO子系统,驱动开发者在驱动代码中使用GPIO子系统提供的API函数,便可以达到对GPIO控制 ...

  6. Unity UGUI事件接口概述

    UGUI 系统虽然提供了很多封装好的组件,但是要实现一些特定的功能还是显得非常有限,这时候就需要使用事件接口来完成UI功能的实现.比如我们想实现鼠标移动到图片上时自动显示图片的文字介绍,一般思路会想到 ...

  7. java接口概述及特点

    1 package face_09; 2 3 4 5 6 abstract class AbsDemo{ 7 abstract void show1(); 8 abstract void show2( ...

  8. 阶段1 语言基础+高级_1-3-Java语言高级_02-继承与多态_第3节 接口_1_接口概述与生活举例

  9. LCD常用接口原理概述

    Android LCD(5)  平台信息:内核:linux2.6/linux3.0系统:android/android4.0 平台:samsung exynos 4210.exynos 4412 .e ...

随机推荐

  1. HDU2717BFS

    /* WA了12发简直不能忍! . 题意非常简单.从正整数a变为b有三种方法: +1,-1.*2 特殊情况一:a与b相等不须要搜索 特殊情况二:a>b时.结果必定是a-b不需搜 特殊情况三:比較 ...

  2. api数据接口

    阿凡达数据 http://www.avatardata.cn/ 聚合数据 https://www.juhe.cn/

  3. U3D关于message的使用

    Message相关有3条指令: SendMessage ("函数名",参数,SendMessageOptions) //GameObject自身的Script BroadcastM ...

  4. html的初识

    今天我们学习了Html语言,感觉学习这个是我期望很久的啦,之前在百度上面也看过html教程,但是看过之后也忘记啦,太多需要记忆的,所以也没记得什么啦.甚是遗憾啊,总感觉html需要学习好多东西啦的,但 ...

  5. cocos2d-x游戏引擎核心之三——主循环和定时器

    一.游戏主循环 在介绍游戏基本概念的时候,我们曾介绍了场景.层.精灵等游戏元素,但我们却故意避开了另一个同样重要的概念,那就是游戏主循环,这是因为 Cocos2d 已经为我们隐藏了游戏主循环的实现.读 ...

  6. 演示PostgreSQL的详细安装及配置图解

    右击文件选择以管理员身份运行 2 开始执行程序的安装 3 设置安装目录 4 设置数据的保存目录 5 设置数据库管理员密码,请牢记此密码. 6 设置端口号,选择默认的端口号即可 7 根据自己选择设置地区 ...

  7. java基础---->Comparable和Comparator的使用

    Comparable和Comparator都可以实现排序,今天我们就开始两种比较排序接口的学习. Comparable的使用 一.Comparable的文档说明: Lists (and arrays) ...

  8. elasticsearch-1.2.1客户端连接DEMO

    1.下载elasticsearch-1.2.1的zip包,解压之后 双击bin目录中的 elasticsearch.bat(针对windows系统) 启动服务器(默认监听9200端口) 访问 http ...

  9. 【BZOJ1692】[Usaco2007 Dec]队列变换 后缀数组+贪心

    [BZOJ1692][Usaco2007 Dec]队列变换 Description FJ打算带他的N(1 <= N <= 30,000)头奶牛去参加一年一度的“全美农场主大奖赛”.在这场比 ...

  10. git下载和上传项目

    首先是git的下载和安装: https://www.cnblogs.com/chenxqNo01/p/6372933.html git的简单使用: 从码云 clone 项目: git clone ht ...