![](/img/trans.png)
[英]How to get rid of ScalalikeJDBC implicit DBSession argument in my DAO trait methods?
[英]Scalalikejdbc Implicit Parameter
試圖理解 Scala 中的以下語法(隱式會話):
def getDates(date: String): Option[String] = DB.readOnly { implicit session =>
val date = "20201020"
}
使用 scalalikejdbc 中的 readOnly 方法。 方法的定義是:
def readOnly[A](execution: DBSession => A)(implicit context: CPContext = NoCPContext, settings: SettingsProvider = SettingsProvider.default): A = {
val cp = connectionPool(context)
using(cp.borrow()) { conn =>
DB(conn, cp.connectionAttributes, settings).autoClose(false).readOnly(execution)
}
}
這意味着session
在整個function 主體中隱含 scope ,例如
trait Foo
val foo = new Foo {}
def g(implicit foo: Foo) = ???
val f: Foo => String = implicit foo => {
// foo is in implicit scope in the method body
g // foo argument passed in to g implicitly
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.