Skip to content

Provide a concrete implementation of MappingSqlQuery (say RowMappingSqlQuery) which can be injected a RowMapper, so that we can put it in a config file [SPR-3986] #8666

Closed
@spring-projects-issues

Description

@spring-projects-issues

Celal Ziftci opened SPR-3986 and commented

MappingSqlQuery is an abstract class and expects concrete subclasses to implement mapRow(ResultSet, int). If we had a concrete implementation (say RowMappingSqlQuery), we could potentially put this RowMappingSqlQuery into a config file entirely.
Below is an example of how it would look:

<bean id="query_withParams" class="org.springframework.jdbc.object.RowMappingSqlQuery">
<property name="dataSource"><ref bean="someDataSource"/></property>
<property name="sql" value="select * from table where varchardata = ?"/>
<property name="parameters">
<list>
<bean class="org.springframework.jdbc.core.SqlParameter">
<constructor-arg index="0" value="varcharData"/>
<constructor-arg index="1"><util:constant static-field="java.sql.Types.VARCHAR"/></constructor-arg>
</bean>
</list>
</property>
<property name="rowMapper">
<bean class="some.package.IntegerRowMapper" />
</property>
</bean>

Note that we also provide a RowMapper to this RowMappingSqlQuery (so it will need to have a setter/getter for RowMapper).
Below is the sample implementation for this RowMappingSqlQuery class:

public class RowMappingSqlQuery extends MappingSqlQuery
{
RowMapper _rowMapper;

public RowMappingSqlQuery()
{
super();
}

public RowMappingSqlQuery( DataSource ds, String sql )
{
super( ds, sql );
}

/* This is the abstract method in MappingSqlQuery */
protected Object mapRow( ResultSet rs_, int rowNum_ ) throws SQLException
{
if( _rowMapper != null )
{
return rowMapper.mapRow( rs, rowNum_ );
}
else
{
return null;
}
}

public void setRowMapper( RowMapper rowMapper )
{
_rowMapper = rowMapper;
}

public RowMapper getRowMapper()
{
return _rowMapper;
}
}


Affects: 2.0.6, 2.1 M4, 2.5 RC1

Referenced from: commits 7ccb0b6, a3942c5

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: coreIssues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions