Skip to content

Instantly share code, notes, and snippets.

@soudmaijer
Last active May 18, 2018 12:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soudmaijer/b58ebac8177f0d1b8d3557143e657fd8 to your computer and use it in GitHub Desktop.
Save soudmaijer/b58ebac8177f0d1b8d3557143e657fd8 to your computer and use it in GitHub Desktop.
import org.springframework.jdbc.core.JdbcTemplate
import org.springframework.jdbc.core.RowMapper
import org.springframework.stereotype.Repository
import java.util.*
@Repository
class OrderRepository(private val jdbcTemplate: JdbcTemplate) {
private val rowMapper: RowMapper<Order> = RowMapper { rs, i -> Order(rs.getLong("id")) }
fun findOrderById(id: Long): Optional<Order> {
val result = jdbcTemplate.query<Order>("select * from orders where id=:id", rowMapper, id)
if (result.size > 1) throw RuntimeException("Too many results from findOrderById.")
return if (result.size == 1) Optional.of(result.get(0)) else Optional.empty()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment