您的位置:首页 > 编程语言 > Java开发

SpringBoot之五:常用接口解析

2018-03-30 11:14 363 查看

一、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 查询条件。通常
4000
使用匿名内部类的方式来创建该接口的对象

自定义 Repository:可以自己定义一个MyRepository接口
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  SpringBoot Respository