前言
Spring Data Jpa框架的目标是显著减少实现各种持久性存储的数据访问层所需的样板代码量。Spring Data Jpa存储库抽象中的中央接口是Repository。它需要领域实体类以及领域实体ID类型作为类型参数来进行管理。该接口主要用作标记接口,以捕获要使用的类型并帮助您发现扩展该接口的接口。CrudRepository、JpaRepository是更具体的数据操作抽象,一般我们在项目中使用的时候定义我们的领域接口然后继承CrudRepository或JpaRepository即可实现实现基础的CURD方法了,但是这种用法有局限性,不能处理超复杂的查询,而且稍微复杂的查询代码写起来也不是很优雅,所以下面看看怎么最优雅的解决这个问题。
扩展接口用法
1/** 2 * @author: kl @kailing.pub 3 * @date: 2019/11/11 4 */ 5@Repository 6public interface SendLogRepository extends JpaRepository<SendLog,Integer> { 7 8 /** 9 * 派生的通过解析方法名称的查询 10 * @param templateName 11 * @return 12 */ 13 List<SendLog> findSendLogByTemplateName(String templateName); 14 15 /** 16 * HQL 17 * @param templateName 18 * @return 19 */ 20 @Query(value ="select SendLog from SendLog s where s.templateName = :templateName") 21 List<SendLog> findByTempLateName(String templateName); 22 23 /** 24 * 原生sql 25 * @param templateName 26 * @return 27 */ 28 @Query(value ="select s.* from sms_sendlog s where s.templateName = :templateName",nativeQuery = true) 29 List<SendLog> findByTempLateNameNative(String templateName); 30}
优点:
- 1、这种扩展接口的方式是最常见的用法,继承JpaRepository接口后,立马拥有基础的CURD功能
- 2、还可以通过特定的方法名做解析查询,这个可以算spring Data Jpa的最特殊的特性了。而且主流的IDE对这种使用方式都有比较好的自动化支持,在输入要解析的方法名时会给出提示。
- 3、可以非常方便的以注解的形式支持HQL和原生SQL
缺陷:
- 1、复杂的分页查询支持不好
缺陷就一条,这种扩展接口的方式要实现复杂的分页查询,有两种方式,而且这两种方式代码写起来都不怎么优雅,而且会把大量的条件拼接逻辑写在调用查询的service层。
第一种实例查询(Example Query)方式:
1 public void testExampleQuery() { 2 SendLog log = new SendLog(); 3 log.setTemplateName("kl"); 4 /* 5 * 注意:withMatcher方法的propertyPath参数值填写领域对象的字段值,而不是实际的表字段 6 */ 7 ExampleMatcher matcher = ExampleMatcher.matching() 8 .withMatcher("templateName", match -> match.contains()); 9 Example example = Example.of(log, matcher); 10 11 Pageable pageable = PageRequest.of(0, 10); 12 Page<SendLog> logPage = repository.findAll(example, pageable); 13 }
上面代码实现的语义是模糊查询templateName等于"kl"的记录并分页,乍一看这个代码还过得去哈,其实当查询的条件多一点,这种代码就会变得又臭又长,而且只支持基础的字符串类型的字段查询,如果查询条件有时间筛选的话就不支持了,在复杂点多表关联的话就更GG了,所以这种方式不合格直接上黑名单了。
第二种继承JpaSpecificationExecutor方式:
JPA 2引入了一个标准API,您可以使用它来以编程方式构建查询。Spring Data JPA提供了使用JPA标准API定义此类规范的API。这种方式首先需要继承JpaSpecificationExecutor接口,下面我们用这种方式实现和上面相同语义的查询:
1 public void testJpaSpecificationQuery() { 2 String templateName = "kk"; 3 Specification specification = (Specification) (root, query, criteriaBuilder) -> { 4 Predicate predicate = criteriaBuilder.like(root.get("templateName"),templateName); 5 query.where(predicate); 6 return predicate; 7 }; 8 9 Pageable pageable = PageRequest.of(0, 2); 10 Page<SendLog> logPage = sendLogRepository.findAll(specification, pageable); 11 }
这种方式显然更对味口了吧,而且也支持复杂的查询条件拼接,比如日期等。唯一的缺憾是领袖对象的属性字符串需要手写了,而且接口只会提供findAll(@Nullable Specification<T> spec, Pageable pageable)方法,各种复杂查询逻辑拼接都要写在service层。对于架构分层思想流行了这么多年外加强迫症的人来说实在是不能忍,如果单独封装一个Dao类编写复杂的查询又显的有点多余和臃肿
Spring Data Jpa最佳实践
在详细介绍最佳实践前,先思考和了解一个东西,Spring Data Jpa是怎么做到继承一个接口就能实现各种复杂查询的呢?这里其实是一个典型的代理模式的应用,只要继承了最底层的Repository接口,在应用启动时就会帮你生成一个代理实例,而真正的目标类才是最终执行查询的类,这个类就是:SimpleJpaRepository,它实现了JpaRepository、JpaSpecificationExecutor的所有接口,所以只要基于SimpleJpaRepository定制Repository基类,就能拥有继承接口一样的查询功能,而且可以在实现类里编写复杂的查询方法了。
一、继承SimpleJpaRepository实现类
1/** 2 * @author: kl @kailing.pub 3 * @date: 2019/11/8 4 */ 5public abstract class BaseJpaRepository<T, ID> extends SimpleJpaRepository<T, ID> { 6 7 public EntityManager em; 8 9 BaseJpaRepository(Class<T> domainClass, EntityManager em) { 10 super(domainClass, em); 11 this.em = em; 12 } 13}
构造一个SimpleJpaRepository实例,只需要一个领域对象的类型,和EntityManager 实例即可,EntityManager在Spring的上下文中已经有了,会自动注入。领域对象类型在具体的实现类中注入即可。如:
1/** 2 * @author: kl @kailing.pub 3 * @date: 2019/11/11 4 */ 5@Repository 6public class SendLogJpaRepository extends BaseJpaRepository<SendLog,Integer> { 7 8 public SendLogJpaRepository(EntityManager em) { 9 super(SendLog.class, em); 10 } 11 /** 12 * 原生查询 13 * @param templateName 14 * @return 15 */ 16 public SendLog findByTemplateName(String templateName){ 17 String sql = "select * from send_log where templateName = :templateName"; 18 Query query =em.createNativeQuery(sql); 19 query.setParameter("templateName",templateName); 20 return (SendLog) query.getSingleResult(); 21 } 22 23 /** 24 * hql查询 25 * @param templateName 26 * @return 27 */ 28 public SendLog findByTemplateNameNative(String templateName){ 29 String hql = "from SendLog where templateName = :templateName"; 30 TypedQuery<SendLog> query =em.createQuery(hql,SendLog.class); 31 query.setParameter("templateName",templateName); 32 return query.getSingleResult(); 33 } 34 35 /** 36 * JPASpecification 实现复杂分页查询 37 * @param logDto 38 * @param pageable 39 * @return 40 */ 41 public Page<SendLog> findAll(SendLogDto logDto,Pageable pageable) { 42 Specification specification = (Specification) (root, query, criteriaBuilder) -> { 43 Predicate predicate = criteriaBuilder.conjunction(); 44 if(!StringUtils.isEmpty(logDto.getTemplateName())){ 45 predicate.getExpressions().add( criteriaBuilder.like(root.get("templateName"),logDto.getTemplateName())); 46 } 47 if(logDto.getStartTime() !=null){ 48 predicate.getExpressions().add(criteriaBuilder.greaterThanOrEqualTo(root.get("createTime").as(Timestamp.class),logDto.getStartTime())); 49 } 50 query.where(predicate); 51 return predicate; 52 }; 53 return findAll(specification, pageable); 54 } 55} 56
通过继承BaseJpaRepository,使SendLogJpaRepository拥有了JpaRepository、JpaSpecificationExecutor接口中定义的所有方法功能。而且基于抽象基类中EntityManager实例,也可以非常方便的编写HQL和原生SQL查询等。最赏心悦目的是不仅拥有了最基本的CURD等功能,而且超复杂的分页查询也不分家了。只是JpaSpecification查询方式还不是特别出彩,下面继续最佳实践
二、集成QueryDsl结构化查询
Querydsl是一个框架,可通过其流畅的API来构造静态类型的类似SQL的查询。这是Spring Data Jpa文档中对QueryDsl的描述。Spring Data Jpa对QueryDsl的扩展支持的比较好,基本可以无缝集成使用。Querydsl定义了一套和JpaSpecification类似的接口,使用方式上也类似,由于QueryDsl多了一个maven插件,可以在编译期间生成领域对象操作实体,所以在拼接复杂的查询条件时相比较JpaSpecification显的更灵活好用,特别在关联到多表查询的时候。下面看下怎么集成:
1、快速集成
因为之前有写过最简单的QueryDsl集成方式,所以这里就不在赘述了,具体参见《Querydsl结构化查询之jpa》,
2、丰富BaseJpaRepository基类
1/** 2 * @author: kl @kailing.pub 3 * @date: 2019/11/8 4 */ 5public abstract class BaseJpaRepository<T, ID> extends SimpleJpaRepository<T, ID> { 6 7 public EntityManager em; 8 protected final QuerydslJpaPredicateExecutor<T> jpaPredicateExecutor; 9 10 BaseJpaRepository(Class<T> domainClass, EntityManager em) { 11 super(domainClass, em); 12 this.em = em; 13 this.jpaPredicateExecutor = new QuerydslJpaPredicateExecutor<>(JpaEntityInformationSupport.getEntityInformation(domainClass, em), em, SimpleEntityPathResolver.INSTANCE, getRepositoryMethodMetadata()); 14 } 15}
在BaseJpaRepository基类中新增了QuerydslJpaPredicateExecutor实例,它是Spring Data Jpa基于QueryDsl的一个实现。用来执行QueryDsl的Predicate相关查询。集成QueryDsl后,复杂分页查询的画风就变的更加清爽了,如:
1 /** 2 * QSendLog实体是QueryDsl插件自动生成的,插件会自动扫描加了@Entity的实体,生成一个用于查询的EntityPath类 3 */ 4 private final static QSendLog sendLog = QSendLog.sendLog; 5 6 public Page<SendLog> findAll(SendLogDto logDto, Pageable pageable) { 7 BooleanExpression expression = sendLog.isNotNull(); 8 if (logDto.getStartTime() != null) { 9 expression = expression.and(sendLog.createTime.gt(logDto.getStartTime())); 10 } 11 if (!StringUtils.isEmpty(logDto.getTemplateName())) { 12 expression = expression.and(sendLog.templateName.like("%"+logDto.getTemplateName()+"%")); 13 } 14 return jpaPredicateExecutor.findAll(expression, pageable); 15 }
到目前为止,实现相同的复杂分页查询,代码已经非常的清爽和优雅了,在复杂的查询在这种模式下也变的非常的清晰。但是,这还不是十分完美的。还有两个问题需要解决下:
-
QuerydslJpaPredicateExecutor实现的方法不支持分页查询同时又有字段排序。下面是它的接口定义,可以看到,要么分页查询一步到位但是没有排序,要么排序查询返回List列表自己封装分页。
public interface QuerydslPredicateExecutor<T> { Optional<T> findOne(Predicate predicate); Iterable<T> findAll(Predicate predicate); Iterable<T> findAll(Predicate predicate, Sort sort); Iterable<T> findAll(Predicate predicate, OrderSpecifier<?>... orders); Iterable<T> findAll(OrderSpecifier<?>... orders); Page<T> findAll(Predicate predicate, Pageable pageable); long count(Predicate predicate); boolean exists(Predicate predicate); }
-
复杂的多表关联查询QuerydslJpaPredicateExecutor不支持
3、最终的BaseJpaRepository形态
Spring Data Jpa对QuerDsl的支持毕竟有限,但是QueryDsl是有这种功能的,像上面的场景就需要特别处理了。最终改造的BaseJpaRepository如下:
1/** 2 * @author: kl @kailing.pub 3 * @date: 2019/11/8 4 */ 5public abstract class BaseJpaRepository<T, ID> extends SimpleJpaRepository<T, ID> { 6 7 protected final JPAQueryFactory jpaQueryFactory; 8 protected final QuerydslJpaPredicateExecutor<T> jpaPredicateExecutor; 9 protected final EntityManager em; 10 private final EntityPath<T> path; 11 protected final Querydsl querydsl; 12 13 BaseJpaRepository(Class<T> domainClass, EntityManager em) { 14 super(domainClass, em); 15 this.em = em; 16 this.jpaPredicateExecutor = new QuerydslJpaPredicateExecutor<>(JpaEntityInformationSupport.getEntityInformation(domainClass, em), em, SimpleEntityPathResolver.INSTANCE, getRepositoryMethodMetadata()); 17 this.jpaQueryFactory = new JPAQueryFactory(em); 18 this.path = SimpleEntityPathResolver.INSTANCE.createPath(domainClass); 19 this.querydsl = new Querydsl(em, new PathBuilder<T>(path.getType(), path.getMetadata())); 20 } 21 22 protected Page<T> findAll(Predicate predicate, Pageable pageable, OrderSpecifier<?>... orders) { 23 final JPAQuery countQuery = jpaQueryFactory.selectFrom(path); 24 countQuery.where(predicate); 25 JPQLQuery<T> query = querydsl.applyPagination(pageable, countQuery); 26 query.orderBy(orders); 27 return PageableExecutionUtils.getPage(query.fetch(), pageable, countQuery::fetchCount); 28 } 29}
新增了findAll(Predicate predicate, Pageable pageable, OrderSpecifier<?>... orders)方法,用于支持复杂分页查询的同时又有字段排序的查询场景。其次的改动是引入了JPAQueryFactory实例,用于多表关联的复杂查询。使用方式如下:
1 /** 2 * QSendLog实体是QueryDsl插件自动生成的,插件会自动扫描加了@Entity的实体,生成一个用于查询的EntityPath类 3 */ 4 private final static QSendLog qSendLog = QSendLog.sendLog; 5 private final static QTemplate qTemplate = QTemplate.template; 6 7 public Page<SendLog> findAll(SendLogDto logDto, Template template, Pageable pageable) { 8 JPAQuery countQuery = jpaQueryFactory.selectFrom(qSendLog).leftJoin(qTemplate); 9 countQuery.where(qSendLog.templateCode.eq(qTemplate.code)); 10 if(!StringUtils.isEmpty(template.getName())){ 11 countQuery.where(qTemplate.name.eq(template.getName())); 12 } 13 JPQLQuery query = querydsl.applyPagination(pageable, countQuery); 14 return PageableExecutionUtils.getPage(query.fetch(), pageable, countQuery::fetchCount); 15 }
三、集成p6spy打印执行的sql
上面的功能以及十分完美了,但是谈到最佳实践似乎少了一个打印SQL的功能。在使用Jpa的结构化语义构建复杂查询时,经常会因为各种原因导致查询的结果集不是自己想要的,但是又没法排查,因为不知道最终执行的sql是怎么样的。Spring Data Jpa也有打印sql的功能,但是比较鸡肋,它打印的是没有替换查询参数的sql,没法直接复制执行。所以这里推荐一个工具p6spy,p6spy是一个打印最终执行sql的工具,而且可以记录sql的执行耗时。使用起来也比较方便,简单三步集成:
1、引入依赖
1 <dependency> 2 <groupId>p6spy</groupId> 3 <artifactId>p6spy</artifactId> 4 <version>${p6spy.version}</version> 5 </dependency>
2、修改数据源链接字符串
jdbc:mysql://127.0.0.1:3306 改成 jdbc:p6spy:mysql://127.0.0.1:3306
3、添加配置spy.propertis配置
1appender=com.p6spy.engine.spy.appender.Slf4JLogger 2logMessageFormat=com.p6spy.engine.spy.appender.CustomLineFormat 3customLogMessageFormat = executionTime:%(executionTime)| sql:%(sqlSingleLine)
这个是最简化的自定义打印的配置,更多配置可参考:https://p6spy.readthedocs.io/en/latest/configandusage.html
结语
最后的BaseJpaRepository功能上基本满足了所有的查询需求,又做了基础查询和复杂查询的不分离,不至于把大量的复杂查询拼接逻辑写到service层,或者是新建的复杂查询类里。彻底解决了文首提出的那些问题。基于QueryDsl的复杂查询代码逻辑清晰,结构优雅,极力推荐使用。最后,在安利下p6spy,一个非常实用的打印sql的工具,可以帮助排查分析JPA最终生成执行的sql语句,其打印的sql语句可以直接复制到mysql管理工具中执行的。
作者简介:
陈凯玲,2016年5月加入凯京科技。现任凯京科技研发中心架构组经理,救火队队长。独立博客KL博客(http://www.kailing.pub)博主。