springboot系列08:jpa的高级使用
自定义高级查询
用一些加一些关键字And
、 Or
-
接口中定义
1 2 3 4 | /** * 用户名或邮箱查询 */ List<User> findByUserNameOrEmail(String username, String email); |
-
测试类
1 2 3 4 | @Test public void testFindByUserNameOrEmail(){ Assert.assertEquals( 2 ,userRepository.findByUserNameOrEmail( "zhangl1" , "1796969389@qq.com" ).size()); } |
同理:修改、删除、统计语法类似
1 2 3 4 | /** *根据主键删除 */ void deleteById(Long id); |
-
测试结果
1 2 3 4 5 | @Test public void testDeleteById(){ userRepository.deleteById(11L); Assert.assertFalse(userRepository.findById(11L).isPresent()); } |
基本上 SQL 体系中的关键词都可以使用,例如: LIKE
、 IgnoreCase
、 OrderBy
1 2 3 4 | /** * 邮箱模糊查询 */ List<User> findByEmailLike(String email); |
1 2 3 4 | @Test public void testFindByEmailLike(){ Assert.assertEquals( 2 ,userRepository.findByEmailLike( "%1796969389%" ).size()); } |
Keyword | Sample | JPQL snippet |
---|---|---|
And | findByLastnameAndFirstname | … where x.lastname = ?1 and x.firstname = ?2 |
Or | findByLastnameOrFirstname | … where x.lastname = ?1 or x.firstname = ?2 |
Is,Equals | findByFirstnameIs,findByFirstnameEquals | … where x.firstname = ?1 |
Between | findByStartDateBetween | … where x.startDate between ?1 and ?2 |
LessThan | findByAgeLessThan | … where x.age < ?1 |
LessThanEqual | findByAgeLessThanEqual | … where x.age ⇐ ?1 |
GreaterThan | findByAgeGreaterThan | … where x.age > ?1 |
GreaterThanEqual | findByAgeGreaterThanEqual | … where x.age >= ?1 |
After | findByStartDateAfter | … where x.startDate > ?1 |
Before | findByStartDateBefore | … where x.startDate < ?1 |
IsNull | findByAgeIsNull | … where x.age is null |
IsNotNull,NotNull | findByAge(Is)NotNull | … where x.age not null |
Like | findByFirstnameLike | … where x.firstname like ?1 |
NotLike | findByFirstnameNotLike | … where x.firstname not like ?1 |
StartingWith | findByFirstnameStartingWith | … where x.firstname like ?1 (parameter bound with appended %) |
EndingWith | findByFirstnameEndingWith | … where x.firstname like ?1 (parameter bound with prepended %) |
Containing | findByFirstnameContaining | … where x.firstname like ?1 (parameter bound wrapped in %) |
OrderBy | findByAgeOrderByLastnameDesc | … where x.age = ?1 order by x.lastname desc |
Not | findByLastnameNot | … where x.lastname <> ?1 |
In | findByAgeIn(Collection ages) | … where x.age in ?1 |
NotIn | findByAgeNotIn(Collection age) | … where x.age not in ?1 |
TRUE | findByActiveTrue() | … where x.active = true |
FALSE | findByActiveFalse() | … where x.active = false |
IgnoreCase | findByFirstnameIgnoreCase | … where UPPER(x.firstame) = UPPER(?1) |
分页查询
SpringBoot Jpa 已经帮我们实现了分页的功能,在查询的方法中,需要传入参数Pageable
1 2 3 4 5 | @Test public void testFindAllByPage(){ Pageable pageable = PageRequest.of( 0 , 10 , new Sort(Sort.Direction.DESC, "id" )); Assert.assertEquals( 2 ,userRepository.findAll(pageable).getTotalElements()); } |
限制查询
有时候我们只需要查询前N个元素,或者取前一个实体
1 2 3 4 | /** * 取前10条数据 */ Page<User> queryFirst10ByUserName(String userName, Pageable pageable); |
-
测试
1 2 3 4 5 6 | @Test public void testQueryFirst10ByUserName(){ Pageable pageable = PageRequest.of( 0 , 10 , new Sort(Sort.Direction.DESC, "id" )); Page<User> userPage = userRepository.queryFirst10ByUserName( "zhangl1" , pageable); Assert.assertEquals( 1 ,userRepository.queryFirst10ByUserName( "zhangl1" , pageable).getSize() ); } |
分类:
spring boot
标签:
spring
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下