开发者

Construct an object from a database using spring specifying the row ID to construct object from

To Simplify this question, is it possible to make spring behave in a very basic active record pattern

Background:

I have a system with multiple types of existing data models all with UIDs stored in read only tables. As a user inputs data into the system the input is compared against the existing row using the UID to match th开发者_高级运维e input data with the existing data.

Problem:

I want to use spring to construct objects from the existing data for comparison, this will allow me to add and configure data models with ease. I don't want to use hibernate or any other complex ORM becuase I only need read access and want to keep the system as simple and light weight as possible so I am considering a simple spring jdbc template.

How should I pass the importId to construct the object? I could make a setter method but then I will have to call the constructor and have a empty object lying round till I set the id at which point I begin initialising other variables, this seems awkward. Is there a standard way to do this? I suspect I might be missing something obvious. Are there any other suggestions for doing this?

Without getting bogged down with the code an example of an object class constructed from the existing data might be:

public class Import extends Model {
    private JdbcTemplate mJdbcTemplate;
    private int mImportId;
    public Import(int importId, JdbcTemplate template) {
        this.mImportId = importId;
        this.mJdbcTemplate = jdbcTemplate;
        ...
        // construct object form fields
        ... 
    }
    ... 
    // Other stuff
    ... 
} 
<beans>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean> 
    <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    ...
    data source setup
    ...
    </bean>
    <bean id="import" class="package.models.Import">
        <constructor-arg ref="jdbcTemplate"/>
        ... what to do here...
    </bean>
</beans>


What you need ti implement is a RowMapper for you Object, then you can yuse the jdbcTemplate.queryForObject method.

public Import getImportById(long id) {
       return jdbcTemplate.queryForObject(
          "Select a, b FROM import WHERE id = ?",
          this.importRowMapper,
          id);
}


private RowMapper<Import> importRowMapper = new RowMapper<Import>() {    
    public Customer mapRow(ResultSet resultSet, int i) throws SQLException {
      long id = resultSet.getInt("id");
      String a = resultSet.getString("a");
      String b = resultSet.getString("b");
      return new Customer(id, a, b);
    }

You can have a look at this blog: Green Beans: Getting Started with Spring in your Service Tier, it describe this a little bit. (Only the last section in this blog is about JPA.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