SpringBoot之五:常用接口解析

本文详细介绍了Spring Data JPA中的Repository接口体系,包括Repository、CrudRepository、PagingAndSortingRepository等核心接口的功能及使用方法。通过具体实例展示了如何通过这些接口实现基本的增删改查操作及高级查询。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

##一、Repository接口

/*
 * Copyright 20011-2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springframework.data.repository;

import org.springframework.stereotype.Indexed;

/**
 * Central repository marker interface. Captures the domain type to manage as well as the domain type's id type. General
 * purpose is to hold type information as well as being able to discover interfaces that extend this one during
 * classpath scanning for easy Spring bean creation.
 * <p>
 * Domain repositories extending this interface can selectively expose CRUD methods by simply declaring methods of the
 * same signature as those declared in {@link CrudRepository}.
 * 
 * @see CrudRepository
 * @param <T> the domain type the repository manages
 * @param <ID> the type of the id of the entity the repository manages
 * @author Oliver Gierke
 */
@Indexed
public interface Repository<T, ID> {

}

  接口功能:

  • Repository是一个空接口,即是一个标记接口
  • 若我们定义的接口继承了Repository,则该接口会被IOC容器识别为一个Repository Bean纳入到IOC容器中,进而可以在该接口中定义满足一定规范的方法。
  • 实际上也可以通过@RepositoryDefinition,注解来替代继承Repository接口。
  • 查询方法以find | read | get开头;
  • 涉及查询条件时,条件的属性用条件关键字连接,要注意的是条件属性以首字母大写。
  • 使用@Query注解可以自定义JPQL语句实现更灵活的查询。

public interface CatRepositoryTwo extends Repository<Cat, Integer>{
	
	/**
	 * 1、查询方法以find、get、read开头
	 * 2、条件的属性用条件关键字连接,要注意的是条件属性以首字母大写。
	 */
	public Cat findByName(String name);
	
	/**
	 * JPQL语句和HQL语句类似
	 * 使用@Query注解标注这个方法是JPQL查询
	 */
	@Query("from Cat where age=:age")
	public Cat findByAge(@Param("age")Integer age);	
}

在Controller中写对应的方法

   @RequestMapping("/findByName")
    public Cat findByName(String name){
	      return catRepositoryTwo.findByName(name);
    }

   @RequestMapping("/findByAge")
   public Cat findByName(Integer age){
	      return catRepositoryTwo.findByAge(age);
   }

##二、CrudRepository接口

package org.springframework.data.repository;

import java.util.Optional;

/**
 - Interface for generic CRUD operations on a repository for a specific type.
 - 
 - @author Oliver Gierke
 - @author Eberhard Wolff
 */
@NoRepositoryBean
public interface CrudRepository<T, ID> extends Repository<T, ID> {
	............................
}

CrudRepository 接口提供了最基本的对实体类的添删改查操作:

  • T save(T entity):保存单个实体
  • Iterable< T> save(Iterable< ? extends T> entities):保存集合
  • T findOne(ID id):根据id查找实体
  • boolean exists(ID id):根据id判断实体是否存在
  • Iterable< T> findAll():查询所有实体,不用或慎用
  • long count():查询实体数量
  • void delete(ID id):根据Id删除实体
  • void delete(T entity):删除一个实体
  • void delete(Iterable< ? extends T> entities):删除一个实体的集合
  • void deleteAll():删除所有实体,不用或慎用

##三、PagingAndSortingRepository接口
  • Iterable< T> findAll(Sort sort):排序
  • Page< T> findAll(Pageable pageable); //分页查询(含排序功能)

##四、其他接口

  • JpaRepository:查找所有实体,排序、查找所有实体,执行缓存与数据库同步
  • JpaSpecificationExecutor:不属于Repository体系,实现一组 JPA Criteria 查询相关的方法,封装 JPA Criteria 查询条件。通常使用匿名内部类的方式来创建该接口的对象
  • 自定义 Repository:可以自己定义一个MyRepository接口
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值