【源码】Spring Data JPA原理解析之Repository自定义方法命名规则执行原理(二)

 Spring Data JPA系列

1、SpringBoot集成JPA及基本使用

2、Spring Data JPA Criteria查询、部分字段查询

3、Spring Data JPA数据批量插入、批量更新真的用对了吗

4、Spring Data JPA的一对一、LazyInitializationException异常、一对多、多对多操作

5、Spring Data JPA自定义Id生成策略、复合主键配置、Auditing使用

6、【源码】Spring Data JPA原理解析之Repository的自动注入(一)

7、【源码】Spring Data JPA原理解析之Repository的自动注入(二)

8、【源码】Spring Data JPA原理解析之Repository执行过程及SimpleJpaRepository源码

9、【源码】Spring Data JPA原理解析之Repository自定义方法命名规则执行原理(一)

10、【源码】Spring Data JPA原理解析之Repository自定义方法命名规则执行原理(二)

11、【源码】Spring Data JPA原理解析之Repository自定义方法添加@Query注解的执行原理

前言

上一篇限于篇幅,只分享了Repository自定义方法命名规则的方法在QueryExecutorMethodInterceptor的构造方法中,通过查询查找策略CreateIfNotFoundQueryLookupStrategy创建一个PartTreeJpaQuery对象。该对象解析方法名称的关键字、查询属性、查询关键字,封装成PartTree。而后将Method和PartTreeJpaQuery组合存放在QueryExecutorMethodInterceptor的Map<Method, RepositoryQuery> queries中。

本篇继续往下分享Repository自定义方法命名规则的方法是如何调用执行的。

方法调用拦截

【源码】Spring Data JPA原理解析之Repository的自动注入(二)-CSDN博客

上面博文分享了Repository bean的创建。Respository的bean是一个通过ProxyFactory创建的动态代理对象,该代理对象添加了QueryExecutorMethodInterceptor拦截器。

【源码】Spring Data JPA原理解析之Repository执行过程及SimpleJpaRepository源码-CSDN博客

博客中介绍了动态代理拦截,当Repository中的接口被调用的时候,会执行ReflectiveMethodInvocation.proceed()的方法,在该方法中,循环遍历所有的拦截器,执行拦截器的invoke(MethodInvocation invocation)方法。

所以会执行QueryExecutorMethodInterceptor.invoke()方法。QueryExecutorMethodInterceptor的相关代码如下:

package org.springframework.data.repository.core.support;/*** 此MethodInterceptor拦截对自定义实现的方法的调用,当自定义的方法被调用时,会被该类拦截。* 此外,它还解析对finder的方法调用,并触发它们的执行。如果返回true,则可以依赖于设置自定义存储库实现实例。*/
class QueryExecutorMethodInterceptor implements MethodInterceptor {// Repository信息,为DefaultRepositoryInformation对象。获取Repository信息,getRepositoryInformation()返回一个RepositoryInformation对象。// 如子类JpaRepositoryFactory,指定baseClass为SimpleJpaRepository.classprivate final RepositoryInformation repositoryInformation;// 方法缓存,key为方法,value为对应方法的查询解析信息private final Map<Method, RepositoryQuery> queries;// 方法调用缓存,key为方法,value为对应方法调用时要执行的执行器private final Map<Method, RepositoryMethodInvoker> invocationMetadataCache = new ConcurrentReferenceHashMap<>();// 查询执行结果处理器private final QueryExecutionResultHandler resultHandler;// 在实体类中添加@NamedQueries注解,配置相关查询信息,默认为空private final NamedQueries namedQueries;private final List<QueryCreationListener<?>> queryPostProcessors;private final RepositoryInvocationMulticaster invocationMulticaster;// 省略其他@Override@Nullablepublic Object invoke(@SuppressWarnings("null") MethodInvocation invocation) throws Throwable {Method method = invocation.getMethod();// 通过返回的返回值,获取执行适配器,默认都为nullQueryExecutionConverters.ExecutionAdapter executionAdapter = QueryExecutionConverters //.getExecutionAdapter(method.getReturnType());if (executionAdapter == null) {return resultHandler.postProcessInvocationResult(doInvoke(invocation), method);}return executionAdapter //.apply(() -> resultHandler.postProcessInvocationResult(doInvoke(invocation), method));}@Nullableprivate Object doInvoke(MethodInvocation invocation) throws Throwable {Method method = invocation.getMethod();// 判断方法是否存在RepositoryQuery。在构造函数中,会解析Repository中的查询方法,并缓存到Mapif (hasQueryFor(method)) {RepositoryMethodInvoker invocationMetadata = invocationMetadataCache.get(method);if (invocationMetadata == null) {// 首次执行对应方法,先创建一个RepositoryQueryMethodInvoker对象,保存方法即方法对应的RepositoryQueryinvocationMetadata = RepositoryMethodInvoker.forRepositoryQuery(method, queries.get(method));// 加入缓存invocationMetadataCache.put(method, invocationMetadata);}// 获取方法所在的Repository类名、方法的参数值【invocation.getArguments()】,执行RepositoryQueryMethodInvoker.invoke()方法return invocationMetadata.invoke(repositoryInformation.getRepositoryInterface(), invocationMulticaster,invocation.getArguments());}// 如果能够处理该查询方法,则不执行invocation.proceed(),即结束拦截器链return invocation.proceed();}/*** 判断是否为给定方法执行查询*/private boolean hasQueryFor(Method method) {return queries.containsKey(method);}}

1.1 在QueryExecutorMethodInterceptor.invoke()中,核心功能如下:

1)执行doInvoke()方法,执行数据库相关操作,获取返回信息;

