Play 2框架中的几个数据源带有光滑

时间:2017-05-08 17:09:45

标签: database scala playframework-2.0 slick-3.0 play-slick

如何使用Play 2.5.x FrameworkSlick中配置和使用多个数据源? 例如,一个数据源应为MySQL,另一个PostgreSQL

  slick.dbs.default.driver = "utils.db.TetraoPostgresProfile$"
  slick.dbs.default.db.driver = "org.postgresql.Driver"
  slick.dbs.default.db.url = "jdbc:postgresql://localhost:5432/templatesite_db"
  slick.dbs.default.db.user = "user"
  slick.dbs.default.db.password = "pass"

  slick.dbs.secondary.driver = "slick.jdbc.MySQLProfile$"
  slick.dbs.secondary.db.driver = "com.mysql.jdbc.Driver"
  slick.dbs.secondary.db.url = "jdbc:mysql://localhost:3306/dbName"
  slick.dbs.secondary.db.user = "root"
  slick.dbs.secondary.db.password = "pass"

1 个答案:

答案 0 :(得分:0)

这在ScalaDatabase文档页面中直接解决:

// inject "orders" database instead of "default"
class ScalaInjectNamed @Inject()(
  @NamedDatabase("orders") db: Database) extends Controller {
  // do whatever you need with the db
}

https://www.playframework.com/documentation/2.5.x/PlaySlick#usage

https://www.playframework.com/documentation/2.5.x/ScalaDatabase#obtaining-a-jdbc-connection