【问题标题】:different development/production databases in scalaqueryscalaquery 中的不同开发/生产数据库
【发布时间】:2012-11-05 19:13:01
【问题描述】:

ScalaQuery 要求 (AFAIK) 在您的代码中使用提供程序特定的导入,例如:

import org.scalaquery.ql.extended.H2Driver.Implicit._

我们正在尝试在开发模式中使用 H2,在生产中使用 MySQL。有没有办法做到这一点?

【问题讨论】:

    标签: database scala scalaquery


    【解决方案1】:

    我在 playframework 中使用以下内容

    object test {
      lazy val extendedProfile = {
        val extendedProfileName = Play.configuration getString "db.default.extendedProfile" get
        companionObjectNamed(extendedProfileName).asInstanceOf[ExtendedProfile]
      }
    
      def companionObjectNamed(name: String) : AnyRef = {
        val c = Class forName (name + "$")
        c.getField("MODULE$") get c
      }
    }
    

    然后导入

    import util.extendedProfile.Implicit._
    

    org.scalaquery.ql.extended.MySQLDriver 是我在 config 中用来让 mysql 工作的字符串。

    【讨论】:

      【解决方案2】:

      我的做法是:

      class Subscribers(database: Database)(profile: ExtendedProfile) {
          import profile.Implicit._
      }
      

      订阅者基本上是我的数据访问对象。 不确定这是最好的方法。它解决了我的问题。

      你会创建这样的 DAO:

      ...在生产代码中:

      new Subscribers(database)(MySQLDriver)
      

      ...在测试代码中:

      new Subscribers(database)(H2Driver)
      

      【讨论】:

        猜你喜欢
        • 2015-11-18
        • 1970-01-01
        • 2012-04-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多