2)执行resultHandler.postProcessInvocationResult(),进行返回值类型转换;

1.2 在doInvoke()方法中,执行如下:

1)调用hasQueryFor()方法,判断当前方法是否有对应的RepositoryQuery对象。在上一篇博文中以及做了详细介绍,该对象是在QueryExecutorMethodInterceptor的构造方法中解析方法信息后封装的和查询相关的信息对象;

2)如果存在RepositoryQuery对象,则执行RepositoryMethodInvoker.forRepositoryQuery(method, queries.get(method)),创建一个RepositoryQueryMethodInvoker对象,然后执行RepositoryQueryMethodInvoker.invoke()方法;

3)如果不存在RepositoryQuery对象,则执行invocation.proceed(),执行ReflectiveMethodInvocation.proceed()方法,继续执行下一个拦截器或执行target的对应方法;

RepositoryQueryMethodInvoker

RepositoryQueryMethodInvoker查询方法回调类的核心代码如下:

abstract class RepositoryMethodInvoker {private final Method method;private final Class<?> returnedType;private final Invokable invokable;private final boolean suspendedDeclaredMethod;@SuppressWarnings("ReactiveStreamsUnusedPublisher")protected RepositoryMethodInvoker(Method method, Invokable invokable) {this.method = method;if (KotlinDetector.isKotlinReflectPresent()) {// 省略其他} else {this.suspendedDeclaredMethod = false;this.returnedType = method.getReturnType();this.invokable = invokable;}}static RepositoryQueryMethodInvoker forRepositoryQuery(Method declaredMethod, RepositoryQuery query) {return new RepositoryQueryMethodInvoker(declaredMethod, query);}@Nullablepublic Object invoke(Class<?> repositoryInterface, RepositoryInvocationMulticaster multicaster, Object[] args)throws Exception {return doInvoke(repositoryInterface, multicaster, args);}@Nullableprivate Object doInvoke(Class<?> repositoryInterface, RepositoryInvocationMulticaster multicaster, Object[] args)throws Exception {// 创建一个RepositoryMethodInvocationCaptor对象RepositoryMethodInvocationCaptor invocationResultCaptor = RepositoryMethodInvocationCaptor.captureInvocationOn(repositoryInterface);try {// 执行对应方法的RepositoryQuery的execute方法Object result = invokable.invoke(args);if (result != null && ReactiveWrappers.supports(result.getClass())) {return new ReactiveInvocationListenerDecorator().decorate(repositoryInterface, multicaster, args, result);}if (result instanceof Stream) {return ((Stream<?>) result).onClose(() -> multicaster.notifyListeners(method, args, computeInvocationResult(invocationResultCaptor.success())));}// 执行结果通知。回调RepositoryMethodInvocationListener.afterInvocation()multicaster.notifyListeners(method, args, computeInvocationResult(invocationResultCaptor.success()));return result;} catch (Exception e) {multicaster.notifyListeners(method, args, computeInvocationResult(invocationResultCaptor.error(e)));throw e;}}private RepositoryMethodInvocation computeInvocationResult(RepositoryMethodInvocationCaptor captured) {return new RepositoryMethodInvocation(captured.getRepositoryInterface(), method, captured.getCapturedResult(),captured.getDuration());}interface Invokable {@NullableObject invoke(Object[] args) throws Exception;}private static class RepositoryQueryMethodInvoker extends RepositoryMethodInvoker {public RepositoryQueryMethodInvoker(Method method, RepositoryQuery repositoryQuery) {// repositoryQuery::execute方法回调声明作为参数赋值给invokable,当执行invokable.invoke()时,// 执行repositoryQuery.execute()方法super(method, repositoryQuery::execute);}}// 省略其他
}

RepositoryQueryMethodInvoker是私有静态内部类,父类为RepositoryMethodInvoker。

在上面讲解的1.2的2)中,通过RepositoryMethodInvoker.forRepositoryQuery(method, queries.get(method)),创建一个RepositoryQueryMethodInvoker对象,将repositoryQuery::execute方法回调声明作为参数赋值给invokable。

当执行RepositoryQueryMethodInvoker.invoke()时,执行doInvoke()方法,该方法执行如下:

1)创建一个RepositoryMethodInvocationCaptor对象;

