簡體   English   中英

帶有PostgreSQL Scala SBT Intellij IDEA的Slick

[英]Slick with PostgreSQL Scala SBT Intellij IDEA

我正在嘗試使用PostgreSQL驅動程序在Intellij IDEA中使用Slick創建一個項目。 但是我設法只找到了我遵循的本教程 ,但是卻遇到了一個錯誤:Main.main(Main.scala)上的線程“ main”中的異常java.lang.ExceptionInInitializerError引起原因:com.typesafe.config.ConfigException $ Missing:找不到鍵“ url”的配置設置

這是我為主類的代碼:

import scala.slick.driver.PostgresDriver.simple._

object Main {

  case class Song(
                   id: Int,
                   name: String,
                   singer: String)

  class SongsTable(tag: Tag) extends Table[Song](tag, "songs") {
    def id = column[Int]("id")

    def name = column[String]("name")

    def singer = column[String]("singer")

    def * = (id, name, singer) <> (Song.tupled, Song.unapply)
  }

  lazy val songsTable = TableQuery[SongsTable] 
  val db = Database.forConfig("scalaxdb")

  def main(args: Array[String]): Unit = {

    val connectionUrl = "jdbc:postgresql://localhost/songs?user=postgres&password=postgresp"

    Database.forURL(connectionUrl, driver = "org.postgresql.Driver") withSession {
      implicit session =>
        val songs = TableQuery[SongsTable]
        songs.list foreach { row =>
          println("song with id " + row.id + " has name " + row.name + " and a singer is " + row.singer)
        }
    }
  }
}

這些是application.conf文件:

scalaxdb = {
  dataSourceClass = "slick.jdbc.DatabaseUrlDataSource"
  properties = {
    driver = "org.postgresql.Driver"
    url = "jdbc:postgresql://localhost/dbname?user=user&password=password"
  }
}

這是build.sbt:

libraryDependencies ++= Seq(
  "org.postgresql" % "postgresql" % "9.3-1100-jdbc4",
  "com.typesafe.slick" %% "slick" % "2.1.0",
  "org.slf4j" % "slf4j-nop" % "1.6.4"
)

我不知道我在做什么錯。 對於解決此問題的任何建議,我將不勝感激。

您是否介意使用最新版本的Slick?

import slick.jdbc.PostgresProfile.api._
import scala.concurrent.Await
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._

object Main {

  case class Song(
                   id: Int,
                   name: String,
                   singer: String)

  class SongsTable(tag: Tag) extends Table[Song](tag, "songs") {
    def id = column[Int]("id")

    def name = column[String]("name")

    def singer = column[String]("singer")

    def * = (id, name, singer) <> (Song.tupled, Song.unapply)
  }

  val db = Database.forConfig("scalaxdb")

  val songs = TableQuery[SongsTable]

  def main(args: Array[String]): Unit = { 
    Await.result({
      db.run(songs.result).map(_.foreach(row =>
        println("song with id " + row.id + " has name " + row.name + " and a singer is " + row.singer)))
    }, 1 minute)
  }
}

build.sbt

scalaVersion := "2.12.4"

libraryDependencies += "com.typesafe.slick" %% "slick" % "3.2.1"
libraryDependencies += "org.slf4j" % "slf4j-nop" % "1.7.25"
libraryDependencies += "com.typesafe.slick" %% "slick-hikaricp" % "3.2.1"
libraryDependencies += "org.postgresql" % "postgresql" % "42.1.4"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM