How to setup username and password with Slick's source code generator?

戏子无情 提交于 2020-01-02 01:56:10

问题


Following the directions in this page: http://slick.typesafe.com/doc/2.0.0/code-generation.html
we see that something like the following segment of code is required to generate models for mysql tables

val url = "jdbc:mysql://127.0.0.1/SOME_DB_SCHEMA?characterEncoding=UTF-8&useUnicode=true"

val slickDriver = "scala.slick.driver.MySQLDriver"

  val jdbcDriver = "com.mysql.jdbc.Driver"

  val outputFolder = "/some/path"

  val pkg = "com.pligor.server"

  scala.slick.model.codegen.SourceCodeGenerator.main(
    Array(slickDriver, jdbcDriver, url, outputFolder, pkg)
  )

These parameteres are enough for an H2 database as the example in the link has it.

How to include username and password for the MySQL database?


回答1:


From several links found in the internet and also based on the cvogt's answer this is the minimum that you need to do.

Note that this is a general solution for sbt. If you are dealing with play framework you might find it easier to perform this task with the relevant plugin

First of all you need a new sbt project because of all the library dependencies that are needed to be referenced in order for slick source generator to run.
Create the new sbt project using this tutorial: http://scalatutorials.com/beginner/2013/07/18/getting-started-with-sbt/
Preferably use the method Setup using giter8

If it happens to work with Intellij then you need to create file project/plugins.sbt and insert inside this line: addSbtPlugin("com.hanhuy.sbt" % "sbt-idea" % "1.6.0").
Execute gen-idea in sbt to generate an intellij project.

With giter8 you get an auto-generated file ProjectNameBuild.scala inside project folder. Open this and include at least these library dependencies:

libraryDependencies ++= List(
    "mysql" % "mysql-connector-java" % "5.1.27",
    "com.typesafe.slick" %% "slick" % "2.0.0",
    "org.slf4j" % "slf4j-nop" % "1.6.4",
    "org.scala-lang" % "scala-reflect" % scala_version
  )

where scala version is the variable private val scala_version = "2.10.3"

Now create the custom source code generator that looks like that:

import scala.slick.model.codegen.SourceCodeGenerator

object CustomSourceCodeGenerator {

import scala.slick.driver.JdbcProfile

import scala.reflect.runtime.currentMirror

def execute(url: String,
          jdbcDriver: String,
          user: String,
          password: String,
          slickDriver: String,
          outputFolder: String,
          pkg: String) = {
val driver: JdbcProfile = currentMirror.reflectModule(
  currentMirror.staticModule(slickDriver)
).instance.asInstanceOf[JdbcProfile]

driver.simple.Database.forURL(
  url,
  driver = jdbcDriver,
  user = user,
  password = password
).withSession {
  implicit session =>
    new SourceCodeGenerator(driver.createModel).writeToFile(slickDriver, outputFolder, pkg)
    }
  }
}

Finally you need to call this execute method inside main project object. Find the file ProjectName.scala that was auto-generated by giter8.
Inside it you will find a println call since this is merely a "hello world" application. Above println call something like that:

CustomSourceCodeGenerator.execute(
url = "jdbc:mysql://127.0.0.1/SOME_DB_SCHEMA?characterEncoding=UTF-8&useUnicode=true",
slickDriver = "scala.slick.driver.MySQLDriver",
jdbcDriver = "com.mysql.jdbc.Driver",
outputFolder = "/some/path",
pkg = "com.pligor.server",
user = "root",
password = "xxxxxyourpasswordxxxxx"
)

This way every time you execute sbt run you are going to generate the Table classes required by Slick automatically




回答2:


Note that at least for 2.0.1 this is fixed. Just add username and password to the end of the Array as Strings




回答3:


This has been asked and answered here: https://groups.google.com/forum/#!msg/scalaquery/UcS4_wyrJq0/obLHheIWIXEJ . Currently you need to customize the code generator. A PR for 2.0.1 is in the queue.




回答4:


My solution is nearly the same as George's answer, but I'll add mine anyway. This is the entire file I use to generate code for my mysql database in an SBT project.

SlickAutoGen.scala

package mypackage

import slick.model.codegen.SourceCodeGenerator

object CodeGen {

  def main(args: Array[String]) {
    SourceCodeGenerator.main(
      Array(
        "scala.slick.driver.MySQLDriver",
        "com.mysql.jdbc.Driver",
        "jdbc:mysql://localhost:3306/mydb",
        "src/main/scala/",
        "mypackage",
        "root",
        "" // I don't use a password on localhost
      )
    )
  }
}

build.sbt

// build.sbt --- Scala build tool settings

libraryDependencies ++= List(
  "com.typesafe.slick" %% "slick" % "2.0.1",
  "mysql" % "mysql-connector-java" % "5.1.24",
  ...
)

To use this, just modify settings, save in project root directory and run as follows:

$ sbt
> runMain mypackage.CodeGen


来源:https://stackoverflow.com/questions/21349807/how-to-setup-username-and-password-with-slicks-source-code-generator

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!