使用scalikeJDBC映射结果时如何解决类型不匹配?
How to resolve type mismatch when mapping results using scalikeJDBC?
我在 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中清理并重新编译,但仍然编译失败。这段代码有什么问题?
感谢您的帮助。
抱歉打扰了。只需指定 map(Emp(e.resultName))
即可。或者像这样更新 Emp 伴随对象:
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))
def apply(e: SyntaxProvider[Emp])(rs: WrappedResultSet): Emp = apply(e.resultName)(rs)
}
我在 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中清理并重新编译,但仍然编译失败。这段代码有什么问题?
感谢您的帮助。
抱歉打扰了。只需指定 map(Emp(e.resultName))
即可。或者像这样更新 Emp 伴随对象:
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))
def apply(e: SyntaxProvider[Emp])(rs: WrappedResultSet): Emp = apply(e.resultName)(rs)
}