Spring Data JPA方法名命名

Spring Data JPA方法名命名

Spring官网截图

在这里插入图片描述
在这里插入图片描述

代码如下:

关键字 方法命名 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代码

 <form th:action="@{/searchusers.html}" method="post">
            根据name查询:<input th:type="text" name="searchByUsername"> <input th:type="submit" value="查询2">
        </form>

controller代码

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

service代码

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

serviceImpl代码

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

dao代码

    List<User> findByUsernameLike(String searchByUsername);
发布了86 篇原创文章 · 获赞 5 · 访问量 8787

猜你喜欢

转载自blog.csdn.net/qq_35367566/article/details/104095599