Spring Data 动态查询的三种方式

发布时间:2026/8/30 11:49:31
Spring Data 动态查询的三种方式 在页面中展示列表数据时通常需要根据用户输入的不同的查询条件返回不同的查询结果传统的方式往往采用手动编写原始sql拼接where条件的方式这种方式并不安全容易存在sql注入漏洞。本文介绍用SpringDataJpa实现灵活查询的方式具体的代码参照 示例项目 https://github.com/qihaiyan/springcamp/tree/master/spring-data-flex-query一、概述SpringDataJpa提供了三种灵活查询的方式分别是1、通过Query注解编写查询语句2、Example查询3、Specification查询。下面分别介绍这三种方式的使用方法。二、通过Query注解编写查询语句这种方式使用比较简单在 Repository 方法中编写查询语句。publicinterfaceMyDataRepositoryextendsJpaRepositoryMyData,Long,JpaSpecificationExecutorMyData{Query(select U from MyData U where (?1 is null or U.id?1) and (?2 is null or U.name?2))ListMyDatafindByQuery(Long id,String name);}为了能够按照不同的查询条件进行查询需要在查询语句中对查询参数进行判空将所有的查询参数组合成and查询条件。当某个查询参数为空时查询路径会被is null覆盖该查询参数不会对数据进行过滤。三、Example查询Example查询是通过一个数据对象按照约定的规则进行查询只有对象中的非空字段或加入过滤条件为空的字段不会进行过滤。publicinterfaceMyService{MyDataexamplenewMyData();example.setName(two);log.info(find by example: {},myDataRepository.findAll(Example.of(example)));}在上述代码中由于我们只对example对象的name字段赋值因此只会按照name条件进行过滤。更加详细的使用方式可以参照[官方文档(https://docs.spring.io/spring-data/jpa/reference/repositories/query-by-example.html)]四、Specification查询相比于前两种方式Specification查询要灵活的多可以任意组合查询条件实现我们想要的查询结果。首先 Repository 要扩展 JpaSpecificationExecutor publicinterfaceMyDataRepositoryextendsJpaRepositoryMyData,Long,JpaSpecificationExecutorMyData{}用Specification编写一个查询方法privateListMyDatafindBySpec(Longid,Stringname,ListLongids){returnmyDataRepository.findAll((root,query,builder)-{ListPredicatepredicatesnewArrayList();if(id!null){predicates.add(builder.equal(root.get(id),id));}if(name!null){predicates.add(builder.like(root.get(name),name));}if(ids!null){predicates.add(root.get(id).in(ids));}returnbuilder.and(predicates.toArray(newPredicate[0]));},Sort.by(id).descending());}在查询方法中我们分别通过 builder.equal、builder.like、 root.get(“id”).in 实现了likein三个sql子句除此之外还有greaterThanlessThan等其它很多方法可以使用。最后一个参数还可以指定分页和排序条件。使用查询方法进行灵活查询log.info(find by spec with id: {},findBySpec(1L,null,null));log.info(find by spec with name: {},findBySpec(null,%wo%,null));log.info(find by spec with id list: {},findBySpec(null,null,List.of(1L,2L)));执行程序后我们可以在日志中看到不同的查询条件返回了不同的查询结果。完整的实例代码Slf4jSpringBootApplicationpublicclassApplicationimplementsCommandLineRunner{AutowiredprivateMyDataRepositorymyDataRepository;Overridepublicvoidrun(String...args){MyDatamyData1newMyData();myData1.setName(one);myDataRepository.save(myData1);MyDatamyData2newMyData();myData2.setName(two);myDataRepository.save(myData2);log.info(find by id with query: {},myDataRepository.findByQuery(1L,null));log.info(find by id and name with query: {},myDataRepository.findByQuery(1L,one));MyDataexamplenewMyData();example.setName(two);log.info(find by example: {},myDataRepository.findAll(Example.of(example)));log.info(find by spec with id: {},findBySpec(1L,null,null));log.info(find by spec with name: {},findBySpec(null,%wo%,null));log.info(find by spec with id list: {},findBySpec(null,null,List.of(1L,2L)));}privateListMyDatafindBySpec(Longid,Stringname,ListLongids){returnmyDataRepository.findAll((root,query,builder)-{ListPredicatepredicatesnewArrayList();if(id!null){predicates.add(builder.equal(root.get(id),id));}if(name!null){predicates.add(builder.like(root.get(name),name));}if(ids!null){predicates.add(root.get(id).in(ids));}returnbuilder.and(predicates.toArray(newPredicate[0]));},Sort.by(id).descending());}publicstaticvoidmain(String[]args){SpringApplication.run(Application.class,args);}}