【问题标题】:Play Scala: How to access multiple databases with anorm and Magic[T]玩 Scala:如何使用 anorm 和 Magic 访问多个数据库[T]
【发布时间】:2011-12-06 02:12:13
【问题描述】:

我想在 Play Scala 中使用 anorm 和 Magic[T] 访问两个数据库(一个是 H2,另一个是 PostgreSQL)。就是不知道怎么设置...

我注意到我们可以在 conf/application.conf 中设置另一个数据库连接

db_other.url=jdbc:mysql://localhost/test
db_other.driver=com.mysql.jdbc.Driver
db_other.user=root
db_other.pass=

但是,我如何将它与 Magic 一起使用? (我看了Magic的源码,看不懂……我是Scala的大一新生)

无论如何,如果使用 Magic[T] 无法访问多个数据库,我希望使用 anorm 来完成,那么我该如何配置呢?

var sqlQuery = SQL(          //I guess some config params should be set here, but how?
    """
      select * from Country
    """
)

【问题讨论】:

    标签: database scala playframework anorm


    【解决方案1】:

    play.api.db.DB 中,您似乎可以传入您在application.conf 中定义的名称的字符串。

    然后使用此处指定的方法之一:http://www.playframework.org/documentation/2.0/ScalaDatabase

    # play.api.db.DB.class
    
    def withConnection[A](name : scala.Predef.String)(block : scala.Function1[java.sql.Connection, A])(implicit app : play.api.Application) : A = { /* compiled code */ }
    

    【讨论】:

      猜你喜欢
      • 2013-07-26
      • 1970-01-01
      • 2015-06-07
      • 1970-01-01
      • 1970-01-01
      • 2016-10-25
      • 2015-11-12
      • 1970-01-01
      • 2015-12-17
      相关资源
      最近更新 更多