0

我的 Scala/Play DB URL 配置如下所示:

db.default.url="jdbc:mysql://localhost"

我不(不能)选择数据库,因为我使用多个数据库。

我有一种方法,它以列名和值对的映射形式从数据库表中检索数据:

def getSession(id: String, db: String): Map[String, Any] = DB.withSession {

    val columns = MTable.getTables(None, None, None, None).list.filter(_.name.name == "myTable").head.getColumns.list.map(_.column) 
    val result = sql"""SELECT * FROM #$db.myTable WHERE id=$id""".as[List[Any]].firstOption.map(columns zip _ toMap).get

}

MTable.getTables在这种情况下似乎不起作用,因为我认为它希望DB.withSession选择一个数据库,但事实并非如此。我该如何进行这项工作?

4

1 回答 1

0

You have to define multiple databases in Play if you want to use multiple databases, eg:

db.database1.url="jdbc:mysql://localhost/database1"
db.database2.url="jdbc:mysql://localhost/database2"
db.database3.url="jdbc:mysql://localhost/database3"

Then, you'll have to run the query on each different database, and merge the results, eg:

DB("database1").withSession { ... } ++ DB("database2").withSession { ... }

etc...

于 2013-11-28T02:07:23.397 回答