Skip to content

Commit e032930

Browse files
committed
JdbcTemplate's queryForObject allows for specifying primitive types as well
Issue: SPR-13220
1 parent 8fdbf42 commit e032930

File tree

3 files changed

+51
-20
lines changed

3 files changed

+51
-20
lines changed

spring-jdbc/src/main/java/org/springframework/jdbc/core/BeanPropertyRowMapper.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,16 @@ public class BeanPropertyRowMapper<T> implements RowMapper<T> {
9292

9393

9494
/**
95-
* Create a new BeanPropertyRowMapper for bean-style configuration.
95+
* Create a new {@code BeanPropertyRowMapper} for bean-style configuration.
9696
* @see #setMappedClass
9797
* @see #setCheckFullyPopulated
9898
*/
9999
public BeanPropertyRowMapper() {
100100
}
101101

102102
/**
103-
* Create a new BeanPropertyRowMapper, accepting unpopulated properties
104-
* in the target bean.
103+
* Create a new {@code BeanPropertyRowMapper}, accepting unpopulated
104+
* properties in the target bean.
105105
* <p>Consider using the {@link #newInstance} factory method instead,
106106
* which allows for specifying the mapped type once only.
107107
* @param mappedClass the class that each row should be mapped to
@@ -111,7 +111,7 @@ public BeanPropertyRowMapper(Class<T> mappedClass) {
111111
}
112112

113113
/**
114-
* Create a new BeanPropertyRowMapper.
114+
* Create a new {@code BeanPropertyRowMapper}.
115115
* @param mappedClass the class that each row should be mapped to
116116
* @param checkFullyPopulated whether we're strictly validating that
117117
* all bean properties have been mapped from corresponding database fields
@@ -326,14 +326,12 @@ protected Object getColumnValue(ResultSet rs, int index, PropertyDescriptor pd)
326326

327327

328328
/**
329-
* Static factory method to create a new BeanPropertyRowMapper
329+
* Static factory method to create a new {@code BeanPropertyRowMapper}
330330
* (with the mapped class specified only once).
331331
* @param mappedClass the class that each row should be mapped to
332332
*/
333333
public static <T> BeanPropertyRowMapper<T> newInstance(Class<T> mappedClass) {
334-
BeanPropertyRowMapper<T> newInstance = new BeanPropertyRowMapper<T>();
335-
newInstance.setMappedClass(mappedClass);
336-
return newInstance;
334+
return new BeanPropertyRowMapper<T>(mappedClass);
337335
}
338336

339337
}

spring-jdbc/src/main/java/org/springframework/jdbc/core/SingleColumnRowMapper.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
2323
import org.springframework.dao.TypeMismatchDataAccessException;
2424
import org.springframework.jdbc.IncorrectResultSetColumnCountException;
2525
import org.springframework.jdbc.support.JdbcUtils;
26+
import org.springframework.util.ClassUtils;
2627
import org.springframework.util.NumberUtils;
2728

