使用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
- 使用slick的codegen生成table code等
- 使用slick 3.2 codegen 和mysql connector 6自动生成代码
- 基于JavaScript的代码自动生成工具 JavaScript Based Code Generator - codegen
- Swagger-Codegen的简单使用
- 使用Maven 插件 cxf-codegen-plugin生成WebService测试程序
- codeGen生成二维码
- slick使用普通的sql操作数据
- 例程codegen功能之一:限制软件的使用期限
- 动态生成Table的行、列以及删除等
- slick
- vs出现CODEGEN的错误
- Hibernate Codegen(Hibernate自动生成工具)
- scala slick基本使用教程
- API生命周期第三阶段:API实施:使用swagger codegen生成可部署工程,择取一个作为mock service
- 有关core data有关Codegen使用过程中出现的问题
- Slick-pg v0.6.5.2 ,Slick 的 PG 扩展
- JQUERY动态生成DIV,table等数据
- table的model生成
- UI课程06 UIController及其子类,已学所有类的关系
- Linux C语言程序设计(十九)——基于UDP的网络编程
- struts2(6)
- php设计模式
- 黑马程序员——学习日记8
- 使用slick的codegen生成table code等
- HightChart标示区(plotBands)
- 3d相册弹出效果02
- hibernate3映射关联关系举例
- 中间件简介
- QT中QString 类的使用
- 用户注册表单验证
- C#在Winform中改变Textbox高度三种方法
- markdown 标记补充