postgresql - Slick 与 PostgreSQL Scala SBT Intellij IDEA

标签 postgresql scala intellij-idea slick

我正在尝试在带有 PostgreSQL 驱动程序的 Intellij IDEA 中使用 Slick 创建一个项目。但我只找到了this tutorial 我跟着它,但我有一个错误: 线程“main”中的异常 java.lang.ExceptionInInitializerError 在 Main.main(Main.scala) Caused by: 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"

关于postgresql - Slick 与 PostgreSQL Scala SBT Intellij IDEA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47243614/

相关文章:

scala - 在 Ammonite 中导入 $ivy

java - 找不到为什么 jmxRemoteURL 包含非 ASCII 字符

java - thymeleaf (th :action) not working when rendering long html

intellij-idea - 更改IntelliJ IDEA 2016中的默认工具窗口行为

mysql - 在数据库 (MySQL) 中表示和保留用户(使用用户名和密码以及社交登录名登录)

scala - Akka HTTP AuthenticationFailedRejection 原因

postgresql - 获取第一个空格后的字符串末尾

从多个表返回所有列的函数 (PostgreSQL)

postgresql - terraform 和 aurora postgresql 的存储类型错误

java - 多次迭代函数中使用的 JPA 查询优化