Scala Slick 3.0.3中的案例类过滤器标准
我有一个模型、相应的表和一个存储库。在我的存储库中,使用TableQuery,我希望根据一些条件(从model到boolean的函数)找到一个模型对象,存储库对此没有控制权,它被作为参数注入。例如Scala Slick 3.0.3中的案例类过滤器标准,scala,design-patterns,repository-pattern,slick,slick-3.0,Scala,Design Patterns,Repository Pattern,Slick,Slick 3.0,我有一个模型、相应的表和一个存储库。在我的存储库中,使用TableQuery,我希望根据一些条件(从model到boolean的函数)找到一个模型对象,存储库对此没有控制权,它被作为参数注入。例如 case class JournalEntryModel(id: Option[Long] = None, isDebit: Boolean, principal: Double) class JournalEntryTable(tag: Tag) extends Table[JournalEntr
case class JournalEntryModel(id: Option[Long] = None, isDebit: Boolean, principal: Double)
class JournalEntryTable(tag: Tag) extends Table[JournalEntryModel](tag, "journal_entries") {
def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
def isDebit = column[Boolean]("is_debit")
def principal = column[Double]("principal")
def * = (id.?, isDebit, principal) <>
(JournalEntryModel.tupled, JournalEntryModel.unapply)
}
object journalEntries extends TableQuery(new JournalEntryTable(_))
object Repository {
def find(criteria: JournalEntryModel => Boolean): List[JournalEntryModel] = db.run {
journalEntries.filter(je => criteria(je)).result
} toList
}
val credits = Repository.find(!_.isDebit && _.principal > 500.0)
case类JournalEntryModel(id:Option[Long]=None,isDebit:Boolean,principal:Double)
类JournalEntryTable(tag:tag)扩展了表[JournalEntryModel](tag,“journal\u条目”){
def id=列[Long](“id”,O.PrimaryKey,O.AutoInc)
def isDebit=列[布尔值](“是借方”)
def principal=列[Double](“principal”)
def*=(id.?,isDebit,委托人)
(JournalEntryModel.tuple,JournalEntryModel.unapply)
}
对象journalEntries扩展了TableQuery(新的JournalEntryTable()
对象存储库{
def find(条件:JournalEntryModel=>Boolean):List[JournalEntryModel]=db.run{
journalEntries.filter(je=>criteria(je)).result
}托利斯特
}
val credits=Repository.find(!.isDebit&&&.principal>500.0)
如何编写这样的过滤函数?我想你的问题是“如何为JournalEntryModel=>Boolean
编写函数文字?”
如果要使用类似的文字,则需要定义参数:
// Parameter list ("model" here) is required. You also need the argument type(s).
// Here, they're inferred from the argument to "find".
val credits = Repository.find(model => !model.isDebit && model.principal > 500.0)
您需要提供更多信息,说明问题中的解决方案不起作用的原因。它是否不编译,或者只是不返回正确的结果?