Spring Data JPA 简单查询-接口方法

一、接口方法整理速查

    下表针对于简单查询,即JpaRepository接口(继承了CrudRepository接口、PagingAndSortingRepository接口)中的可访问方法进行整理。(1)先按照功能进行分类整理,分为保存、删除、查找单个、查找多个、其他5类。(2)再将不建议使用的方法置灰,此类方法多为CrudRepository接口、PagingAndSortingRepository接口中定义,后来JpaRepository接口中又定义了替代方法,更方便使用,比如:查找多个对象时,返回 List 比返回 Iterable 更容易处理。

 

二、五个接口详解 

1、CrudRepository接口。

其中T是要操作的实体类,ID是实体类主键的类型。该接口提供了11个常用操作方法。

复制代码
@NoRepositoryBean  
public interface CrudRepository<T, ID extends Serializable> extends Repository<T, ID> { 

    <S extends T> S save(S entity);//保存  
    <S extends T> Iterable<S> save(Iterable<S> entities);//批量保存  

    T findOne(ID id);//根据id 查询一个对象。返回对象本身,当对象不存在时,返回null   
    Iterable<T> findAll();//查询所有的对象  
    Iterable<T> findAll(Iterable<ID> ids);//根据id列表 查询所有的对象  

    boolean exists(ID id);//根据id 判断对象是否存在 
    long count();//计算对象的总个数  

    void delete(ID id);//根据id 删除  
    void delete(T entity);//删除一个对象 
    void delete(Iterable<? extends T> entities);//批量删除,集合对象(后台执行时,一条一条删除)
    void deleteAll();//删除所有 (后台执行时,一条一条删除)
}
复制代码

 

2、PagingAndSortingRepository接口。

该接口继承了CrudRepository接口,提供了两个方法,实现了分页和排序的功能了。

@NoRepositoryBean  
public interface PagingAndSortingRepository<T, ID extends Serializable> extends CrudRepository<T, ID> {  
Iterable<T> findAll(Sort sort);// 仅排序 Page<T> findAll(Pageable pageable);// 分页和排序 }

 

3、JpaRepository接口。

该接口继承了PagingAndSortingRepository接口。

同时也继承QueryByExampleExecutor接口,这是个用“实例”进行查询的接口,后续再写文章详细说明。

复制代码
@NoRepositoryBean
public interface JpaRepository<T, ID extends Serializable>
        extends PagingAndSortingRepository<T, ID>, QueryByExampleExecutor<T> {
    
    List<T> findAll(); //查询所有对象,返回List
    List<T> findAll(Sort sort); //查询所有对象,并排序,返回List
    List<T> findAll(Iterable<ID> ids); //根据id列表 查询所有的对象,返回List

    void flush(); //强制缓存与数据库同步 

    <S extends T> List<S> save(Iterable<S> entities); //批量保存,并返回对象List
    <S extends T> S saveAndFlush(S entity); //保存并强制同步数据库

    void deleteInBatch(Iterable<T> entities); //批量删除 集合对象(后台执行时,生成一条语句执行,用多个or条件)
    void deleteAllInBatch();//删除所有 (执行一条语句,如:delete from user)

    T getOne(ID id); //根据id 查询一个对象,返回对象的引用(区别于findOne)。当对象不存时,返回引用不是null,但各个属性值是null
    
    @Override
    <S extends T> List<S> findAll(Example<S> example); //根据实例查询
    @Override
    <S extends T> List<S> findAll(Example<S> example, Sort sort);//根据实例查询,并排序。

}
复制代码

 几点说明:

(1)几个查询、及批量保存方法,和 CrudRepository 接口相比,返回的是 List,使用起来更方便。

(2)增加了 InBatch 删除, 实际执行时,后台生成一条sql语句,效率更高些。相比较而言,CrudRepository 接口的删除方法,都是一条一条删除的,即便是 deleteAll 也是一条一条删除的。

(3)增加了 getOne() 方法,切记,该方法返回的是对象引用,当查询的对象不存在时,它的值不是Null。

 

4、JpaSpecificationExecutor接口
该接口提供了对JPA Criteria查询(动态查询)的支持。这个接口很有用,具体不粘源码了。

 

5、Repository接口
这个接口是最基础的接口,只是一个标志性的接口,没有定义任何的方法,那这个接口有什么用了?既然Spring data JPA提供了这个接口,自然是有它的用处,例如,我们有一部分方法是不想对外提供的,比如我们只想提供增加和修改方法,不提供删除方法,那么前面的几个接口都是做不到的,这个时候,我们就可以继承这个接口,然后将CrudRepository接口里面相应的方法拷贝到Repository接口就可以了

 

 

关键字 方法命名 sql where字句
And findByNameAndPwd where name= ? and pwd =?
Or findByNameOrSex where name= ? or sex=?
Is,Equals findById,findByIdEquals where id= ?
Between findByIdBetween where id between ? and ?
LessThan findByIdLessThan where id < ?
LessThanEquals findByIdLessThanEquals where id <= ?
GreaterThan findByIdGreaterThan where id > ?
GreaterThanEqual findByAgeGreaterThanEqual where age >= ?
After findByIdAfter where id > ?
Before findByIdBefore where id < ?
IsNull findByNameIsNull where name is null
isNotNull,NotNull findByNameNotNull where name is not null
Like findByNameLike where name like ?
NotLike findByNameNotLike where name not like ?

StartingWith

findByNameStartingWith where name like '?%'
EndingWith findByNameEndingWith where name like '%?'
Containing findByNameContaining where name like '%?%'
OrderBy findByIdOrderByXDesc where id=? order by x desc
Not findByNameNot where name <> ?
In findByIdIn(Collection<?> c) where id in (?)
NotIn findByIdNotIn(Collection<?> c) where id not in (?)
True

findByAaaTue

where aaa = true
False findByAaaFalse where aaa = false
IgnoreCase findByNameIgnoreCase where UPPER(name)=UPPER(?)
top findTop100 top 10/where ROWNUM <=10

示例:

 

// demo
/**
*根据comId和state查询从导入初始值开始到生效时间最新的记录
 * <p>
 * 2017年7月13日13:37:49
 * xj
 *
 * @param comId            公司代码
 * @param state            状态为1为删除的状态
 * @param effectTimeStart 固定值1970-01-01
 * @param effectTime       生效时间
 * @return
 */
 List<CompanyIndustry> findTop1ByComIdAndStateAndEffectTimeBetweenOrderByEffectTimeDesc (String comId , Integer state , Date effectTimeStart , Date effectTime) ;
  

 

posted @ 2018-02-23 14:50  在途中#  阅读(3964)  评论(0编辑  收藏  举报