我在Single / Optional Result for Query @ ScalikeJDBC Docs上输入了几行:
package org.mysample
import scalikejdbc._
// define a class to map the result
case class Emp(id: String, name: String)
// QueryDSL
object Emp extends SQLSyntaxSupport[Emp] {
def apply(e: ResultName[Emp])(rs: WrappedResultSet): Emp =
new Emp(id = rs.get(e.id), name = rs.get(e.name))
}
class TestClass {
val emp: Option[Emp] = DB readOnly { implicit session =>
sql"SELECT id, name FROM emp WHERE id = ${id}"
.map(rs => Emp(rs.string("id"), rs.string("name"))).single.apply()
}
val e = Emp.syntax("e")
val id = 123
val emp2: Option[Emp] = DB readOnly { implicit session =>
withSQL {
select.from(Emp as e).where.eq(e.id, id)
}.map(Emp(e)).single.apply()
}
}
但是这个片段有一个编译错误:
[error] /Users/pj/git/sirloin/src/main/scala/org/mysample/emp.scala:25: type mismatch;
[error] found : scalikejdbc.QuerySQLSyntaxProvider[scalikejdbc.SQLSyntaxSupport[org.mysample.Emp],org.mysample.Emp]
[error] required: scalikejdbc.ResultName[org.mysample.Emp]
[error] (which expands to) scalikejdbc.ResultNameSQLSyntaxProvider[scalikejdbc.SQLSyntaxSupport[org.mysample.Emp],org.mysample.Emp]
[error] }.map(Emp(e)).single.apply()
[error] ^
我在 sbt 中清理并重新编译,但仍然无法编译。这段代码有什么问题?
感谢您的帮助。