[英]What is 'override def *' in Scala Slick schema trait
case class Fruit(
apple: String,
banana: String
)
trait AppleComponent { self: HasDatabaseConfig[JdbcProfile] =>
import profile.api._
class Fruits(tag: Tag) extends Table[Fruit](tag, "fruits") {
def apple = column[String]("apple")
def banana = column[String]("banana")
// what is this line of code used for?
override def * = (apple,banana) <> (Fruit.tupled, Fruit.unapply)
def applePK = primaryKey("apple", apple)
}
protected lazy val Apples = TableQuery[Fruits]
}
I am new to Scala Slick ,so I want to know what does override def * = (apple,banana) <> (Fruit.tupled, Fruit.unapply)
mean?我是Scala Slick 的新手,所以我想知道
override def * = (apple,banana) <> (Fruit.tupled, Fruit.unapply)
是什么意思? I really can't find any document about it.我真的找不到任何关于它的文件。 Also, why we need a trait here?
另外,为什么我们需要一个特质?
Table
class has *
method which should implements SELECT * FROM ...
semantics. Table
类有*
方法,该方法应该实现SELECT * FROM ...
语义。 Since Slick cannot guess how you want to extract columns, you have to write this manually (using all columns in the order you want).由于 Slick 无法猜测您想如何提取列,因此您必须手动编写(按您想要的顺序使用所有列)。
<>
is just so that you will have a case class returned by *
rather than a tuple. <>
只是为了让你有一个由*
而不是元组返回的案例类。
You don't need trait
here.你在这里不需要
trait
。 This:这个:
// module name
trait AppleComponent {
// self-type for dependency injection
self: HasDatabaseConfig[JdbcProfile] =>
// dependencies injected by mixing-in this trait:
protected lazy val Apples = TableQuery[Fruits]
}
is called a cake pattern.被称为蛋糕图案。 In general it is an anti-pattern (but ZIO promotes recently a specific way of using it).
一般来说,它是一种反模式(但 ZIO 最近推广了一种使用它的特定方式)。
I'd say whatever documentation or tutorial you are using is outdated by a few years.我想说您使用的任何文档或教程都已经过时了几年。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.