DATAJDBC-166 - Allow registration of RowMappers based on result type.

RowMapper can be configured either via the @Query(rowMapperClass = …​.) or by registerign a RowMapperMap bean and register RowMapper per method return type.

                @Bean
                RowMapperMap rowMappers() {
                        return new ConfigurableRowMapperMap() //
                                        .register(Person.class, new PersonRowMapper()) //
                                        .register(Address.class, new AddressRowMapper());
                }

When determining the RowMapper to use for a method the following steps are followed based on the return type of the method:

1. If the type is a simple type no RowMapper is used. Instead the query is expected to return a single row with a single column and a conversion to the return type is applied to that value.

2. The entity classes in the RowMapperMap are iterated until one is found that is a superclass or interface of the return type in question. The RowMapper registered for that class is used. Iterating happens in the order of registration, so make sure to register more general types after specific ones.

If applicable wrapper type like collections or Optional are unwrapped. So a return type of Optional<Person> will use the type Person in the steps above.
This commit is contained in:
Jens Schauder
2018-01-17 13:13:29 +01:00
committed by Greg Turnquist
parent b49b7674f2
commit a2b7699256
12 changed files with 405 additions and 18 deletions

View File

@@ -283,7 +283,7 @@ public class DefaultDataAccessStrategy implements DataAccessStrategy {
}
public <T> EntityRowMapper<T> getEntityRowMapper(Class<T> domainType) {
return new EntityRowMapper<>(getRequiredPersistentEntity(domainType), context.getConversions(), context, accessStrategy);
return new EntityRowMapper<>(getRequiredPersistentEntity(domainType), context, accessStrategy);
}
private RowMapper getMapEntityRowMapper(JdbcPersistentProperty property) {

View File

@@ -53,11 +53,11 @@ public class EntityRowMapper<T> implements RowMapper<T> {
private final DataAccessStrategy accessStrategy;
private final JdbcPersistentProperty idProperty;
public EntityRowMapper(JdbcPersistentEntity<T> entity, ConversionService conversions, JdbcMappingContext context,
DataAccessStrategy accessStrategy) {
public EntityRowMapper(JdbcPersistentEntity<T> entity, JdbcMappingContext context,
DataAccessStrategy accessStrategy) {
this.entity = entity;
this.conversions = conversions;
this.conversions = context.getConversions();
this.context = context;
this.accessStrategy = accessStrategy;

View File

@@ -0,0 +1,38 @@
/*
* Copyright 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jdbc.repository;
import org.springframework.jdbc.core.RowMapper;
/**
* A map from a type to a {@link RowMapper} to be used for extracting that type from {@link java.sql.ResultSet}s.
*
* @author Jens Schauder
*/
public interface RowMapperMap {
/**
* An immutable empty instance that will return {@literal null} for all arguments.
*/
RowMapperMap EMPTY = new RowMapperMap() {
public <T> RowMapper<? extends T> rowMapperFor(Class<T> type) {
return null;
}
};
<T> RowMapper<? extends T> rowMapperFor(Class<T> type);
}

View File

@@ -0,0 +1,61 @@
/*
* Copyright 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jdbc.repository.config;
import java.util.LinkedHashMap;
import java.util.Map;
import org.springframework.data.jdbc.repository.RowMapperMap;
import org.springframework.jdbc.core.RowMapper;
/**
* A {@link RowMapperMap} that allows for registration of {@link RowMapper}s via a fluent Api.
*
* @author Jens Schauder
*/
public class ConfigurableRowMapperMap implements RowMapperMap {
private Map<Class<?>, RowMapper<?>> rowMappers = new LinkedHashMap<>();
/**
* Registers a the given {@link RowMapper} as to be used for the given type.
*
* @return this instance, so this can be used as a fluent interface.
*/
public <T> ConfigurableRowMapperMap register(Class<T> type, RowMapper<? extends T> rowMapper) {
rowMappers.put(type, rowMapper);
return this;
}
@SuppressWarnings("unchecked")
public <T> RowMapper<? extends T> rowMapperFor(Class<T> type) {
RowMapper<? extends T> candidate = (RowMapper<? extends T>) rowMappers.get(type);
if (candidate == null) {
for (Map.Entry<Class<?>, RowMapper<?>> entry : rowMappers.entrySet()) {
if (type.isAssignableFrom(entry.getKey())) {
candidate = (RowMapper<? extends T>) entry.getValue();
}
}
}
return candidate;
}
}

View File

@@ -21,6 +21,7 @@ import org.springframework.core.convert.ConversionService;
import org.springframework.data.jdbc.core.DataAccessStrategy;
import org.springframework.data.jdbc.core.EntityRowMapper;
import org.springframework.data.jdbc.mapping.model.JdbcMappingContext;
import org.springframework.data.jdbc.repository.RowMapperMap;
import org.springframework.data.projection.ProjectionFactory;
import org.springframework.data.repository.core.NamedQueries;
import org.springframework.data.repository.core.RepositoryMetadata;
@@ -40,13 +41,15 @@ class JdbcQueryLookupStrategy implements QueryLookupStrategy {
private final JdbcMappingContext context;
private final DataAccessStrategy accessStrategy;
private final RowMapperMap rowMapperMap;
private final ConversionService conversionService;
JdbcQueryLookupStrategy(EvaluationContextProvider evaluationContextProvider, JdbcMappingContext context,
DataAccessStrategy accessStrategy) {
DataAccessStrategy accessStrategy, RowMapperMap rowMapperMap) {
this.context = context;
this.accessStrategy = accessStrategy;
this.rowMapperMap = rowMapperMap;
this.conversionService = context.getConversions();
}
@@ -55,22 +58,32 @@ class JdbcQueryLookupStrategy implements QueryLookupStrategy {
ProjectionFactory projectionFactory, NamedQueries namedQueries) {
JdbcQueryMethod queryMethod = new JdbcQueryMethod(method, repositoryMetadata, projectionFactory);
Class<?> returnedObjectType = queryMethod.getReturnedObjectType();
RowMapper<?> rowMapper = queryMethod.isModifyingQuery() ? null : createRowMapper(returnedObjectType);
RowMapper<?> rowMapper = queryMethod.isModifyingQuery() ? null : createRowMapper(queryMethod);
return new JdbcRepositoryQuery(queryMethod, context, rowMapper);
}
private RowMapper<?> createRowMapper(Class<?> returnedObjectType) {
private RowMapper<?> createRowMapper(JdbcQueryMethod queryMethod) {
Class<?> returnedObjectType = queryMethod.getReturnedObjectType();
return context.getSimpleTypeHolder().isSimpleType(returnedObjectType)
? SingleColumnRowMapper.newInstance(returnedObjectType, conversionService)
: new EntityRowMapper<>( //
context.getRequiredPersistentEntity(returnedObjectType), //
conversionService, //
: determineDefaultRowMapper(queryMethod);
}
private RowMapper<?> determineDefaultRowMapper(JdbcQueryMethod queryMethod) {
Class<?> domainType = queryMethod.getReturnedObjectType();
RowMapper typeMappedRowMapper = rowMapperMap.rowMapperFor(domainType);
return typeMappedRowMapper == null //
? new EntityRowMapper<>( //
context.getRequiredPersistentEntity(domainType), //
context, //
accessStrategy //
);
accessStrategy) //
: typeMappedRowMapper;
}
}

View File

@@ -22,6 +22,7 @@ import org.springframework.data.jdbc.core.DataAccessStrategy;
import org.springframework.data.jdbc.core.JdbcEntityTemplate;
import org.springframework.data.jdbc.mapping.model.JdbcMappingContext;
import org.springframework.data.jdbc.mapping.model.JdbcPersistentEntityInformation;
import org.springframework.data.jdbc.repository.RowMapperMap;
import org.springframework.data.jdbc.repository.SimpleJdbcRepository;
import org.springframework.data.repository.core.EntityInformation;
import org.springframework.data.repository.core.RepositoryInformation;
@@ -42,6 +43,7 @@ public class JdbcRepositoryFactory extends RepositoryFactorySupport {
private final JdbcMappingContext context;
private final ApplicationEventPublisher publisher;
private final DataAccessStrategy accessStrategy;
private RowMapperMap rowMapperMap = RowMapperMap.EMPTY;
public JdbcRepositoryFactory(ApplicationEventPublisher publisher, JdbcMappingContext context,
DataAccessStrategy dataAccessStrategy) {
@@ -84,6 +86,10 @@ public class JdbcRepositoryFactory extends RepositoryFactorySupport {
throw new IllegalArgumentException(String.format("Unsupported query lookup strategy %s!", key));
}
return Optional.of(new JdbcQueryLookupStrategy(evaluationContextProvider, context, accessStrategy));
return Optional.of(new JdbcQueryLookupStrategy(evaluationContextProvider, context, accessStrategy, rowMapperMap));
}
public void setRowMapperMap(RowMapperMap rowMapperMap) {
this.rowMapperMap = rowMapperMap;
}
}

View File

@@ -22,6 +22,7 @@ import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.data.jdbc.core.DataAccessStrategy;
import org.springframework.data.jdbc.mapping.model.JdbcMappingContext;
import org.springframework.data.jdbc.repository.RowMapperMap;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
import org.springframework.data.repository.core.support.TransactionalRepositoryFactoryBeanSupport;
@@ -41,6 +42,7 @@ public class JdbcRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extend
private ApplicationEventPublisher publisher;
private JdbcMappingContext mappingContext;
private DataAccessStrategy dataAccessStrategy;
private RowMapperMap rowMapperMap = RowMapperMap.EMPTY;
JdbcRepositoryFactoryBean(Class<? extends T> repositoryInterface) {
super(repositoryInterface);
@@ -60,7 +62,15 @@ public class JdbcRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extend
*/
@Override
protected RepositoryFactorySupport doCreateRepositoryFactory() {
return new JdbcRepositoryFactory(publisher, mappingContext, dataAccessStrategy);
JdbcRepositoryFactory jdbcRepositoryFactory = new JdbcRepositoryFactory(publisher, mappingContext,
dataAccessStrategy);
if (rowMapperMap != null) {
jdbcRepositoryFactory.setRowMapperMap(rowMapperMap);
}
return jdbcRepositoryFactory;
}
@Autowired
@@ -75,6 +85,11 @@ public class JdbcRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extend
this.dataAccessStrategy = dataAccessStrategy;
}
@Autowired(required = false)
public void setRowMapperMap(RowMapperMap rowMapperMap) {
this.rowMapperMap = rowMapperMap;
}
@Override
public void afterPropertiesSet() {