197

Spring 5 JDBC support for Kotlin – Mario Arias – Medium

 6 years ago
source link: https://medium.com/@mario.arias.c/spring-5-jdbc-support-for-kotlin-7cc31f4db4a5
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Spring 5 JDBC support for Kotlin

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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK