如何使用Slick的源代码生成器设置用户名和密码? [英] How to setup username and password with Slick's source code generator?

查看:155
本文介绍了如何使用Slick的源代码生成器设置用户名和密码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照此页面上的指示进行操作: http://slick.typesafe .com/doc/2.0.0/code-generation.html
我们看到需要类似以下代码段的代码才能为mysql表生成模型

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)
  )

这些参数对于H2数据库就足够了,如链接中的示例所示.

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

如何包括MySQL数据库的用户名和密码?

How to include username and password for the MySQL database?

推荐答案

在Internet上找到的几个链接中,也基于cvogt的回答,这是您需要做的最少工作.

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

请注意,这是sbt的通用解决方案.如果您使用的是Play框架,则可能会发现使用相关插件更容易执行此任务

首先,您需要一个新的sbt项目,因为需要引用所有库依赖关系才能运行平滑的源生成器.
使用本教程创建新的sbt项目: http://scalatutorials .com/beginner/2013/07/18/getting-started-with-sbt/
最好使用方法使用giter8设置

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

如果碰巧与Intellij兼容,则需要创建文件project/plugins.sbt并插入以下行:addSbtPlugin("com.hanhuy.sbt" % "sbt-idea" % "1.6.0").
在sbt中执行gen-idea生成一个intellij项目.

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.

使用giter8,您将在项目文件夹中获得一个自动生成的文件ProjectNameBuild.scala.打开它并至少包括以下库依赖项:

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
  )

其中scala版本是变量private val scala_version = "2.10.3"

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)
    }
  }
}

最后,您需要在主项目对象中调用此execute方法.查找由giter8自动生成的文件ProjectName.scala.
在其中可以找到println调用,因为这只是一个"hello world"应用程序.在println上方调用类似的内容:

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

这样,每次执行sbt run时,您将自动生成Slick所需的Table类

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

这篇关于如何使用Slick的源代码生成器设置用户名和密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