2)执行invokable.invoke(),即执行对应方法的RepositoryQuery的execute方法,执行数据库操作;

RepositoryQuery.execute() -> AbstractJpaQuery.execute() -> AbstractJpaQuery.doExecute() -> JpaQueryExecution.execute() -> JpaQueryExecution.doExecute()。

3)执行结果通知。回调RepositoryMethodInvocationListener.afterInvocation();

4)返回2)中的返回值;

第2)中调用的JpaQueryExecution.doExecute()是一个抽象方法,实现类如下:

针对数据库表操作后不同的返回值信息,使用不同的实现类,且实现类都是JpaQueryExecution的内部类。以下以SingleEntityExecution为例。

public abstract class JpaQueryExecution {static class SingleEntityExecution extends JpaQueryExecution {@Overrideprotected Object doExecute(AbstractJpaQuery query, JpaParametersParameterAccessor accessor) {return query.createQuery(accessor).getSingleResult();}}
}

执行AbstractJpaQuery.createQuery(),获取一个Query对象,最后调用Query.getSingleResult(),返回一个查询执行结果。其他的实现类处理类似,只是最后调用Query的不同方法,从数据库中查询不同的结果值。

AbstractJpaQuery

AbstractJpaQuery的相关代码如下:

/*** 记录Repository中每个方法解析后的信息*/
public abstract class AbstractJpaQuery implements RepositoryQuery {private final JpaQueryMethod method;private final EntityManager em;private final JpaMetamodel metamodel;// 根据EntityManager,返回PersistenceProvider。PersistenceProvider是枚举类型,有HIBERNATE、ECLIPSELINK、GENERIC_JPA。// 不同的PersistenceProvider,extractQueryString、getIdentifierFrom等方式不一样private final PersistenceProvider provider;// 根据查询方法的返回值,使用不同的执行器private final Lazy<JpaQueryExecution> execution;// 参数绑定器final Lazy<ParameterBinder> parameterBinder = Lazy.of(this::createBinder);@Nullable@Overridepublic Object execute(Object[] parameters) {return doExecute(getExecution(), parameters);}/*** 执行查询* @param execution 执行器。主要根据方法的返回值确定的执行器* @param values 方法执行时的参数值* @return*/@Nullableprivate Object doExecute(JpaQueryExecution execution, Object[] values) {// 创建一个JpaParametersParameterAccessor对象,保存方法的参数信息及本次查询的参数值JpaParametersParameterAccessor accessor = obtainParameterAccessor(values);// 执行数据库查询,获取返回值Object result = execution.execute(this, accessor);ResultProcessor withDynamicProjection = method.getResultProcessor().withDynamicProjection(accessor);return withDynamicProjection.processResult(result, new TupleConverter(withDynamicProjection.getReturnedType()));}private JpaParametersParameterAccessor obtainParameterAccessor(Object[] values) {if (method.isNativeQuery() && PersistenceProvider.HIBERNATE.equals(provider)) {return new HibernateJpaParametersParameterAccessor(method.getParameters(), values, em);}return new JpaParametersParameterAccessor(method.getParameters(), values);}/*** 获取方法对应的查询执行器*/protected JpaQueryExecution getExecution() {// 获取根据返回值确定的查询的执行器JpaQueryExecution execution = this.execution.getNullable();if (execution != null) {return execution;}// 如果添加了@Modify注解,则返回if (method.isModifyingQuery()) {return new ModifyingExecution(method, em);}// 否则返回单个实体类的执行器return new SingleEntityExecution();}/*** 为query添加定义的查询hint信息。方法中添加@QueryHints注解*/protected <T extends Query> T applyHints(T query, JpaQueryMethod method) {List<QueryHint> hints = method.getHints();if (!hints.isEmpty()) {for (QueryHint hint : hints) {applyQueryHint(query, hint);}}// Apply any meta-attributes that existif (method.hasQueryMetaAttributes()) {if (provider.getCommentHintKey() != null) {query.setHint( //provider.getCommentHintKey(), provider.getCommentHintValue(method.getQueryMetaAttributes().getComment()));}}return query;}protected <T extends Query> void applyQueryHint(T query, QueryHint hint) {Assert.notNull(query, "Query must not be null");Assert.notNull(hint, "QueryHint must not be null");query.setHint(hint.name(), hint.value());}/*** 为query应用锁模式*/private Query applyLockMode(Query query, JpaQueryMethod method) {LockModeType lockModeType = method.getLockModeType();return lockModeType == null ? query : query.setLockMode(lockModeType);}protected Query createQuery(JpaParametersParameterAccessor parameters) {return applyLockMode(applyEntityGraphConfiguration(applyHints(doCreateQuery(parameters), method), method), method);}/*** 如果方法添加@EntityGraph注解,在query中添加对应的Hint* @param query* @param method* @return*/private Query applyEntityGraphConfiguration(Query query, JpaQueryMethod method) {JpaEntityGraph entityGraph = method.getEntityGraph();if (entityGraph != null) {QueryHints hints = Jpa21Utils.getFetchGraphHint(em, method.getEntityGraph(),getQueryMethod().getEntityInformation().getJavaType());hints.forEach(query::setHint);}return query;}/*** 为查询创建一个Query,并调用query.setParameter()设置参数值及分页信息*/protected abstract Query doCreateQuery(JpaParametersParameterAccessor accessor);// 省略其他
}

