数据库
首页 > 数据库> > 无法使Scala,Slick和MySQL协同工作

无法使Scala,Slick和MySQL协同工作

作者:互联网

我试图使这三个一起工作,但不能.缺少光滑的文档.我有像这样的Typesafe配置的application.conf设置:

mysql = {
  url = "jdbc:mysql://localhost/slickdb"
  slick.driver=scala.slick.driver.MySQLDriver
  driver=com.mysql.cj.jdbc.Driver
  properties = {
    user = root
    password = null
  }
  connectionPool = true
  keepAliveConnection = true
}

和build.sbt相关的依赖项:

libraryDependencies ++= Seq(
   ...
   "org.eclipse.jetty" % "jetty-webapp" % "9.2.15.v20160210" % "container",
   "javax.servlet" % "javax.servlet-api" % "3.1.0" % "provided",
   "com.typesafe.slick" %% "slick" % "3.2.0",
   "com.typesafe.slick" %% "slick-hikaricp" % "3.2.0",
   "mysql" % "mysql-connector-java" % "latest.integration"
   ...
)

并尝试编译一个简单的DAO

import slick.jdbc.MySQLProfile.api._
class testDAO {

  private val db = Database.forConfig("mysql")
} 

但是SBT编译器抛出此异常:

java.lang.ClassNotFoundException: true
        at java.lang.ClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at slick.util.ClassLoaderUtil$$anon$1.loadClass(ClassLoaderUtil.scala:17)
        at slick.jdbc.JdbcDataSource$.loadFactory$1(JdbcDataSource.scala:37)
        at slick.jdbc.JdbcDataSource$.forConfig(JdbcDataSource.scala:46)

我也尝试过:

import slick.jdbc.MySQLProfile.api._
import slick.basic.DatabaseConfig

class testDAO {
  val dbConfig = DatabaseConfig.forConfig("mysql") 
}

但我得到另一个异常:

slick.SlickException: Configured profile com.mysql.cj.jdbc.Driver does not conform to requested profile scala.runtime.Nothing$
        at slick.basic.DatabaseConfig$.forConfig(DatabaseConfig.scala:99)
        at com.unamur.service.testDAO.<init>(CatDAO.scala:18)
        at com.unamur.app.TestServlet.<init>(TestServlet.scala:11)
        at ScalatraBootstrap.init(ScalatraBootstrap.scala:7)

有人有主意吗?提前致谢.

解决方法:

这里的问题是connectionPool = true.您需要提供HikariCP或其他实现.

标签:scala,slick,mysql
来源: https://codeday.me/bug/20191111/2020724.html