@@ -5,6 +5,7 @@ import org.springframework.boot.CommandLineRunner
55import org.springframework.boot.autoconfigure.SpringBootApplication
66import org.springframework.boot.runApplication
77import org.springframework.jdbc.core.JdbcTemplate
8+ import org.springframework.jdbc.core.query
89
910@SpringBootApplication
1011class RelationalDataAccessApplication (
@@ -17,13 +18,15 @@ class RelationalDataAccessApplication(
1718 log.info(" Creating tables" )
1819
1920 jdbcTemplate.execute(" DROP TABLE IF EXISTS customers" )
20- jdbcTemplate.execute("""
21+ jdbcTemplate.execute(
22+ """
2123 CREATE TABLE customers(
2224 id SERIAL,
2325 first_name VARCHAR(255),
2426 last_name VARCHAR(255)
2527 )
26- """ .trimIndent())
28+ """ .trimIndent()
29+ )
2730
2831 // Split up the array of whole names into an array of first/last names
2932 val splitUpNames = listOf (" John Woo" , " Jeff Dean" , " Josh Bloch" , " Josh Long" )
@@ -38,19 +41,18 @@ class RelationalDataAccessApplication(
3841 jdbcTemplate.batchUpdate(" INSERT INTO customers(first_name, last_name) VALUES (?,?)" , splitUpNames)
3942
4043 log.info(" Querying for customer records where first_name = 'Josh':" )
41- jdbcTemplate.query(
42- " SELECT id, first_name, last_name FROM customers WHERE first_name = ?" ,
43- { rs, _ ->
44- Customer (
45- rs.getLong(" id" ),
46- rs.getString(" first_name" ),
47- rs.getString(" last_name" )
48- )
49- },
50- " Josh"
51- ).forEach { customer ->
52- log.info(customer.toString())
44+ // Import .query() Kotlin extension that allows varargs before lambda to enable trailing lambda syntax
45+ jdbcTemplate.query(" SELECT id, first_name, last_name FROM customers WHERE first_name = ?" , " Josh" )
46+ { rs, _ ->
47+ Customer (
48+ rs.getLong(" id" ),
49+ rs.getString(" first_name" ),
50+ rs.getString(" last_name" )
51+ )
5352 }
53+ .forEach { customer ->
54+ log.info(customer.toString())
55+ }
5456 }
5557}
5658
0 commit comments