Slick不会在h2数据库

时间:2015-11-11 12:22:21

标签: 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

Application.conf:

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=""

BarisDatabase:

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
  }

Tables.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

我希望这是足够的信息

1 个答案:

答案 0 :(得分:0)

尝试改变:

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

val db = Database.forConfig("h2mem1")