在createQuery()方法中,执行如下:

1)调用抽象方法doCreateQuery(),获取一个Query;

对于自定义方法命名规则的方法,实现在PartTreeJpaQuery类。

2)执行applyHints(),在query中添加对应的Hint;

3)执行applyEntityGraphConfiguration(),如果方法添加@EntityGraph注解,在query中添加对应的Hint;

4)执行applyLockMode(),为query应用锁模式;

PartTreeJpaQuery

PartTreeJpaQuery的相关代码如下:

package org.springframework.data.jpa.repository.query;/*** 保存了方法信息,包括方法、方法参数、方法名称解析后的Part树、对应的查询query、查询计数countQuery等信息*/
public class PartTreeJpaQuery extends AbstractJpaQuery {private final PartTree tree;private final JpaParameters parameters;private final QueryPreparer query;private final QueryPreparer countQuery;private final EntityManager em;private final EscapeCharacter escape;private final JpaMetamodelEntityInformation<?, Object> entityInformation;/*** 为查询创建一个Query,并调用query.setParameter()设置参数值及分页信息*/@Overridepublic Query doCreateQuery(JpaParametersParameterAccessor accessor) {return query.createQuery(accessor);}@Override@SuppressWarnings("unchecked")public TypedQuery<Long> doCreateCountQuery(JpaParametersParameterAccessor accessor) {return (TypedQuery<Long>) countQuery.createQuery(accessor);}private class QueryPreparer {// 缓存创建的对象private final @Nullable CriteriaQuery<?> cachedCriteriaQuery;private final @Nullable ParameterBinder cachedParameterBinder;private final QueryParameterSetter.QueryMetadataCache metadataCache = new QueryParameterSetter.QueryMetadataCache();QueryPreparer(boolean recreateQueries) {JpaQueryCreator creator = createCreator(null);if (recreateQueries) {this.cachedCriteriaQuery = null;this.cachedParameterBinder = null;} else {// 子类CountQueryPreparer的createQuery(),执行JpaCountQueryCreator重写的complete()方法,// 执行query.select(),select为builder.count(),并加上predicate条件信息this.cachedCriteriaQuery = creator.createQuery();this.cachedParameterBinder = getBinder(creator.getParameterExpressions());}}/*** 为查询创建一个Query,并调用query.setParameter()设置参数值及分页信息*/public Query createQuery(JpaParametersParameterAccessor accessor) {CriteriaQuery<?> criteriaQuery = cachedCriteriaQuery;ParameterBinder parameterBinder = cachedParameterBinder;if (cachedCriteriaQuery == null || accessor.hasBindableNullValue()) {JpaQueryCreator creator = createCreator(accessor);criteriaQuery = creator.createQuery(getDynamicSort(accessor));List<ParameterMetadata<?>> expressions = creator.getParameterExpressions();parameterBinder = getBinder(expressions);}if (parameterBinder == null) {throw new IllegalStateException("ParameterBinder is null");}// 通过EntityManager.createQuery(criteriaQuery),返回TypedQueryTypedQuery<?> query = createQuery(criteriaQuery);ScrollPosition scrollPosition = accessor.getParameters().hasScrollPositionParameter()? accessor.getScrollPosition(): null;// 调用invokeBinding()执行query.setParameter()方法,设置查询的条件参数值,如果有分页,设置分页信息// 如果有需要,设置返回最大值信息return restrictMaxResultsIfNecessary(invokeBinding(parameterBinder, query, accessor, this.metadataCache),scrollPosition);}@SuppressWarnings("ConstantConditions")private Query restrictMaxResultsIfNecessary(Query query, @Nullable ScrollPosition scrollPosition) {if (scrollPosition instanceof OffsetScrollPosition offset && !offset.isInitial()) {query.setFirstResult(Math.toIntExact(offset.getOffset()) + 1);}if (tree.isLimiting()) {if (query.getMaxResults() != Integer.MAX_VALUE) {if (query.getMaxResults() > tree.getMaxResults() && query.getFirstResult() > 0) {query.setFirstResult(query.getFirstResult() - (query.getMaxResults() - tree.getMaxResults()));}}query.setMaxResults(tree.getMaxResults());}if (tree.isExistsProjection()) {query.setMaxResults(1);}return query;}/*** 通过EntityManager.createQuery(criteriaQuery),返回TypedQuery*/private TypedQuery<?> createQuery(CriteriaQuery<?> criteriaQuery) {if (this.cachedCriteriaQuery != null) {synchronized (this.cachedCriteriaQuery) {return getEntityManager().createQuery(criteriaQuery);}}return getEntityManager().createQuery(criteriaQuery);}protected JpaQueryCreator createCreator(@Nullable JpaParametersParameterAccessor accessor) {EntityManager entityManager = getEntityManager();CriteriaBuilder builder = entityManager.getCriteriaBuilder();ResultProcessor processor = getQueryMethod().getResultProcessor();ParameterMetadataProvider provider;ReturnedType returnedType;if (accessor != null) {provider = new ParameterMetadataProvider(builder, accessor, escape);returnedType = processor.withDynamicProjection(accessor).getReturnedType();} else {provider = new ParameterMetadataProvider(builder, parameters, escape);returnedType = processor.getReturnedType();}if (accessor != null && accessor.getScrollPosition() instanceof KeysetScrollPosition keyset) {return new JpaKeysetScrollQueryCreator(tree, returnedType, builder, provider, entityInformation, keyset);}return new JpaQueryCreator(tree, returnedType, builder, provider);}/*** 调用query.setParameter()方法,设置查询的条件参数值,如果有分页,设置分页信息*/protected Query invokeBinding(ParameterBinder binder, TypedQuery<?> query, JpaParametersParameterAccessor accessor,QueryParameterSetter.QueryMetadataCache metadataCache) {// 将query查询添加到缓存QueryParameterSetter.QueryMetadata metadata = metadataCache.getMetadata("query", query);return binder.bindAndPrepare(query, metadata, accessor);}private ParameterBinder getBinder(List<ParameterMetadata<?>> expressions) {return ParameterBinderFactory.createCriteriaBinder(parameters, expressions);}private Sort getDynamicSort(JpaParametersParameterAccessor accessor) {return parameters.potentiallySortsDynamically() //? accessor.getSort() //: Sort.unsorted();}}private class CountQueryPreparer extends QueryPreparer {CountQueryPreparer(boolean recreateQueries) {super(recreateQueries);}/*** 创建一个JpaCountQueryCreator*/@Overrideprotected JpaQueryCreator createCreator(@Nullable JpaParametersParameterAccessor accessor) {EntityManager entityManager = getEntityManager();CriteriaBuilder builder = entityManager.getCriteriaBuilder();ParameterMetadataProvider provider;if (accessor != null) {provider = new ParameterMetadataProvider(builder, accessor, escape);} else {provider = new ParameterMetadataProvider(builder, parameters, escape);}return new JpaCountQueryCreator(tree, getQueryMethod().getResultProcessor().getReturnedType(), builder, provider);}@Overrideprotected Query invokeBinding(ParameterBinder binder, TypedQuery<?> query, JpaParametersParameterAccessor accessor,QueryParameterSetter.QueryMetadataCache metadataCache) {QueryParameterSetter.QueryMetadata metadata = metadataCache.getMetadata("countquery", query);return binder.bind(query, metadata, accessor);}}
}

1)在PartTreeJpaQuery.doCreateQuery()方法,执行QueryPreparer.createQuery()方法。

