The problem is that the compiler doesn't need to do an implicit conversion because jdbcTemplate.update
has an overloaded type signature which only requires String
and Object...
which will match with String
and Student
without needing an implicit conversion. You can do this explicitly (which I think looks better for readability in code) like this:
implicit class preparedStatementSetterImplicit(student: Student) {
def toPreparedStatement() = {
new PreparedStatementSetter() {
def setValues(ps: PreparedStatement) {
ps.setString(1, student.id)
ps.setString(2, student.firstName)
ps.setString(3, student.lastName)
}
}
}
}
def insert(student: Student) {
jdbcTemplate.update("INSERT INTO STUDENT (ID, FIRSTNAME, LASTNAME) VALUES(?,?,?)",
student.toPreparedStatement())
}