使用 slick.jdbc.MySQLProfile 将 Slick 3.2.1 连接到 MySQL
Connecting Slick 3.2.1 to MySQL using slick.jdbc.MySQLProfile
SO 上有 100 多个关于如何将 slick 连接到 Mysql 的线程,并且所有线程都使用 "slick.driver.MySQLDriver$"。我相信这个 class 现在已被弃用并已被 "slick.jdbc.MySQLProfile"
取代
这里的产品文档已经说明了这一点
http://slick.lightbend.com/doc/3.2.1/api/#slick.driver.package
所以为了使用新的 class 我将我的配置定义为
mysql = {
driver = "slick.jdbc.MySQLProfile"
properties = {
driver = "com.mysql.cj.jdbc.Driver"
url = "jdbc:mysql://localhost:3306/foo"
user = "foo"
password = "bar"
}
}
Database.forConfig("mysql")
但我得到一个例外
java.lang.RuntimeException: Failed to load class of driverClassName slick.jdbc.MySQLProfile
at com.zaxxer.hikari.HikariConfig.setDriverClassName(HikariConfig.java:323)
at slick.jdbc.hikaricp.HikariCPJdbcDataSource$.$anonfun$forConfig(HikariCPJdbcDataSource.scala:31)
at slick.jdbc.hikaricp.HikariCPJdbcDataSource$.$anonfun$forConfig$adapted(HikariCPJdbcDataSource.scala:31)
at scala.Option.map(Option.scala:146)
at slick.jdbc.hikaricp.HikariCPJdbcDataSource$.forConfig(HikariCPJdbcDataSource.scala:31)
我尝试了许多其他组合,但似乎对新 class 没有任何效果。那么当 slick.driver.MySQLDriver$ 被弃用时,现在正确的连接方式是什么?
这是我的 libraryDependencies 来自 build.sbt
"com.typesafe.slick" %% "slick" % "3.2.1",
"com.typesafe.slick" %% "slick-hikaricp" % "3.2.1",
"com.typesafe.slick" %% "slick-codegen" % "3.2.1",
"mysql" % "mysql-connector-java" % "5.1.34",
是的,我确实需要连接池。
编辑:根据下面的建议,我将配置更改为
mysql = {
profile = "slick.jdbc.MySQLProfile$"
properties = {
driver = "com.mysql.cj.jdbc.Driver"
url = "jdbc:mysql://local:3306/foo"
user = "foo"
password = "bar"
}
}
但现在我得到错误
java.lang.IllegalArgumentException: dataSource or dataSourceClassName or jdbcUrl is required.
at com.zaxxer.hikari.HikariConfig.validate(HikariConfig.java:786)
at com.zaxxer.hikari.HikariDataSource.<init>(HikariDataSource.java:67)
at slick.jdbc.hikaricp.HikariCPJdbcDataSource$.forConfig(HikariCPJdbcDataSource.scala:58)
at slick.jdbc.hikaricp.HikariCPJdbcDataSource$.forConfig(HikariCPJdbcDataSource.scala:21)
documentation 建议将其放入您的配置中:
profile = "slick.jdbc.MySQLProfile$"
注意行尾的 $
。
此外,将以下内容添加到 enable connection pooling:
dataSourceClass = "slick.jdbc.DatabaseUrlDataSource"
如果有人需要,这里是完整的工作配置
mysql = {
profile = "slick.jdbc.MySQLProfile$"
dataSourceClass = "slick.jdbc.DatabaseUrlDataSource"
properties = {
driver = "com.mysql.cj.jdbc.Driver"
url = "jdbc:mysql://localhost:3306/foo"
user = "foo"
password = "bar"
}
}
满build.sbt
"com.typesafe.slick" %% "slick" % "3.2.1",
"com.typesafe.slick" %% "slick-hikaricp" % "3.2.1",
"com.typesafe.slick" %% "slick-codegen" % "3.2.1",
"mysql" % "mysql-connector-java" % "5.1.34",
连接建立使用
Database.forConfig("mysql")
使用 HikariCP 池
application.conf
slick {
profile = "slick.jdbc.MySQLProfile$"
db {
connectionPool = "HikariCP"
url = "jdbc:mysql://127.0.0.1/abc"
user = "user"
password = "passabc"
driver = "com.mysql.cj.jdbc.Driver"
numThreads = 5
maxConnections = 5
minConnections = 1
}
}
build.sbt
"com.typesafe.slick" %% "slick" % slickVersion,
"com.typesafe.slick" %% "slick-hikaricp" % slickVersion,
"mysql" % "mysql-connector-java" % "8.0.15",
SO 上有 100 多个关于如何将 slick 连接到 Mysql 的线程,并且所有线程都使用 "slick.driver.MySQLDriver$"。我相信这个 class 现在已被弃用并已被 "slick.jdbc.MySQLProfile"
取代这里的产品文档已经说明了这一点
http://slick.lightbend.com/doc/3.2.1/api/#slick.driver.package
所以为了使用新的 class 我将我的配置定义为
mysql = {
driver = "slick.jdbc.MySQLProfile"
properties = {
driver = "com.mysql.cj.jdbc.Driver"
url = "jdbc:mysql://localhost:3306/foo"
user = "foo"
password = "bar"
}
}
Database.forConfig("mysql")
但我得到一个例外
java.lang.RuntimeException: Failed to load class of driverClassName slick.jdbc.MySQLProfile
at com.zaxxer.hikari.HikariConfig.setDriverClassName(HikariConfig.java:323)
at slick.jdbc.hikaricp.HikariCPJdbcDataSource$.$anonfun$forConfig(HikariCPJdbcDataSource.scala:31)
at slick.jdbc.hikaricp.HikariCPJdbcDataSource$.$anonfun$forConfig$adapted(HikariCPJdbcDataSource.scala:31)
at scala.Option.map(Option.scala:146)
at slick.jdbc.hikaricp.HikariCPJdbcDataSource$.forConfig(HikariCPJdbcDataSource.scala:31)
我尝试了许多其他组合,但似乎对新 class 没有任何效果。那么当 slick.driver.MySQLDriver$ 被弃用时,现在正确的连接方式是什么?
这是我的 libraryDependencies 来自 build.sbt
"com.typesafe.slick" %% "slick" % "3.2.1",
"com.typesafe.slick" %% "slick-hikaricp" % "3.2.1",
"com.typesafe.slick" %% "slick-codegen" % "3.2.1",
"mysql" % "mysql-connector-java" % "5.1.34",
是的,我确实需要连接池。
编辑:根据下面的建议,我将配置更改为
mysql = {
profile = "slick.jdbc.MySQLProfile$"
properties = {
driver = "com.mysql.cj.jdbc.Driver"
url = "jdbc:mysql://local:3306/foo"
user = "foo"
password = "bar"
}
}
但现在我得到错误
java.lang.IllegalArgumentException: dataSource or dataSourceClassName or jdbcUrl is required.
at com.zaxxer.hikari.HikariConfig.validate(HikariConfig.java:786)
at com.zaxxer.hikari.HikariDataSource.<init>(HikariDataSource.java:67)
at slick.jdbc.hikaricp.HikariCPJdbcDataSource$.forConfig(HikariCPJdbcDataSource.scala:58)
at slick.jdbc.hikaricp.HikariCPJdbcDataSource$.forConfig(HikariCPJdbcDataSource.scala:21)
documentation 建议将其放入您的配置中:
profile = "slick.jdbc.MySQLProfile$"
注意行尾的 $
。
此外,将以下内容添加到 enable connection pooling:
dataSourceClass = "slick.jdbc.DatabaseUrlDataSource"
如果有人需要,这里是完整的工作配置
mysql = {
profile = "slick.jdbc.MySQLProfile$"
dataSourceClass = "slick.jdbc.DatabaseUrlDataSource"
properties = {
driver = "com.mysql.cj.jdbc.Driver"
url = "jdbc:mysql://localhost:3306/foo"
user = "foo"
password = "bar"
}
}
满build.sbt
"com.typesafe.slick" %% "slick" % "3.2.1",
"com.typesafe.slick" %% "slick-hikaricp" % "3.2.1",
"com.typesafe.slick" %% "slick-codegen" % "3.2.1",
"mysql" % "mysql-connector-java" % "5.1.34",
连接建立使用
Database.forConfig("mysql")
使用 HikariCP 池
application.conf
slick {
profile = "slick.jdbc.MySQLProfile$"
db {
connectionPool = "HikariCP"
url = "jdbc:mysql://127.0.0.1/abc"
user = "user"
password = "passabc"
driver = "com.mysql.cj.jdbc.Driver"
numThreads = 5
maxConnections = 5
minConnections = 1
}
}
build.sbt
"com.typesafe.slick" %% "slick" % slickVersion,
"com.typesafe.slick" %% "slick-hikaricp" % slickVersion,
"mysql" % "mysql-connector-java" % "8.0.15",