2

最近我开始学习 doobie,但我无法创建一个没有错误的 hikari 交易者。我正在使用 mysql,Intellij-Idea。

这是我的 build.sbt 文件

name := "doobie"
version := "0.1"
//scalaVersion := "2.13.1"
scalacOptions += "-Ypartial-unification" // 2.11.9+
libraryDependencies ++= {
  lazy val doobieVersion = "0.8.4"
  Seq(
    "org.tpolecat" %% "doobie-core"      % doobieVersion,
    "org.tpolecat"    %% "doobie-h2"            % doobieVersion,
    "org.tpolecat"    %% "doobie-hikari"        % doobieVersion,
    "org.tpolecat"    %% "doobie-quill"         % doobieVersion,
    "org.tpolecat"    %% "doobie-specs2"        % doobieVersion,
    "org.tpolecat"    %% "doobie-scalatest"     % doobieVersion   % "test",
    "mysql"           % "mysql-connector-java"  % "8.0.17",
    "org.slf4j"       % "slf4j-api"             % "1.7.5",
    "ch.qos.logback"  % "logback-classic"       % "1.0.9"
  )
}

resolvers ++= Seq(
  "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/"
)

这是我的 Connection.scala 文件

import cats.effect.IO
import com.zaxxer.hikari.{HikariConfig, HikariDataSource}
import doobie.hikari.HikariTransactor

trait Connection {

  val config = new HikariConfig()
  config.setJdbcUrl("jdbc:mysql://localhost:quill_demo")
  config.setUsername("admin")
  config.setPassword("password")
  config.setMaximumPoolSize(5)

  val transactor: IO[HikariTransactor[IO]] =
    IO.pure(HikariTransactor.apply[IO](new HikariDataSource(config)))

}

问题是在上面的文件IO.pure(HikariTransactor.apply[IO](new HikariDataSource(config)))语句中给出了错误。这里最后一个闭合大括号中的 3 个给出了 3 个错误,如下所示。

No implicit arguments of type: ContextShift[IO]
Unspecified value parameters: connectEC: ExecutionContext, transactEC: ExecutionContext
No implicits found for parameter evidence$2: ContextShift[IO]

我想知道,如何正确地做到这一点。

4

2 回答 2

0

尝试添加以下导入和隐式

import scala.concurrent.ExecutionContext

implicit val cs = IO.contextShift(ExecutionContext.global)
于 2019-10-24T14:31:17.730 回答
0

如下更改依赖项的版本后,我可以解决所有错误。

name := "doobie"
version := "0.1"
//scalaVersion := "2.13.1"
scalacOptions += "-Ypartial-unification"
libraryDependencies ++= {
  lazy val doobieVersion = "0.5.4"
  Seq(
    "org.tpolecat" %% "doobie-core"      % doobieVersion,
    "org.tpolecat"    %% "doobie-h2"            % doobieVersion,
    "org.tpolecat"    %% "doobie-hikari"        % doobieVersion,
    "org.tpolecat"    %% "doobie-quill"         % doobieVersion,
    "org.tpolecat"    %% "doobie-specs2"        % doobieVersion,
    "org.tpolecat"    %% "doobie-scalatest"     % doobieVersion   % "test",
    "mysql"           % "mysql-connector-java"  % "5.1.34",
    "org.slf4j"       % "slf4j-api"             % "1.7.5",
    "ch.qos.logback"  % "logback-classic"       % "1.0.9"
  )
}

resolvers ++= Seq(
  "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/"
)
于 2019-10-29T07:02:09.680 回答