paint-brush
Spring 5 JDBC support for Kotlinby@mario.arias.c
6,756 reads
6,756 reads

Spring 5 JDBC support for Kotlin

by Mario AriasSeptember 30th, 2017
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

<a href="https://spring.io/blog/2017/09/28/spring-framework-5-0-goes-ga" target="_blank">Spring 5 was released last week</a> with many exciting features, but for me, the most important one is the <a href="https://hackernoon.com/tagged/kotlin" target="_blank">Kotlin</a> support. Not only because I love the language but because I contributed to it. (I know, shameless plug)
featured image - Spring 5 JDBC support for Kotlin
Mario Arias HackerNoon profile picture

Spring 5 was released last week with many exciting features, but for me, the most important one is the Kotlin support. Not only because I love the language but because I contributed to it. (I know, shameless plug)

One of the features included in this support is a set of extension functions for JdbcTemplate and MapSqlParamaterSource that gives you a more idiomatic API.

Let’s translate the Accessing Relational Data using JDBC with Spring official guide to Kotlin

Using Spring 5.0 JDBC

For this example, we’ll use Spring Boot 2.0 SNAPSHOT

Customer data class

Using JdbcTemplate

Let’s comment the most interesting lines.

  1. log is declared inside the companion object on line 46
  2. Function with will help us to save some typing. Inside the with block, we can access all members of jdbcTemplate.
  3. splitUpNames is a List<Pair<String, String>>. Later we’ll use Pair’s destructuring declarations.
  4. Inside any function that has a parameter type with destructuring declarations (component1(), component2() and so on), you can replace it with them. In this case, we replace Pair<String, String> with (String, String).
  5. Last parameter of batchUpdate is a ParameterizedPreparedStatementSetter<T> which is a Java 8 functional interface. We can use functional interfaces in Kotlin as functions, including destructuring declarations.
  6. This query is an extension function. It changes the order of parameters and switches RowMapper<T> into a (ResultSet, Int) ->T. Because we aren’t using the last parameter (the row number), we can replace it with _

Speaking about extensions functions, you can check the kdoc here.

Using NamedParameterJdbcTemplate

NamedParameterJdbcTemplate is a version of JdbcTemplate that use named parameters (‘:parameter1’) instead of placeholders (‘?’). Spring 5 doesn’t provides any extension functions for NamedParameterJdbcTemplate (the API is good enough to use it from Kotlin) but it provides extension functions for a related class, MapSqlParameterSource.

  1. jdbcOperations returns a plain JdbcTemplate if you still need to use it
  2. sources is a List<MapSqlParameterSource>
  3. Extension function to set parameters in an array-like setter.
  4. There isn’t extension function to read with an array-like getter but we can add one easily.
  5. This insert query use named parameters.
  6. query receives a Map<String, *> as a second parameters, very easy to use in Kotlin.

Conclusion

Spring JDBC with Kotlin provides a solid API for your relational database application. Enjoy.