spring-data-JPA repository method custom rules

When Spring Data JPA framework for carrying out the method name resolution, will first method name prefix intercepted off the excess, like find, findBy, read, readBy, get, getBy, then rest for resolution.
If create the following query: findByUserDepUuid (), the frame when parsing the method, firstly findBy removed, and then analyzing the remaining attributes, the querying entity is assumed Doc
1: first determine userDepUuid (POJO according to specifications, the first letter lowercase) to query whether the attributes of an entity, and if so, then according to the attribute query; Without this attribute, the second step continues;
2: right to left, taken at the beginning of the first capital letter string here the UUID), and then check whether the rest of the query string attributes of an entity, and if so, then according to the attribute query; if not this property, the second step is repeated to continue taken from right to left; and finally assume the user to query attributes of an entity;
3: The remaining portion is then processed (DepUuid), first determines whether the user corresponding to the type attribute has depUuid, if any, indicates that the method according to the query is ultimately "Doc.user.depUuid" values; otherwise continue in accordance with the steps rule 2 is taken from right to left, represents the final query based on the value "Doc.user.dep.uuid" a.
4: There may be a special case, such a user attribute Doc comprising, userDep also has a property, then there will be mixed. Properties between plus clear "_" In the explicit expression meaning, such as "findByUser_DepUuid)" or "findByUserDep_uuid ()"
关键词    样品    JPQL片段
IsNotNull    findByAgeNotNull    ...其中x.age 不为空【年龄不为空】
喜欢    findByNameLike    ...其中x.name是什么样的?【模糊查找是......】
不喜欢    findByNameNotLike    ...其中x.name不喜欢?【模糊查找不是......】
从...开始    findByNameStartingWith    ...其中x.name类似?(参数绑定附加%)【模糊匹配,类似使用%结尾】
EndingWith    findByNameEndingWith    ...其中x.name类似于?(参数与预置%绑定)【模糊匹配,类似使用%开始】
含    findByNameContaining    ...其中x.name like?(参数绑定在%中)[模糊匹配,类似使用%开头和结尾]
排序依据    findByAgeOrderByName    ...其中x.age =?order by x.name desc 【查找后排序】
不    findByNameNot    ...其中x.name <>?【查找列不是...的】
在    findByAgeIn    ...哪里x.age在?
NotIn    findByAgeNotIn    ...其中x.age不在?
真正    findByActiveTrue    ...其中x.avtive = true
产品嫁接    findByActiveFalse    ...其中x.active = false
和     findByNameAndAge    ...其中x.name =?和x.age =?2
要么    findByNameOrAge    ...其中x.name =?或x.age =?2
之间    findBtAgeBetween    ...其中x.age之间?和?2
少于    findByAgeLessThan    ...其中x.age <?
比...更棒    findByAgeGreaterThan    ...其中x.age>?
在那之后    ...    ...
一片空白    findByAgeIsNull    ...其中x.age为空

自定义查找实例:

    /**
     * 根据id查用户
     **/
    List<User> findByIdOrName(Long id,String name);
 
    /**
     * 根据id查用户
     **/
    User queryXXXXByName(String name);
 
    /**
     * 根据id查name
     **/
    User readNameById(Long id);
 
    /**
     * 查年龄为10岁的学生
     **/
    List<User> getByAge(int age);

三,Spring Data JPA分页查询:

    /**
     * 分页查询左右用户
     * @param pageable
     * @return
     */
    Page<User> findAll(Pageable pageable);
 
    /**
     * 分页查询id不是传入id的用户
     * @param id
     * @param pageable
     * @return
     */
    Page<User> findByIdNot(Long id,Pageable pageable);

【注意】分页查询,的结果页,,页接口继承自切片,这个接口有以下方法

public interface Slice<T> extends Iterable<T> {
    int getNumber();//返回当前页码
 
    int getSize();//返回当前页大小(可能不是一整页)
 
    int getNumberOfElements();//返回当前的元素数量
 
    List<T> getContent();//返回当前页的内容(查询结果)
 
    boolean hasContent();//判断是否有内容存在
 
    Sort getSort();//返回排序方式
 
    boolean isFirst();//判断是不是第一页
 
    boolean isLast();//判断是不是最后一页
 
    boolean hasNext();//判断是否还有下一页
 
    boolean hasPrevious();//判断是否上一页
 
    Pageable nextPageable();//返回下一页
 
    Pageable previousPageable();//返回上一页,如果当前已经是第一个,则返回,请求前一个可以是【null】。在调用此方法之前,客户端应该检查是否收到一个非值。
 
    <S> Slice<S> map(Converter<? super T, ? extends S> var1);//用给定的映射,映射当前的内容,为Slice
}
方法名    关键字    SQL
findById         其中id =?
findByIdIs    是    其中id =?
findByIdEquals    等于    其中id =?
findByNameAndAge    和    where name =?和年龄=?
findByNameOrAge    要么    where name =?或年龄=?
findByNameOrderByAgeDesc    按顺序排列    where name =?按年龄顺序排列
findByAgeNotIn    不在    年龄不在(?)
findByStatusTrue    真正    where status = true
findByStatusFalse    假    其中status = false
findByAgeBetween    之间    年龄在哪?和?
findByNameNot    不    名称<>?
findByAgeLessThan    少于    年龄<?
findByAgeLessThanEqual    LessThanEqual    年龄<=?
findByAgeGreaterThan    比...更棒    年龄>?
findByAgeGreaterThanEqual    GreaterThanEqual    年龄> =?
findByAgeAfter    后    年龄>?
findByAgeBefore    之前    年龄<?
findByNameIsNull    一片空白    其中名称为空
findByNameNotNull    不为空    其中名称不为空
findByNameLike    喜欢    哪里的名字像?
findByNameNotLike    不喜欢    哪里的名字不像?
findByNameStartingWith    从...开始    名称如'?%'
findByNameEndingWith    EndingWith    名称如'%?'
findByNameContaining    含    名称如'%?%'

代码中几个复杂的。 
findByNameAndAgeAndSex:表示where name =?和年龄=?和性=? 
findByNameInAndAgeIsNull:表示(?)中的名称和年龄为null  
findByNameAndAgeInAndSexIn:表示where name =?年龄(?)和性别(?)

可以看出关键字是可以连用的,查找都是用findBy +表中列名,表的列名还有关键字等等拼接时,它们的首字母要大写。  


 

 

Guess you like

Origin www.cnblogs.com/h-c-g/p/10980469.html