使用slick的codegen生成table code等

来源:互联网 发布:淘宝进货网下载 编辑:程序博客网 时间:2024/04/29 08:22

具体的

http://slick.typesafe.com/doc/3.0.3/code-generation.html

object TableGe extends App{        slick.codegen.SourceCodeGenerator.main(    Array("slick.driver.MySQLDriver", "com.mysql.jdbc.Driver", "jdbc:mysql://localhost/test", "table2", "table2", "root", "111111")  )}
生成的代码比较规范,值得学习

package table2// AUTO-GENERATED Slick data model/** Stand-alone Slick data model for immediate use */object Tables extends {  val profile = slick.driver.MySQLDriver} with Tables/** Slick data model trait for extension, choice of backend or usage in the cake pattern. (Make sure to initialize this late.) */trait Tables {  val profile: slick.driver.JdbcProfile  import profile.api._  import slick.model.ForeignKeyAction  // NOTE: GetResult mappers for plain SQL are only generated for tables where Slick knows how to map the types of all columns.  import slick.jdbc.{GetResult => GR}  /** DDL for all tables. Call .create to execute. */  lazy val schema = Coffees.schema ++ Suppliers.schema  @deprecated("Use .schema instead of .ddl", "3.0")  def ddl = schema  /** Entity class storing rows of table Coffees   *  @param name Database column name SqlType(VARCHAR), Length(20,true)   *  @param supId Database column sup_id SqlType(INT)   *  @param price Database column price SqlType(DOUBLE)   *  @param sales Database column sales SqlType(INT)   *  @param total Database column total SqlType(INT) */  case class CoffeesRow(name: String, supId: Int, price: Double, sales: Int, total: Int)  /** GetResult implicit for fetching CoffeesRow objects using plain SQL queries */  implicit def GetResultCoffeesRow(implicit e0: GR[String], e1: GR[Int], e2: GR[Double]): GR[CoffeesRow] = GR{    prs => import prs._    CoffeesRow.tupled((<<[String], <<[Int], <<[Double], <<[Int], <<[Int]))  }  /** Table description of table coffees. Objects of this class serve as prototypes for rows in queries. */  class Coffees(_tableTag: Tag) extends Table[CoffeesRow](_tableTag, "coffees") {    def * = (name, supId, price, sales, total) <> (CoffeesRow.tupled, CoffeesRow.unapply)    /** Maps whole row to an option. Useful for outer joins. */    def ? = (Rep.Some(name), Rep.Some(supId), Rep.Some(price), Rep.Some(sales), Rep.Some(total)).shaped.<>({r=> _1.map(_=> CoffeesRow.tupled((_1.get, _2.get, _3.get, _4.get, _5.get)))}, (_:Any) =>  throw new Exception("Inserting into ? projection not supported."))    /** Database column name SqlType(VARCHAR), Length(20,true) */    val name: Rep[String] = column[String]("name", O.Length(20,varying=true))    /** Database column sup_id SqlType(INT) */    val supId: Rep[Int] = column[Int]("sup_id")    /** Database column price SqlType(DOUBLE) */    val price: Rep[Double] = column[Double]("price")    /** Database column sales SqlType(INT) */    val sales: Rep[Int] = column[Int]("sales")    /** Database column total SqlType(INT) */    val total: Rep[Int] = column[Int]("total")    /** Foreign key referencing Suppliers (database name coffees_ibfk_1) */    lazy val suppliersFk = foreignKey("coffees_ibfk_1", supId, Suppliers)(r => r.id, onUpdate=ForeignKeyAction.NoAction, onDelete=ForeignKeyAction.NoAction)  }  /** Collection-like TableQuery object for table Coffees */  lazy val Coffees = new TableQuery(tag => new Coffees(tag))  /** Entity class storing rows of table Suppliers   *  @param id Database column id SqlType(INT), PrimaryKey   *  @param name Database column name SqlType(VARCHAR), Length(20,true)   *  @param street Database column street SqlType(VARCHAR), Length(30,true)   *  @param city Database column city SqlType(VARCHAR), Length(30,true)   *  @param state Database column state SqlType(VARCHAR), Length(80,true)   *  @param zip Database column zip SqlType(VARCHAR), Length(10,true) */  case class SuppliersRow(id: Int, name: String, street: String, city: String, state: String, zip: String)  /** GetResult implicit for fetching SuppliersRow objects using plain SQL queries */  implicit def GetResultSuppliersRow(implicit e0: GR[Int], e1: GR[String]): GR[SuppliersRow] = GR{    prs => import prs._    SuppliersRow.tupled((<<[Int], <<[String], <<[String], <<[String], <<[String], <<[String]))  }  /** Table description of table suppliers. Objects of this class serve as prototypes for rows in queries. */  class Suppliers(_tableTag: Tag) extends Table[SuppliersRow](_tableTag, "suppliers") {    def * = (id, name, street, city, state, zip) <> (SuppliersRow.tupled, SuppliersRow.unapply)    /** Maps whole row to an option. Useful for outer joins. */    def ? = (Rep.Some(id), Rep.Some(name), Rep.Some(street), Rep.Some(city), Rep.Some(state), Rep.Some(zip)).shaped.<>({r=> _1.map(_=> SuppliersRow.tupled((_1.get, _2.get, _3.get, _4.get, _5.get, _6.get)))}, (_:Any) =>  throw new Exception("Inserting into ? projection not supported."))    /** Database column id SqlType(INT), PrimaryKey */    val id: Rep[Int] = column[Int]("id", O.PrimaryKey)    /** Database column name SqlType(VARCHAR), Length(20,true) */    val name: Rep[String] = column[String]("name", O.Length(20,varying=true))    /** Database column street SqlType(VARCHAR), Length(30,true) */    val street: Rep[String] = column[String]("street", O.Length(30,varying=true))    /** Database column city SqlType(VARCHAR), Length(30,true) */    val city: Rep[String] = column[String]("city", O.Length(30,varying=true))    /** Database column state SqlType(VARCHAR), Length(80,true) */    val state: Rep[String] = column[String]("state", O.Length(80,varying=true))    /** Database column zip SqlType(VARCHAR), Length(10,true) */    val zip: Rep[String] = column[String]("zip", O.Length(10,varying=true))  }  /** Collection-like TableQuery object for table Suppliers */  lazy val Suppliers = new TableQuery(tag => new Suppliers(tag))}

在Table中关键有三点

DDL

  /** DDL for all tables. Call .create to execute. */  lazy val schema = Coffees.schema ++ Suppliers.schema  @deprecated("Use .schema instead of .ddl", "3.0")  def ddl = schema

CASE CLASS

case class CoffeesRow
case class SuppliersRow
以及Table的定义

还有就是提供数据与class映射的implicit


  case class CoffeesRow(name: String, supId: Int, price: Double, sales: Int, total: Int)  /** GetResult implicit for fetching CoffeesRow objects using plain SQL queries */  implicit def GetResultCoffeesRow(implicit e0: GR[String], e1: GR[Int], e2: GR[Double]): GR[CoffeesRow] = GR{    prs => import prs._    CoffeesRow.tupled((<<[String], <<[Int], <<[Double], <<[Int], <<[Int]))  }





0 0
原创粉丝点击