2829
/**
@@ -41,31 +42,34 @@
4142
*/
4243
public class SingleColumnRowMapper<T> implements RowMapper<T> {
4344

44-
private Class<T> requiredType;
45+
private Class<?> requiredType;
4546

4647

4748
/**
48-
* Create a new SingleColumnRowMapper.
49+
* Create a new {@code SingleColumnRowMapper} for bean-style configuration.
4950
* @see #setRequiredType
5051
*/
5152
public SingleColumnRowMapper() {
5253
}
5354

5455
/**
55-
* Create a new SingleColumnRowMapper.
56+
* Create a new {@code SingleColumnRowMapper}.
57+
* <p>Consider using the {@link #newInstance} factory method instead,
58+
* which allows for specifying the required type once only.
5659
* @param requiredType the type that each result object is expected to match
5760
*/
5861
public SingleColumnRowMapper(Class<T> requiredType) {
59-
this.requiredType = requiredType;
62+
setRequiredType(requiredType);
6063
}
6164

65+
6266
/**
6367
* Set the type that each result object is expected to match.
6468
* <p>If not specified, the column value will be exposed as
6569
* returned by the JDBC driver.
6670
*/
6771
public void setRequiredType(Class<T> requiredType) {
68-
this.requiredType = requiredType;
72+
this.requiredType = ClassUtils.resolvePrimitiveIfNecessary(requiredType);
6973
}
7074

7175

@@ -187,15 +191,13 @@ else if (Number.class.isAssignableFrom(requiredType)) {
187191

188192

189193
/**
190-
* Static factory method to create a new ParameterizedSingleColumnRowMapper
194+
* Static factory method to create a new {@code SingleColumnRowMapper}
191195
* (with the required type specified only once).
192196
* @param requiredType the type that each result object is expected to match
193197
* @since 4.1
194198
*/
195199
public static <T> SingleColumnRowMapper<T> newInstance(Class<T> requiredType) {
196-
SingleColumnRowMapper<T> newInstance = new SingleColumnRowMapper<T>();
197-
newInstance.setRequiredType(requiredType);
198-
return newInstance;
200+
return new SingleColumnRowMapper<T>(requiredType);
199201
}
200202

201203
}

spring-jdbc/src/test/java/org/springframework/jdbc/core/JdbcTemplateQueryTests.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -41,6 +41,7 @@
4141
/**
4242
* @author Juergen Hoeller
4343
* @author Phillip Webb
44+
* @author Rob Winch
4445
* @since 19.12.2004
4546
*/
4647
public class JdbcTemplateQueryTests {
@@ -49,13 +50,20 @@ public class JdbcTemplateQueryTests {
4950
public ExpectedException thrown = ExpectedException.none();
5051

5152
private Connection connection;
53+
5254
private DataSource dataSource;
55+
5356
private Statement statement;
57+
5458
private PreparedStatement preparedStatement;
59+
5560
private ResultSet resultSet;
61+
5662
private ResultSetMetaData resultSetMetaData;
63+
5764
private JdbcTemplate template;
5865

66+
5967
@Before
6068
public void setUp() throws Exception {
6169
this.connection = mock(Connection.class);
@@ -75,6 +83,7 @@ public void setUp() throws Exception {
7583
given(this.statement.executeQuery(anyString())).willReturn(this.resultSet);
7684
}
7785

86+
7887
@Test
7988
public void testQueryForList() throws Exception {
8089
String sql = "SELECT AGE FROM CUSTMR WHERE ID < 3";
@@ -219,7 +228,18 @@ public void testQueryForInt() throws Exception {
219228
String sql = "SELECT AGE FROM CUSTMR WHERE ID = 3";
220229
given(this.resultSet.next()).willReturn(true, false);
221230
given(this.resultSet.getInt(1)).willReturn(22);
222-
int i = this.template.queryForObject(sql,Integer.class).intValue();
231+
int i = this.template.queryForObject(sql, Integer.class).intValue();
232+
assertEquals("Return of an int", 22, i);
233+
verify(this.resultSet).close();
234+
verify(this.statement).close();
235+
}
236+
237+
@Test
238+
public void testQueryForIntPrimitive() throws Exception {
239+
String sql = "SELECT AGE FROM CUSTMR WHERE ID = 3";
240+
given(this.resultSet.next()).willReturn(true, false);
241+
given(this.resultSet.getInt(1)).willReturn(22);
242+
int i = this.template.queryForObject(sql, int.class);
223243
assertEquals("Return of an int", 22, i);
224244
verify(this.resultSet).close();
225245
verify(this.statement).close();
@@ -236,6 +256,17 @@ public void testQueryForLong() throws Exception {
236256
verify(this.statement).close();
237257
}
238258

259+
@Test
260+
public void testQueryForLongPrimitive() throws Exception {
261+
String sql = "SELECT AGE FROM CUSTMR WHERE ID = 3";
262+
given(this.resultSet.next()).willReturn(true, false);
263+
given(this.resultSet.getLong(1)).willReturn(87L);
264+
long l = this.template.queryForObject(sql, long.class);
265+
assertEquals("Return of a long", 87, l);
266+
verify(this.resultSet).close();
267+
verify(this.statement).close();
268+
}
269+
239270
@Test
240271
public void testQueryForListWithArgs() throws Exception {
241272
doTestQueryForListWithArgs("SELECT AGE FROM CUSTMR WHERE ID < ?");

0 commit comments

Comments
 (0)