Spring Data JPA方法名命名

Spring Data JPA方法名命名

Spring官网截图

Spring Data JPA方法名命名_第1张图片
Spring Data JPA方法名命名_第2张图片

代码如下:

关键字 方法命名 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 < ?
LessThanEqual findByIdLessThanEqual where id <= ?
GreaterThan findByIdGreaterThan where id > ?
GreaterThanEqual findByIdGreaterThanEqual where id > = ?
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(?)

举例(以like为例)

在这里插入图片描述
模糊查询:我这里是根据username进行模糊查询,关键字Like,方法命名时,findByUsernameLike(),仿照样品的形式,JPQL片段:where x.username like…

html代码

 
根据name查询:

controller代码

@PostMapping("/searchusers.html")
    public String searchUsers(Model model,String searchByUsername){
        List users = userService.findByUsernameLike("%"+searchByUsername+"%");
        model.addAttribute("users",users);
        return "userlist";
    }

service代码

//模糊查询
    List findByUsernameLike(String searchByUsername);

serviceImpl代码

    @Override
    public List findByUsernameLike(String searchByUsername) {
        return userDao.findByUsernameLike(searchByUsername);
    }

dao代码

    List findByUsernameLike(String searchByUsername);

你可能感兴趣的:(Spring,Boot)