我以前使用过类似这样的代码生成:
scala.slick.codegen.SourceCodeGenerator.main( Array("com.typesafe.slick.driver.ms.SQLServerDriver", "net.sourceforge.jtds.jdbc.Driver", "jdbc:jtds:sqlserver://10.0.1.12 :1433;databaseName=master;", "/home/bipin/slickcode", "demo", "user", "pass") )
它生成可以连接到数据库的类。但我不需要所有的表格,只需要其中的几个。所以我使用了一个自定义的 slick 代码生成器,如下所示(取自https://github.com/slick/slick-codegen-customization-example/blob/master/codegen/CustomizedCodeGenerator.scala并修改):
import scala.slick.model.Model
import scala.slick.jdbc.meta.createModel
import com.typesafe.slick.driver.ms.SQLServerDriver
object CustomizedCodeGenerator{
def main(args: Array[String]) = {
codegen.writeToFile(
"com.typesafe.slick.driver.ms.SQLServerDriver",
"/home/bipin/slickcode",
"demo"
)
}
val db = SQLServerDriver.simple.Database.forURL(
"jdbc:jtds:sqlserver://10.0.1.12:1433;databaseName=master;user=user;password=pass;",
driver="net.sourceforge.jtds.jdbc.Driver")
// filter out desired tables
val included = Seq("Table1","Table2","T3","T4")
val model = db.withSession{ implicit session =>
val tables = SQLServerDriver.getTables.list.filter(t => included contains t.name.name)
tables.foreach(println);
createModel( tables, SQLServerDriver )
}
val codegen = new scala.slick.codegen.SourceCodeGenerator(model){
// customize Scala entity name (case class, etc.)
override def entityName = dbTableName => dbTableName match {
case "COFFEES" => "Coffee"
case _ => super.entityName(dbTableName)
}
// customize Scala table name (table class, table values, ...)
override def tableName = dbTableName => dbTableName match {
case "COF_INVENTORY" => "CoffeeInventory"
case _ => super.tableName(dbTableName)
}
// override generator responsible for tables
override def Table = new Table(_){
table =>
// customize table value (TableQuery) name (uses tableName as a basis)
override def TableValue = new TableValue{
override def rawName = super.rawName.uncapitalize
}
// override generator responsible for columns
override def Column = new Column(_){
// customize Scala column names
override def rawName = (table.model.name.table,this.model.name) match {
case ("T3","@Column1") => "Column1"
case _ => super.rawName
}
}
}
}
}
但是当我运行它时,输出文件没有任何表 ddl。
/** DDL for all tables. Call .create to execute. */
lazy val ddl =
谁能告诉我我做错了什么以及如何解决它。谢谢