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

编程入门 行业动态 更新时间:2024-10-15 18:26:26
本文介绍了在 Slick 中使用 DatabaseConfig 和 Database 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在 slick 中阅读了有关 DatabaseConfig 的内容文档:

I was reading about DatabaseConfig in slick's documentation:

在Database 的配置语法之上,还有一个DatabaseConfig 形式的层,它允许您配置一个光滑的驱动程序加上一个匹配的Database.这样可以很容易地通过简单地改变一个数据库系统来抽象不同类型的数据库系统配置文件.

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 如何使底层数据库系统比 Database 方法更抽象?假设,我在以下测试中使用 DatabaseConfig:

I don't get this part, how DatabaseConfig makes the underlying database system more abstract than the Database approach? Suppose, i'm using DatabaseConfig in the following test:

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,除了配置更改之外,我还需要更改 import 将 postgre API 导入到 mysql 的语句.另一方面,如果我使用 Database:

As you can see, if i change my underlying database system from PostgreSQL to MySQL, in addition to configuration change, i need to change the import statement that imports the postgre API to mysql's. On the other hand, If i was using Database:

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)) } }

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

When i'm using Database, same change on the underlying database, would result in two changes: one in configuration file and the other in source code. With all these being said, how one approach is more abstract than the other one? Am i using DatabaseConfig wrong?

推荐答案

您已经很接近了,但是您没有完全正确地使用 DatabaseConfig.您需要导入与配置关联的驱动程序,而不是导入特定的驱动程序.这样的事情应该可以工作:

You are close, but you aren't quite using DatabaseConfig properly. Rather than importing a specific driver, you need to import the driver associated with the config. Something like this should work:

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)) } }

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

This should allow you to switch databases in the config without having to change any code or recompile.

更多推荐

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

本文发布于:2023-11-15 14:25:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1595234.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有什么区别   Slick   DatabaseConfig   Database

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!