我不知道它是Scala还是Play!题.我想从我的Play应用程序执行一些外部命令,从命令获取输出并根据命令输出向用户显示报告.有人可以帮忙吗?
例如,当我从shell 输入my-command时,它会显示如下所示的输出,我想捕获并显示在web中:
Id Name IP
====================
1 A x.y.z.a
2 B p.q.r.s
Run Code Online (Sandbox Code Playgroud)
请不要担心输出的格式和解析.在功能上,我看起来像PHP exec.我知道java Runtime.getRuntime().exec("command")但是有没有Scala/Play版本可以达到目的?
我想根据Id查询用户的单行.我有以下虚拟代码
case class User(
id: Option[Int],
name: String
}
object Users extends Table[User]("user") {
def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
def name = column[String]("name")
def * = id ~ name <>(User, User.unapply _)
def findById(userId: Int)(implicit session: Session): Option[User] = {
val user = this.map { e => e }.where(u => u.id === userId).take(1)
val usrList = user.list
if (usrList.isEmpty) None
else Some(usrList(0))
}
}
Run Code Online (Sandbox Code Playgroud)
在我看来,findById查询单个列是一种过度杀伤,因为Id是标准主键.有谁知道更好的方法?请注意我正在使用Play!2.1.0
我有两个模型(比如供应商和咖啡),咖啡模型有供应商模型的外键参考.在ddl期间,我希望这种关系存在于表创建中.但我也希望能够通过Coffee对象引用Supplier对象coffeeObj.supplier.name.下面是我的虚拟代码.我正在使用MappedTable,foreignKey和TypeMapper.
import scala.slick.driver.H2Driver.simple._
import Database.threadLocalSession
object SlickTest {
// Supplier
case class Supplier(id: Option[Int], name: String)
object Suppliers extends Table[Supplier]("supplier") {
def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
def name = column[String]("name")
def * = id.? ~ name <> (Supplier, Supplier.unapply _)
def findOneById(id: Int): Supplier =
this.map { e => e }.where(r => r.id === id).firstOption.get
}
// Implicit Conversion from Supplier to Int
implicit val supTypeMapper = …Run Code Online (Sandbox Code Playgroud)