加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 在Slick中使用DatabaseConfig和Database有什么区别?

发布时间:2020-12-16 18:51:27 所属栏目:安全 来源:网络整理
导读:我在 slick’s documentation阅读有关DatabaseConfig的内容: On top of the configuration syntax for Database ,there is another layer in the form of DatabaseConfig which allows you to configure a Slick driver plus a matching Database together.
我在 slick’s documentation阅读有关DatabaseConfig的内容:

On top of the configuration syntax for Database,there is another
layer in the form of DatabaseConfig which allows you to configure a
Slick driver plus a matching Database together. This makes it easy to
abstract over different kinds of database systems by simply changing a
configuration file
.

我没有得到这一部分,DatabaseConfig如何使底层数据库系统比数据库方法更抽象?假设,我在以下测试中使用DatabaseConfig:

import org.scalatest.{Matchers,FlatSpec}
import slick.backend.DatabaseConfig
import slick.driver.JdbcProfile
import slick.driver.PostgresDriver.api._

import scala.concurrent.ExecutionContext.Implicits.global

class DatabaseConfigTest extends FlatSpec with Matchers {
  def withDb(test: DatabaseConfig[JdbcProfile] => Any) = {
    val dbConfig = DatabaseConfig.forConfig[JdbcProfile]("abstract")

    try test(dbConfig)
    finally dbConfig.db.close()
  }

  "DatabaseConfig" should "work" in withDb { dbConfig =>
    import Supplier._

    val cities = suppliers.map(_.city)

    dbConfig.db.run(cities.result).map(_.foreach(println))
  }
}

如您所见,如果我将我的底层数据库系统从PostgreSQL更改为MySQL,除了配置更改之外,我还需要更改将postgre API导入mysql的import语句.另一方面,如果我使用数据库:

import org.scalatest.{FlatSpec,Matchers}
import slick.driver.PostgresDriver.api._
import slick.jdbc.JdbcBackend.Database

import scala.concurrent.ExecutionContext.Implicits.global

class DatabaseTest extends FlatSpec with Matchers {
  def withDb(test: Database => Any) = {
    val db = Database.forConfig("default")

    try test(db)
    finally db.close()
  }

  "Supplier names" should "be fetched" in withDb { db =>
    import Supplier._

    val names = suppliers.map(_.name)

    db.run(names.result).map(_.foreach(println))
  }
}

当我使用数据库时,底层数据库上的相同更改将导致两个更改:一个在配置文件中,另一个在源代码中.说了这么多,一种方法比另一种更抽象?我使用DatabaseConfig错了吗?

解决方法

你很接近,但你还没有正确使用DatabaseConfig.您需要导入与配置关联的驱动程序,而不是导入特定的驱动程序.这样的事情应该有效:

import org.scalatest.{Matchers,FlatSpec}
import slick.backend.DatabaseConfig
import slick.jdbc.JdbcProfile
//import slick.driver.PostgresDriver.api._

import scala.concurrent.ExecutionContext.Implicits.global

class DatabaseConfigTest extends FlatSpec with Matchers {
  def withDb(test: DatabaseConfig[JdbcProfile] => Any) = {
    val dbConfig = DatabaseConfig.forConfig[JdbcProfile]("abstract")

    /* The api for the driver specified in the config is imported here. */
    import dbConfig.driver.api._  

    try test(dbConfig)
    finally dbConfig.db.close()
  }

  "DatabaseConfig" should "work" in withDb { dbConfig =>
    import Supplier._

    val cities = suppliers.map(_.city)

    dbConfig.db.run(cities.result).map(_.foreach(println))
  }
}

这应该允许您在配置中切换数据库,而无需更改任何代码或重新编译.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读