2)QueryPreparer.createQuery()方法先调用createQuery(),执行如下:

2.1)通过EntityManager.createQuery(criteriaQuery),返回TypedQuery;

2.2)执行invokeBinding(),在TypedQuery对象中,调用query.setParameter()绑定查询条件的参数值,如果有分页,设置分页信息;

通过执行ParameterBinder.bindAndPrepare()方法,调用query.setParameter()绑定查询条件的参数值,如果有分页,设置分页信息。

2.3)执行restrictMaxResultsIfNecessary(),如果有需要,设置返回最大值信息;

ParameterBinder

ParameterBinder的代码如下:

package org.springframework.data.jpa.repository.query;/*** ParameterBinder用于将方法参数绑定到Query。通常在执行AbstractJpaQuery时执行。*/
public class ParameterBinder {static final String PARAMETER_NEEDS_TO_BE_NAMED = "For queries with named parameters you need to provide names for method parameters; Use @Param for query method parameters, or when on Java 8+ use the javac flag -parameters";private final JpaParameters parameters;// 查询方法对应的参数设置器private final Iterable<QueryParameterSetter> parameterSetters;private final boolean useJpaForPaging;ParameterBinder(JpaParameters parameters, Iterable<QueryParameterSetter> parameterSetters) {this(parameters, parameterSetters, true);}public ParameterBinder(JpaParameters parameters, Iterable<QueryParameterSetter> parameterSetters,boolean useJpaForPaging) {Assert.notNull(parameters, "JpaParameters must not be null");Assert.notNull(parameterSetters, "Parameter setters must not be null");this.parameters = parameters;this.parameterSetters = parameterSetters;this.useJpaForPaging = useJpaForPaging;}public <T extends Query> T bind(T jpaQuery, QueryParameterSetter.QueryMetadata metadata,JpaParametersParameterAccessor accessor) {// 绑定参数值bind(metadata.withQuery(jpaQuery), accessor, ErrorHandling.STRICT);return jpaQuery;}public void bind(QueryParameterSetter.BindableQuery query, JpaParametersParameterAccessor accessor,ErrorHandling errorHandling) {// 遍历方法的参数设置器,调用QueryParameterSetter.setParameter() -> query.setParameter()为查询语句赋值for (QueryParameterSetter setter : parameterSetters) {setter.setParameter(query, accessor, errorHandling);}}Query bindAndPrepare(Query query, QueryParameterSetter.QueryMetadata metadata,JpaParametersParameterAccessor accessor) {// 绑定参数。调用query.setParameter(),为查询赋值bind(query, metadata, accessor);// 如果没有分页,直接返回if (!useJpaForPaging || !parameters.hasLimitingParameters() || accessor.getPageable().isUnpaged()) {return query;}// 设置分页信息query.setFirstResult(PageableUtils.getOffsetAsInteger(accessor.getPageable()));query.setMaxResults(accessor.getPageable().getPageSize());return query;}
}

