• 1. 概述

  • 2. 快速入门

  • 3. 分页操作

  • 4. 基于方法名查询

  • 5. 基于注解查询

  • 666. 彩蛋


4. 基于方法名查询

示例代码对应仓库:lab-13-jpa 。

在 Spring Data 中,支持根据方法名作生成对应的查询(WHERE)条件,进一步进化我们使用 JPA ,具体是方法名以 findByexistsBycountBydeleteBy 开头,后面跟具体的条件。具体的规则,在 《Spring Data JPA —— Query Creation》 文档中,已经详细提供。如下:

关键字方法示例JPQL snippet
AndfindByLastnameAndFirstname… where x.lastname = ?1 and x.firstname = ?2
OrfindByLastnameOrFirstname… where x.lastname = ?1 or x.firstname = ?2
IsEqualsfindByFirstname,findByFirstnameIs,findByFirstnameEquals… where x.firstname = ?1
BetweenfindByStartDateBetween… where x.startDate between ?1 and ?2
LessThanfindByAgeLessThan… where x.age < ?1
LessThanEqualfindByAgeLessThanEqual… where x.age <= ?1
GreaterThanfindByAgeGreaterThan… where x.age > ?1
GreaterThanEqualfindByAgeGreaterThanEqual… where x.age >= ?1
AfterfindByStartDateAfter… where x.startDate > ?1
BeforefindByStartDateBefore… where x.startDate < ?1
IsNullNullfindByAge(Is)Null… where x.age is null
IsNotNullNotNullfindByAge(Is)NotNull… where x.age not null
LikefindByFirstnameLike… where x.firstname like ?1
NotLikefindByFirstnameNotLike… where x.firstname not like ?1
StartingWithfindByFirstnameStartingWith… where x.firstname like ?1 (parameter bound with appended %)
EndingWithfindByFirstnameEndingWith… where x.firstname like ?1 (parameter bound with prepended %)
ContainingfindByFirstnameContaining… where x.firstname like ?1 (parameter bound wrapped in %)
OrderByfindByAgeOrderByLastnameDesc… where x.age = ?1 order by x.lastname desc
NotfindByLastnameNot… where x.lastname <> ?1
InfindByAgeIn(Collection ages)… where x.age in ?1
NotInfindByAgeNotIn(Collection ages)… where x.age not in ?1
TruefindByActiveTrue()… where x.active = true
FalsefindByActiveFalse()… where x.active = false
IgnoreCasefindByFirstnameIgnoreCase… where UPPER(x.firstame) = UPPER(?1)
  • 注意,如果我们有排序需求,可以使用 OrderBy 关键字。

下面,我们来编写一个简单的示例。

艿艿:IDEA 牛逼,提供的插件已经能够自动提示上述关键字。太强了~

4.1 UserRepository03

在 cn.iocoder.springboot.lab13.mybatis.repository 包路径下,创建 UserRepository03 接口。代码如下:

// UserRepository03.java

public interface UserRepository03 extends PagingAndSortingRepository<UserDOInteger{

    UserDO findByUsername(String username);

    Page<UserDO> findByCreateTimeAfter(Date createTime, Pageable pageable);

}
  • 对于分页操作,需要使用到 Pageable 参数,需要作为方法的最后一个参数。

4.2 简单测试

创建 UserRepository03Test 测试类,我们来测试一下简单的 UserRepository03 的每个操作。代码如下:

// UserRepository03.java

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserRepository03Test {

    @Autowired
    private UserRepository03 userRepository;

    @Test
    public void testFindByUsername() {
        UserDO user = userRepository.findByUsername("yunai");
        System.out.println(user);
    }

    @Test
    public void testFindByCreateTimeAfter() {
        // 创建分页条件
        Pageable pageable = PageRequest.of(110);
        // 执行分页操作
        Date createTime = new Date(2018 - 1990, Calendar.FEBRUARY, 24); // 临时 Demo ,实际不建议这么写
        Page<UserDO> page = userRepository.findByCreateTimeAfter(createTime, pageable);
        // 打印
        System.out.println(page.getTotalElements());
        System.out.println(page.getTotalPages());
    }

}

具体的,胖友可以自己跑跑,妥妥的。


©著作权归作者所有:来自51CTO博客作者mb5ff80520dfa04的原创作品,如需转载,请注明出处,否则将追究法律责任

更多相关文章

  1. flask示例
  2. ThingJS官方示例(十一):基于数据矢量及贴图url开发OD线
  3. LightningChart JS Angular图表使用示例
  4. 每日 (No.0x01)ECMAScript 2016,2017和2018中所有新功能的示例(上
  5. 每日前端夜话(0x02):ECMAScript 2016,2017和2018中所有新功能的示
  6. 官方示例(十二):网页加载道路及Geoline开发ThingJS
  7. 官方示例(十三):3步70行代码开发GIS点坐标技术 ThingJS
  8. 利用端口扫描进行终端合规性检查的一个示例
  9. 如何将smarty安装到MVC架构中(代码示例)

随机推荐

  1. android中解析文件的三种方式
  2. Android数据库升级
  3. Android下uid与多用户释疑(一)
  4. Android(安卓)源码解析 - ScrollView
  5. android的init实例
  6. Android 原生页面同H5交互
  7. Android使用LocalSocket抓取数据
  8. 【Android UI】Android颜色系大全
  9. 图解IntelliJ IDEA 13版本对Android SQLi
  10. Android(安卓)使用 AIDL 实现进程间通信,