user1521881
user1521881

Reputation: 67

scala jdbc template and rowmapper

I am new to Scala and I am looking at a bit of Scala code which involves a Spring JDBC template and a RowMapper:

It is something like this:

val db = jdbcTemplate.queryForObject(QUERY, new RowMapper[SomeObject]() {
  def mapRow(ResultSet rs, int rowNum) {
     var s = new SomeObject()
     s.setParam1 = rs.getDouble("columnName")
     return s
  }
  })
   db

I am writing this from memory so I have just used generic names.

I was wondering why db is written at the end. I can't think of what purpose it serves.

Also, If I had several JDBC templates and an object like s in the example where I wanted to populate it's data with output from the several JDBC templates. Is it possible to do this in one function? Is it possible to have a mapRow function which doesn't return anything so that I could maybe have an array of templates and loop through them?

Thanks

Upvotes: 0

Views: 1927

Answers (1)

Eugene Ryzhikov
Eugene Ryzhikov

Reputation: 17359

The db at the end means return db where return statement is skipped. This is a standard convention in Scala. Seems like your code is a body of the function which suppose to return db. The first statement simply assigns the result of the query to a db

RowMapper interface can be replaced with implicit conversion to a function of the following type (ResultSet, Int) => SomeObject, which means that it takes two parameters (ResultSet and Int) and returns the result of type SomeObject

Upvotes: 1

Related Questions