小结

限于篇幅,本篇先分享到这里。结合上一篇【源码】Spring Data JPA原理解析之Repository自定义方法命名规则执行原理(一)一起做一个小结:

1)Repository的代理类中,会添加QueryExecutorMethodInterceptor方法拦截器;

2)QueryExecutorMethodInterceptor方法拦截器的构造方法中,会根据查询查找策略CreateIfNotFoundQueryLookupStrategy,获得RepositoryQuery对象,解析方法。对于按方法命名规则实现的方法,使用的RepositoryQuery对象为PartTreeJpaQuery;

3)在PartTreeJpaQuery构造方法中,创建一个PartTree对象,解析方法名称中的起始关键字【如:findBy、readBy、deleteBy等】、条件属性【实体类中的属性】、查询关键字【Between、In、Equals等】;

4)创建对应方法的countQuery和query,将解析出的查询的基础信息封装在QueryPreparer对象中,根据解析出的查询信息,创建CriteriaQuery对象;

5)解析完方法信息,保存在PartTreeJpaQuery后,保存到QueryExecutorMethodInterceptor的Map<Method, RepositoryQuery> queries中;

6)当Repository的接口被调用的时候,在ReflectiveMethodInvocation.proceed()中,先执行QueryExecutorMethodInterceptor.invoke()方法;

6.1)调用doInvoke()方法,获取数据库执行后的数据;

6.1.1)调用RepositoryQueryMethodInvoker.invoke() -> RepositoryQuery.execute() -> AbstractJpaQuery.execute() -> AbstractJpaQuery.doExecute() -> JpaQueryExecution.execute() -> JpaQueryExecution.doExecute();

