Skip to content

Commit d3e1cfb

Browse files
committed
DATAJDBC-165 - Allows configuration of custom RowMapper on @query annotation.
1 parent 8af7553 commit d3e1cfb

File tree

5 files changed

+228
-5
lines changed

5 files changed

+228
-5
lines changed

src/main/java/org/springframework/data/jdbc/repository/query/Query.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.lang.annotation.Target;
2323

2424
import org.springframework.data.annotation.QueryAnnotation;
25+
import org.springframework.jdbc.core.RowMapper;
2526

2627
/**
2728
* Annotation to provide SQL statements that will get used for executing the method.
@@ -36,5 +37,14 @@
3637
@QueryAnnotation
3738
@Documented
3839
public @interface Query {
40+
41+
/**
42+
* The SQL statement to execute when the annotated method gets invoked.
43+
*/
3944
String value();
45+
46+
/**
47+
* Optional {@link RowMapper} to use to convert the result of the query to domain class instances.
48+
*/
49+
Class<? extends RowMapper> rowMapperClass() default RowMapper.class;
4050
}

src/main/java/org/springframework/data/jdbc/repository/support/JdbcQueryMethod.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.lang.reflect.Method;
1919

2020
import org.springframework.core.annotation.AnnotatedElementUtils;
21+
import org.springframework.core.annotation.AnnotationUtils;
2122
import org.springframework.data.jdbc.repository.query.Query;
2223
import org.springframework.data.projection.ProjectionFactory;
2324
import org.springframework.data.repository.core.RepositoryMetadata;
@@ -36,15 +37,23 @@ public class JdbcQueryMethod extends QueryMethod {
3637
private final Method method;
3738

3839
public JdbcQueryMethod(Method method, RepositoryMetadata metadata, ProjectionFactory factory) {
40+
3941
super(method, metadata, factory);
4042

4143
this.method = method;
4244
}
4345

4446
public String getAnnotatedQuery() {
47+
return getMergedAnnotationAttribute("value");
48+
}
49+
50+
private <T> T getMergedAnnotationAttribute(String attribute) {
4551

4652
Query queryAnnotation = AnnotatedElementUtils.findMergedAnnotation(method, Query.class);
53+
return (T) AnnotationUtils.getValue(queryAnnotation, attribute);
54+
}
4755

48-
return queryAnnotation == null ? null : queryAnnotation.value();
56+
public Class<?> getRowMapperClass() {
57+
return getMergedAnnotationAttribute("rowMapperClass");
4958
}
5059
}

src/main/java/org/springframework/data/jdbc/repository/support/JdbcRepositoryQuery.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
*/
1616
package org.springframework.data.jdbc.repository.support;
1717

18+
import org.springframework.beans.BeanUtils;
1819
import org.springframework.data.jdbc.mapping.model.JdbcMappingContext;
1920
import org.springframework.data.repository.query.RepositoryQuery;
2021
import org.springframework.jdbc.core.RowMapper;
2122
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
23+
import org.springframework.util.StringUtils;
2224

