database - Slick 不在 h2 数据库中输入数据

标签 database scala playframework-2.0 h2 slick-3.0

我已经被这个问题困了好几天了。我有正确的连接和正确的查询,但是当我运行应用程序时,我的数据库没有任何变化,可能是它在 localhost:9000 上运行我的 html 网页而不是执行 Slick 查询? 我正在使用 IntelliJ IDEA 1.15 和 Typesafe Activator

构建:

libraryDependencies ++= Seq(
jdbc,
cache,
ws,
specs2 % Test,
"com.typesafe.slick" %% "slick" % "3.1.0-RC2",
"org.slf4j" % "slf4j-nop" % "1.7.10",
"org.slf4j" % "jcl-over-slf4j"  % "1.7.10",
"com.h2database" % "h2" % "1.4.187",
"org.scalatest" %% "scalatest" % "2.2.4" % "test"
).map(_.force())
// login embed:       jdbc:h2:~/test1
// login servermode:  jdbc:h2:tcp://localhost/~/test1
libraryDependencies += evolutions

应用程序配置文件:

h2mem1 = {
url = "jdbc:h2:mem:test1"
driver = org.h2.Driver
connectionPool = disabled
keepAliveConnection = true
}

db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:mem:test1;MODE=MYSQL;DB_CLOSE_DELAY=-1"
db.default.username=sa
db.default.password=""

Baris数据库:

import scala.concurrent.{Future, Await}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration.Duration
import slick.backend.DatabasePublisher
import slick.driver.H2Driver.api._

// The main application
object BarisDatabase extends App {
val db = Database.forURL("jdbc:h2:mem:test1;MODE=MYSQL;DB_CLOSE_DELAY=-1", driver="org.h2.Driver")
try {

// The query interface for the Suppliers table
val suppliers: TableQuery[Suppliers] = TableQuery[Suppliers]

// the query interface for the Coffees table
val coffees: TableQuery[Coffees] = TableQuery[Coffees]

val setupAction: DBIO[Unit] = DBIO.seq(
  // Create the schema by combining the DDLs for the Suppliers and Coffees
  // tables using the query interfaces
  (suppliers.schema ++ coffees.schema).create,

  // Insert some suppliers
  suppliers += (101, "Acme, Inc.", "99 Market Street", "Groundsville", "CA", "95199"),
  suppliers += ( 49, "Superior Coffee", "1 Party Place", "Mendocino", "CA", "95460"),
  suppliers += (150, "The High Ground", "100 Coffee Lane", "Meadows", "CA", "93966")
)

val setupFuture: Future[Unit] = db.run(setupAction)
val f = setupFuture.flatMap { _ =>

  // Insert some coffees (using JDBC's batch insert feature)
  val insertAction: DBIO[Option[Int]] = coffees ++= Seq (
    ("Colombian",         101, 7.99, 0, 0),
    ("French_Roast",       49, 8.99, 0, 0),
    ("Espresso",          150, 9.99, 0, 0),
    ("Colombian_Decaf",   101, 8.99, 0, 0),
    ("French_Roast_Decaf", 49, 9.99, 0, 0)
  )

  val insertAndPrintAction: DBIO[Unit] = insertAction.map { coffeesInsertResult =>
    // Print the number of rows inserted
    coffeesInsertResult foreach { numRows =>
      println(s"Inserted $numRows rows into the Coffees table")
    }
  }

  val allSuppliersAction: DBIO[Seq[(Int, String, String, String, String, String)]] =
    suppliers.result

  val combinedAction: DBIO[Seq[(Int, String, String, String, String, String)]] =
    insertAndPrintAction >> allSuppliersAction

  val combinedFuture: Future[Seq[(Int, String, String, String, String, String)]] =
    db.run(combinedAction)

  combinedFuture.map { allSuppliers =>
    allSuppliers.foreach(println)
  }
  }
  } finally db.close
  }

表.scala:

import slick.driver.H2Driver.api._
import slick.lifted.{ProvenShape, ForeignKeyQuery}

// A Suppliers table with 6 columns: id, name, street, city, state, zip class Suppliers(tag: Tag)
extends Table[(Int, String, String, String, String, String)](tag, "SUPPLIERS") {

// This is the primary key column:
def id: Rep[Int] = column[Int]("SUP_ID", O.PrimaryKey)
def name: Rep[String] = column[String]("SUP_NAME")
def street: Rep[String] = column[String]("STREET")
def city: Rep[String] = column[String]("CITY")
def state: Rep[String] = column[String]("STATE")
def zip: Rep[String] = column[String]("ZIP")

// Every table needs a * projection with the same type as the table's type parameter
def * : ProvenShape[(Int, String, String, String, String, String)] =
(id, name, street, city, state, zip)
}

// A Coffees table with 5 columns: name, supplier id, price, sales, total
class Coffees(tag: Tag)
extends Table[(String, Int, Double, Int, Int)](tag, "COFFEES") {

def name: Rep[String] = column[String]("COF_NAME", O.PrimaryKey)
def supID: Rep[Int] = column[Int]("SUP_ID")
def price: Rep[Double] = column[Double]("PRICE")
def sales: Rep[Int] = column[Int]("SALES")
def total: Rep[Int] = column[Int]("TOTAL")

def * : ProvenShape[(String, Int, Double, Int, Int)] = (name, supID, price, sales, total)

// A reified foreign key relation that can be navigated to create a join
def supplier: ForeignKeyQuery[Suppliers, (Int, String, String, String, String, String)] =
foreignKey("SUP_FK", supID, TableQuery[Suppliers])(_.id)
}

我从 Typesafe Activator 中的 Hello-Slick-3.1 教程中获得了大部分代码。 Filestructure in IntelliJ IDEA

我希望这是足够的信息

最佳答案

尝试改变:

val db = Database.forURL("jdbc:h2:mem:test1;MODE=MYSQL;DB_CLOSE_DELAY=-1", driver="org.h2.Driver")

val db = Database.forConfig("h2mem1")

关于database - Slick 不在 h2 数据库中输入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33650455/

相关文章:

database - Cassandra 未读消息计数

scala - 以无状态方式处理输入事件

scala - Scala Play 2.0 框架中的搜索表单提交和分页

playframework-2.0 - 玩框架 2 双重身份验证?

scala - 如何在 scala Play 中执行启动代码!框架应用?

ruby-on-rails - 在 Ruby 数据库中存储使用自定义类的哈希值

sql - 如何将一列拆分为不同的列

c# - 调整我的搜索功能

r - Spark ML 管道 Logistic 回归产生的预测比 R GLM 差得多

scala - 在没有匹配器的情况下如何跳过specs2中的测试?