6.1.2)doExecute()是一个抽象方法,针对不同的数据库查询返回值信息,使用不同的实现类。所有的实现类都会先调用AbstractJpaQuery.createQuery(),获取一个Query对象;

6.1.3)在AbstractJpaQuery.createQuery()中,调用抽象方法doCreateQuery()。对于按方法命名规则的Repository接口,实现类为PartTreeJpaQuery;

6.1.4)在PartTreeJpaQuery.coCreateQuery()方法中,通过EntityManager.createQuery(criteriaQuery)返回TypedQuery,然后执行invokeBinding(),在TypedQuery对象中,调用query.setParameter()绑定查询条件的参数值,如果有分页,设置分页信息;

6.1.5)参数完参数,在6.1.3中设置hint等。然后执行6.1.2中的具体实现类,执行数据库查询。如SingleEntityExecution实现类,执行TypeQuery.getSingleResult(),然后单个结果;

6.2)调用resultHandler.postProcessInvocationResult(),对数据库查询后的值进行返回值类型转换;

关于本篇内容你有什么自己的想法或独到见解,欢迎在评论区一起交流探讨下吧。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/bicheng/20054.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Java垃圾回收_1

一、垃圾回收 1.如何判断对象可以回收 &#xff08;1&#xff09;引用计数法 存在循环引用问题&#xff0c; Java未使用这种算法 在引用计数法中&#xff0c;每个对象都有一个引用计数器&#xff0c;记录着指向该对象的引用数量。当引用计数器为零时&#xff0c;表示没有任…

JavaSE:SE知识整体总结

1、引言 历时一个多月的学习&#xff0c;已经掌握了JavaSE的知识&#xff0c;这篇博客就来做一下SE知识的总结~ 2、数据类型和变量 Java中的数据类型分为基本数据类型和引用数据类型。 2.1 基本数据类型 基本数据类型共有四类八种&#xff1a; 四类&#xff1a;整形、浮点…

在phpstorm2024版里如何使用Jetbrains ai assistant 插件 ?

ai assistant激活成功后&#xff0c;如图 ai assistant渠道&#xff1a;https://web.52shizhan.cn/activity/ai-assistant 在去年五月份的 Google I/O 2023 上&#xff0c;Google 为 Android Studio 推出了 Studio Bot 功能&#xff0c;使用了谷歌编码基础模型 Codey,Codey 是…

SpringBoot HelloWorld 之 实现注册功能