2325
/**
2426
* A query to be executed based on a repository method, it's annotated SQL query and the arguments provided to the
@@ -34,26 +36,45 @@ class JdbcRepositoryQuery implements RepositoryQuery {
3436
private final JdbcMappingContext context;
3537
private final RowMapper<?> rowMapper;
3638

37-
JdbcRepositoryQuery(JdbcQueryMethod queryMethod, JdbcMappingContext context, RowMapper rowMapper) {
39+
JdbcRepositoryQuery(JdbcQueryMethod queryMethod, JdbcMappingContext context, RowMapper defaultRowMapper) {
3840

3941
this.queryMethod = queryMethod;
4042
this.context = context;
41-
this.rowMapper = rowMapper;
43+
this.rowMapper = createRowMapper(queryMethod, defaultRowMapper);
44+
}
45+
46+
private static RowMapper<?> createRowMapper(JdbcQueryMethod queryMethod, RowMapper defaultRowMapper) {
47+
48+
Class<?> rowMapperClass = queryMethod.getRowMapperClass();
49+
50+
return rowMapperClass == null || rowMapperClass == RowMapper.class ? defaultRowMapper : (RowMapper<?>) BeanUtils.instantiateClass(rowMapperClass);
4251
}
4352

4453
@Override
4554
public Object execute(Object[] objects) {
4655

56+
return context.getTemplate().query(determineQuery(), bindParameters(objects), rowMapper);
57+
}
58+
59+
private String determineQuery() {
60+
4761
String query = queryMethod.getAnnotatedQuery();
4862

63+
if (StringUtils.isEmpty(query)) {
64+
throw new IllegalStateException(String.format("No query specified on %s", queryMethod.getName()));
65+
}
66+
return query;
67+
}
68+
69+
private MapSqlParameterSource bindParameters(Object[] objects) {
70+
4971
MapSqlParameterSource parameters = new MapSqlParameterSource();
5072
queryMethod.getParameters().getBindableParameters().forEach(p -> {
5173

5274
String parameterName = p.getName().orElseThrow(() -> new IllegalStateException(PARAMETER_NEEDS_TO_BE_NAMED));
5375
parameters.addValue(parameterName, objects[p.getIndex()]);
5476
});
55-
56-
return context.getTemplate().query(query, parameters, rowMapper);
77+
return parameters;
5778
}
5879

5980
@Override
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.jdbc.repository.support;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
import static org.mockito.Mockito.*;
20+
21+
import java.lang.reflect.Method;
22+
import java.sql.ResultSet;
23+
24+
import org.junit.Test;
25+
import org.springframework.data.jdbc.repository.query.Query;
26+
import org.springframework.data.projection.ProjectionFactory;
27+
import org.springframework.data.repository.core.RepositoryMetadata;
28+
import org.springframework.jdbc.core.RowMapper;
29+
30+
/**
31+
* Unit tests for {@link JdbcQueryMethod}.
32+
*
33+
* @author Jens Schauder
34+
*/
35+
public class JdbcQueryMethodUnitTests {
36+
37+
public static final String DUMMY_SELECT = "SELECT something";
38+
39+
@Test // DATAJDBC-165
40+
public void returnsSqlStatement() throws NoSuchMethodException {
41+
42+
RepositoryMetadata metadata = mock(RepositoryMetadata.class);
43+
when(metadata.getReturnedDomainClass(any(Method.class))).thenReturn((Class) String.class);
44+
45+
JdbcQueryMethod queryMethod = new JdbcQueryMethod(JdbcQueryMethodUnitTests.class.getDeclaredMethod("queryMethod"),
46+
metadata, mock(ProjectionFactory.class));
47+
48+
assertThat(queryMethod.getAnnotatedQuery()).isEqualTo(DUMMY_SELECT);
49+
}
50+
51+
@Test // DATAJDBC-165
52+
public void returnsSpecifiedRowMapperClass() throws NoSuchMethodException {
53+
54+
RepositoryMetadata metadata = mock(RepositoryMetadata.class);
55+
when(metadata.getReturnedDomainClass(any(Method.class))).thenReturn((Class) String.class);
56+
57+
JdbcQueryMethod queryMethod = new JdbcQueryMethod(JdbcQueryMethodUnitTests.class.getDeclaredMethod("queryMethod"),
58+
metadata, mock(ProjectionFactory.class));
59+
60+
assertThat(queryMethod.getRowMapperClass()).isEqualTo(CustomRowMapper.class);
61+
}
62+
63+
@Query(value = DUMMY_SELECT, rowMapperClass = CustomRowMapper.class)
64+
private void queryMethod() {}
65+
66+
private class CustomRowMapper implements RowMapper {
67+
68+
@Override
69+
public Object mapRow(ResultSet rs, int rowNum) {
70+
return null;
71+
}
72+
}
73+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* Copyright 2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.jdbc.repository.support;
17+
18+
import static org.mockito.Mockito.*;
19+
20+
import java.sql.ResultSet;
21+
22+
import org.assertj.core.api.Assertions;
23+
import org.junit.Before;
24+
import org.junit.Test;
25+
import org.springframework.data.jdbc.mapping.model.JdbcMappingContext;
26+
import org.springframework.data.repository.query.DefaultParameters;
27+
import org.springframework.data.repository.query.Parameters;
28+
import org.springframework.jdbc.core.RowMapper;
29+
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
30+
31+
/**
32+
* Unit tests for {@link JdbcRepositoryQuery}.
33+
*
34+
* @author Jens Schauder
35+
*/
36+
public class JdbcRepositoryQueryUnitTests {
37+
38+
JdbcQueryMethod queryMethod;
39+
JdbcMappingContext context;
40+
RowMapper defaultRowMapper;
41+
JdbcRepositoryQuery query;
42+
43+
@Before
44+
public void setup() throws NoSuchMethodException {
45+
46+
Parameters parameters = new DefaultParameters(JdbcRepositoryQueryUnitTests.class.getDeclaredMethod("dummyMethod"));
47+
queryMethod = mock(JdbcQueryMethod.class);
48+
when(queryMethod.getParameters()).thenReturn(parameters);
49+
50+
context = mock(JdbcMappingContext.class, RETURNS_DEEP_STUBS);
51+
defaultRowMapper = mock(RowMapper.class);
52+
}
53+
54+
@Test // DATAJDBC-165
55+
public void emptyQueryThrowsException() {
56+
57+
when(queryMethod.getAnnotatedQuery()).thenReturn(null);
58+
query = new JdbcRepositoryQuery(queryMethod, context, defaultRowMapper);
59+
60+
Assertions.assertThatExceptionOfType(IllegalStateException.class) //
61+
.isThrownBy(() -> query.execute(new Object[] {}));
62+
}
63+
64+
@Test // DATAJDBC-165
65+
public void defaultRowMapperIsUsedByDefault() {
66+
67+
when(queryMethod.getAnnotatedQuery()).thenReturn("some sql statement");
68+
when(queryMethod.getRowMapperClass()).thenReturn((Class) RowMapper.class);
69+
query = new JdbcRepositoryQuery(queryMethod, context, defaultRowMapper);
70+
71+
query.execute(new Object[] {});
72+
73+
verify(context.getTemplate()).query(anyString(), any(SqlParameterSource.class), eq(defaultRowMapper));
74+
}
75+
76+
@Test // DATAJDBC-165
77+
public void defaultRowMapperIsUsedForNull() {
78+
79+
when(queryMethod.getAnnotatedQuery()).thenReturn("some sql statement");
80+
query = new JdbcRepositoryQuery(queryMethod, context, defaultRowMapper);
81+
82+
query.execute(new Object[] {});
83+
84+
verify(context.getTemplate()).query(anyString(), any(SqlParameterSource.class), eq(defaultRowMapper));
85+
}
86+
87+
@Test // DATAJDBC-165
88+
public void customRowMapperIsUsedWhenSpecified() {
89+
90+
when(queryMethod.getAnnotatedQuery()).thenReturn("some sql statement");
91+
when(queryMethod.getRowMapperClass()).thenReturn((Class) CustomRowMapper.class);
92+
query = new JdbcRepositoryQuery(queryMethod, context, defaultRowMapper);
93+
94+
query.execute(new Object[] {});
95+
96+
verify(context.getTemplate()).query(anyString(), any(SqlParameterSource.class), isA(CustomRowMapper.class));
97+
}
98+
99+
/**
100+
* The whole purpose of this method is to easily generate a {@link DefaultParameters} instance during test setup.
101+
*/
102+
private void dummyMethod() {}
103+
104+
private static class CustomRowMapper implements RowMapper {
105+
@Override
106+
public Object mapRow(ResultSet rs, int rowNum) {
107+
return null;
108+
}
109+
}
110+
}

0 commit comments

Comments
 (0)