zoukankan      html  css  js  c++  java
  • SpringData_Repository接口概述

    • 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); 条件的属性名称与个数要与参数的位置与个数一一对应
      

        支持的关键字

     SampleJPQL 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
    KeywordSampleJPQL 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);
    	}
    	
    	
    	
    }
    

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

  • 相关阅读:
    导航条按钮的设置UIBarButtonItem
    动态修改app build版本CFBundleVersion
    iOS应用图片尺寸制作脚本
    iPad所有平板型号屏幕尺寸
    一个小时学会Git
    NSLog的各种打印格式符和打印CGRect相关结构体
    Linux上统计文件夹下文件个数以及目录个数
    Privacy Description
    iOS开发微信支付的介绍与实现
    iOS开发苹果内购的介绍与实现
  • 原文地址:https://www.cnblogs.com/airycode/p/6535323.html
Copyright © 2011-2022 走看看