22程序的列限制

use*_*845 6 scala playframework slick

在使用Slick调用程序时,我们如何克服22限制?

我们目前有:

val q3 = sql"""call getStatements(${accountNumber})""".as[Transaction]
Run Code Online (Sandbox Code Playgroud)

问题是我们必须返回超过22列,而Transaction case类不能超过22列,因为当我们执行JSONFormat时,我们得到一个错误:

[error] E:\IdeaProjects\admin\Transaction.scala:59: No unapply or unapplySeq function found
[error]   implicit val jsonFormat = Json.format[Transaction]
Run Code Online (Sandbox Code Playgroud)

有什么建议?

Pau*_*ega 11

好吧 - 所以,如果你真的可以修改你的Transaction案例类而不是一个更好的解决方案HList(说实话,以后操作可能有点麻烦).

所以这就是事情:让我们假设你有一个User具有以下属性的表:

  • ID
  • 名称
  • 学院
  • finalGrade
  • 邮编

上面的列可能没有意义,但让我们使用它们作为示例.上面最直接的处理方法是创建一个case类:

case class User(
   id: Long,
   name: String,
   ...  // rest of the attributes here
   postCode: String)
Run Code Online (Sandbox Code Playgroud)

这将从应用程序端的表映射.

现在您还可以做的是:

case class Address(street: String, number: String, city: String, postCode: String)

case class UniversityInfo(faculty: String, finalGrade: Double)

case class User(id: Long, name: String, surname: String, uniInfo: UniversityInfo, address: Address)
Run Code Online (Sandbox Code Playgroud)

这个组合将帮助您避免太多列的问题(这基本上是您的案例类/元组中的太多属性的问题).除此之外 - 我认为,如果你有很多专栏,那么总是(通常是)有益于这样做 - 如果只是为了可读性目的.

如何进行映射

class User(tag: Tag) extends Table(tag, "User") {

  // cricoss info
  def id = column[Long]("id")
  def name = column[String]("name")

  // ... all the other fields
  def postCode = column[String]("postCode")

  def * = (id, name, surname, uniInfoProjection, addressProjection) <>((User.apply _).tupled, User.unapply)

  def uniInfoProjection = (faculty, finalGrade) <>((UniversityInfo.apply _).tupled, UniversityInfo.unapply)

  def addressProjection = (street, number, city, city) <>((Address.apply _).tupled, Address.unapply)
}
Run Code Online (Sandbox Code Playgroud)

使用自定义SQL映射也可以完成相同的操作.

implicit val getUserResult = GetResult(r => 
    User(r.nextLong, r.nextString, r.nextString, 
         UniversityInfo(r.nextString, r.nextDouble),
         Adress(r.nextString, r.nextString, r.nextString, r.nextString))
)         
Run Code Online (Sandbox Code Playgroud)

所以简单地说 - 尝试将你的字段分成多个嵌套的case类,你的问题应该消失(增加可读性的附加好处).如果你这样做,接近元组/案例类限制几乎不应该是一个问题(你甚至不需要使用HList).