您需要通过 JdbcProfile 抽象出 Slick 配置文件。这将消除您已确定为问题的特定于 Oracle 的部分。
听起来您有一个相当大的应用程序。在这种情况下,通常(根据我的经验)将 Slick 配置文件作为类或特征的参数,然后在您知道它是什么时传入特定的配置文件。
例如(使用Essential Slick 中的示例代码)我们可以说我们的应用程序配置文件必须有一个JdbcProfile:
import slick.jdbc.JdbcProfile
trait Profile {
val profile: JdbcProfile
}
然后,对于数据库代码的每个模块,我们导入profile API:
trait DatabaseModule1 { self: Profile =>
import profile.api._
// Write database code here
}
注意抽象 profile.api._ 导入如何替换特定于数据库的 slick.jdbc.OracleProfile.api._
如果你有很多模块,你可以将它们组合成一个案例类:
class DatabaseLayer(val profile: JdbcProfile) extends
Profile with
DatabaseModule1 with
DatabaseModule2
最后,在程序的最后,您可以决定使用什么配置:
object Main extends App {
// At this point, you can make a test and decide which specific
// database you want to use. Here I'm saying it's always H2:
val databaseLayer = new DatabaseLayer(slick.jdbc.H2Profile)
// You can now use methods in `databaseLayer`,
// or pass it to other modules in your system
}
这在以下有进一步描述: