我只是在学习 Scala 并尝试使用 Spring 创建不同的场景。
以下代码完美执行:
def insert(student: Student): Unit = {
jdbcTemplate.update("INSERT INTO STUDENT (ID, FIRSTNAME, LASTNAME) VALUES(?,?,?)",
new PreparedStatementSetter() {
def setValues(ps: PreparedStatement) {
ps.setString(1, student.id)
ps.setString(2, student.firstName)
ps.setString(3, student.lastName)
}
})
}
}
但是,当我尝试隐式创建 PreparedStatementSetter 时,我的 SQL 代码会失败。
我的隐含功能是:
implicit def preparedStatementSetterImplicit(student: Student) = {
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): Unit = {
jdbcTemplate.update("INSERT INTO STUDENT (ID, FIRSTNAME, LASTNAME) VALUES(?,?,?)",
student)
}
我的 SQL 插入失败,但出现以下异常:
Exception in thread "main" org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [INSERT INTO STUDENT (ID, FIRSTNAME, LASTNAME) VALUES(?,?,?)]; nested exception is java.sql.SQLSyntaxErrorException: incompatible data type in conversion
我的隐式转换有什么问题?