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接口
相关文章推荐
- spring boot 常用注解解析
- spring boot https --restful接口篇
- spring boot jar的启动原理解析
- springboot源码解析:自己实现一个springboot自动配置
- spring扩展的常用接口
- SpringBoot之CommandLineRunner接口和ApplicationRunner接口
- POI使用:用poi接口不区分xls/xlsx格式解析Excel文档(41种日期格式解析方法,5种公式结果类型解析方法,3种常用数值类型精度控制办法)
- springboot+CXF开发webservice对外提供接口(转)
- spring Boot 中文返回给浏览器乱码 解析成问号?? fastJson jackJson
- spring boot 源码解析14-默认错误页面处理流程, 自定义,及EnableAutoConfigurationImportSelector处理
- spring常用注解使用解析
- SpringBoot常用配置 application.yml /application.properties常用配置介绍
- Spring启动常用的几个接口
- SpringBoot HTTP接口 POST、PUT、DELETE 请求
- 附3 springboot源码解析 - 构建SpringApplication
- Spring Bean常用扩展接口
- SpringBoot整合常用技术--MyBatis、Redis、定时任务、分页插件、逆向生成
- spring boot 源码解析25-DataSourcePoolMetadata与DataSourcePoolMetadataProvider
- SpringBoot 中常用注解@Controller/@RestController/@RequestMapping介绍
- springBoot学习之常用注解 --1