本文介绍了迁移到 anorm2.4(使用 play 2.4):ToStatement[T] 和 ToStatement[Option[T]]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

直到 play 2.3 包含异常,我可以写以下内容:

Up until the anorm included with play 2.3, I could write the following:

implicit val arbitraryClassToStatement = new ToStatement[ArbitraryClass] {
  def set(
    s: java.sql.PreparedStatement,
    index: Int,
    aValue: ArbitraryClass
  )
  : Unit = {
    s.setString(
      index,
      ArbitraryClass.definingString
    )
  }
}

这将有助于插入

SQL("INSERT INTO SomeTable Values( {nonNullAc}, {possiblyNullAc} )" ).on(
 'nonNullAc -> ArbitraryClass( "abcd" ),
 'possiblyNullAc -> Option( ArbitraryClass( "abcd" ) )
)

意味着 ArbitraryClass 和 Option[ ArbitraryClass ] 都会被它满足.这似乎不再是这种情况,因为我收到以下错误:

meaning that both ArbitraryClass and Option[ ArbitraryClass ] would be satisfied by it. This seems to no longer be the case as I get the following error:

[error]  found   : (Symbol, Option[models.Misc.Url])
[error]     (which expands to)  (Symbol, Option[java.net.URL])
[error]  required: anorm.NamedParameter

有人能指出我处理这个问题的正确方法吗?我想要最少的代码重复..

Can someone please point me to what's the right way to handle this? I'd want minimal duplication of code..

推荐答案

您需要创建一个 ParameterMetaData[ArbitraryClass] 才能工作

You need to create a ParameterMetaData[ArbitraryClass] for this to work

implicit object ArbitraryClassMetaData extends ParameterMetaData[ArbitraryClass] {
  val sqlType = ParameterMetaData.StringParameterMetaData.sqlType
  val jdbcType = ParameterMetaData.StringParameterMetaData.jdbcType
}

这里我只是重新取了ParameterMetaData[String]

这篇关于迁移到 anorm2.4(使用 play 2.4):ToStatement[T] 和 ToStatement[Option[T]]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 21:55