While (still) looking around for a Scala SQL equivalent of iBatis, I found this post on stackoverflow. Daniel’s answer let me to Querulous. Several years ago I used the Spring Framework JdbcTemplate and it worked fine, but it was kind of clunky. That experience with JdbcTemplate led directly to using iBatis on that very same project. I have been mostly working with Hibernate/JPA since then, but for a certain class of problems, a SQL centric solution seems a better fit that a full blown ORM.
After looking at Querulous, and seeing how it manages to de-clutter the anonymous inner class clunkyness that is JdbcTemplate I’m left wondering if this isn’t the way I should go.
Here is a quick example from the JdbcTemplate documentation in Java:
Collection actors = this.jdbcTemplate.query(
"select first_name, surname from t_actor",
new RowMapper() {
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
Actor actor = new Actor();
actor.setFirstName(rs.getString("first_name"));
actor.setSurname(rs.getString("surname"));
return actor;
}
});
Here is the equivalent with Querulous in Scala:
val actors = queryEvaluator.select("select first_name, surname from t_actor") { row =>
new Actor(row.getString("first_name"), row.getString("surname"))
}
Simple and easy to understand. Can’t beat that.