SpringBoot HelloWorld 之 实现注册功能 一.配置 创建数据库big_event CREATE TABLE user (id int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT ID,username varchar(20) COLLATE utf8_unicode_ci NOT NULL COMMENT 用户名,password varchar(32) COLLATE utf8_unicode_ci …

Vue3项目练习详细步骤(第五部分:用户模块的功能)

顶部导航栏个人信息显示 接口文档 接口请求与绑定 导航栏下拉菜单功能 路由实现 退出登录和路由跳转实现 基本资料修改 页面结构 接口文档 接口请求与绑定 修改头像 页面结构 头像回显 头像上传 接口文档 重置密码 页面结构 接口文档 接口请求与绑定 顶部导航…

自然语言处理学习路线

学习目标 NLP 系统知识&#xff08;从入门到入土&#xff09; 学习内容 NLP的基本流程&#xff1a;&#xff08;待更&#xff09;文本预处理&#xff08;标点符号处理、繁体转简体、分词Tokenizer&#xff09;&#xff1a;&#xff08;待更&#xff09;词袋模型&#xff08;TF…

红外热成像观驱一体仪,夜间驱鸟新利器

夜间驱鸟是机场鸟防工作的重点和难点&#xff0c;但红外热成像观驱一体仪的出现解决了这个问题&#xff0c;它结合了红外热成像技术和激光驱鸟技术&#xff0c;极大地提升了夜间驱鸟工作的效率和安全性。 驱鸟技术详解&#xff1a; 在夜晚低能见度的环境下&#xff0c;红外热成…

基于开源项目ESP32 SVPWM驱动无刷电机开环速度测试

基于开源项目ESP32 SVPWM驱动无刷电机开环速度测试 ✨本篇硬件电路和代码来源于此开源项目&#xff1a;https://github.com/MengYang-x/STM3F401-FOC/tree/main&#x1f4cd;硬件电路和项目介绍&#xff0c;立创开源广场&#xff1a;https://oshwhub.com/shadow27/tai-yang-nen…

2024 HN CTF WebMisc 部分 wp

Web ez_tp 判断是thinkphp 3.2 参考官方手册:https://www.kancloud.cn/manual/thinkphp/1697 判断路由模式 URL_CASE_INSENSITIVE > true, // 默认false 表示URL区分大小写 true则表示不区分大小写URL_MODEL > 1, // URL访问模式,可选参数0、1、…

Python使用动态代理的多元应用

Python作为一种功能强大且易于学习的编程语言&#xff0c;在网络编程领域具有广泛的应用。当Python与动态代理技术结合时&#xff0c;便开启了一扇通往更多可能性的大门。以下将深入探讨Python使用动态代理可以实现的多种应用。 首先&#xff0c;Python结合动态代理在网络爬虫…

中文多模态InternVL-Chat-V1-5,中文理解能力强劲,8 项指标超越商业模型,性能媲美 GPT-4V

前言 近年来&#xff0c;多模态大型语言模型&#xff08;MLLM&#xff09;的快速发展&#xff0c;为人工智能在图像、文本等多模态信息理解和处理方面带来了前所未有的突破。然而&#xff0c;现有的主流多模态模型多以英文为训练语言&#xff0c;在中文理解和处理方面存在着明…

可用于嵌入式的解释器调研对比,及lua解释器介绍

嵌入式不一定只能用C! ---------------------------------------------------------------------------------------手动分割线-------------------------------------------------------------------------------- 本文章参考了以下文章&#xff1a; 这里是引用 ------------…

1113 钱串子的加法

idea 测试点3&#xff1a;输入的两个整数都是0测试点4.5&#xff1a;大数&#xff0c;需要用大数加法 solution1(测试点4&#xff0c;5不通过) 直接相加再转30进制 #include<iostream> #include<string> using namespace std; typedef long long ll; string a,…

linux sed命令替换文件端口

1、需求描述&#xff1a;因sed -i ‘s/旧端口/新端口/g’ 文件&#xff0c;替换会直接增加端口导致端口直接追加后面&#xff0c;因此需要修改 要求&#xff1a;2300替换为23003&#xff0c;23001替换为23004 <value>192.168.1.133</value></constructor-arg>…

windows 10下conda环境目录转移

目录 一&#xff1a;背景 二&#xff1a;转移过程 三&#xff1a;环境验证 一&#xff1a;背景 最近用conda安装了几个python环境&#xff0c;随着安装包和数据的不断增大&#xff0c;发现C盘占用空间一直在增加&#xff0c;已经有十几个G了&#xff0c;系统也变的越来越慢。…

【深度学习】安全帽检测,目标检测,yolov10算法,yolov10训练

文章目录 一、数据集二、yolov10介绍三、数据voc转换为yolo四、训练五、验证六、数据、模型、训练后的所有文件 寻求帮助请看这里&#xff1a; https://docs.qq.com/sheet/DUEdqZ2lmbmR6UVdU?tabBB08J2一、数据集 安全帽佩戴检测 数据集&#xff1a;https://github.com/njvi…

MySql part1 安装和介绍

MySql part1 安装和介绍 数据 介绍 什么是数据库&#xff0c;数据很好理解&#xff0c;一般来说数据通常是我们所认识的 描述事物的符号记录&#xff0c; 可以是数字、 文字、图形、图像、声音、语言等&#xff0c;数据有多种形式&#xff0c;它们都以经过数字化后存入计算机…

Nuxt3项目实现 OG:Image

目录 前言 1、安装 2、设置网站 URL 3、启用 Nuxt DevTools 4、创建您的第一个Og:Image a. 定义OG镜像 b. 查看您的Og:Image 5、自定义NuxtSeo模板 a. 定义 NuxtSeo模板 b. 使用其他可用的社区模板 6、创建自己的模板 a. 定义组件 BlogPost.vue b. 使用新模板 c.…

python实训——回归类型数据挖掘任务

回归类型数据挖掘任务 基于ARIMA和多层神经网络模型的地铁站点日客流量预测。有郑州市2015年8月-11月各地铁闸机刷卡数据集。对每日各地铁站的客流量进行分析并进行可视化。基于上一步的分析结果&#xff0c;分别采用ARIMA模型和多层神经网络模型对数据进行建模&#xff0c;训…

Usage - hackthebox

简介 靶场&#xff1a;hackmyvm 靶机&#xff1a;Usage(10.10.11.18) 难度&#xff1a;Easy 靶机链接:https://app.hackthebox.com/machines/Usage 攻击机1&#xff1a;ubuntu22.04 (10.10.16.21) 攻击机2&#xff1a;windows11(10.10.14.33) 扫描 nmap起手 nmap -sT …