slick是scala的一个FRM(Functional Relational Mapper)框架,即函数式的关系数据库编程工具库。使用slick不同于使用java的hibernate或者是mybatis,对其进行迭代开发非常方便,因为其对表的映射基于函数式的编程方式。
使用slick对数据库表映射比较方便。
比如有一个表
CREATE TABLE Persons ( id int, LastName varchar(255), FirstName varchar(255), Address varchar(255), City varchar(255) )
那么slick可以使用一个内部类进行映射
//内部类映射Persons表 class TagsTree(tag: Tag) extends Table[(Int, String, String, String,String)](tag, "Persons") { def id= column[Int]("id") def LastName= column[String]("LastName") def FirstName= column[String]("FirstName") def Address= column[String]("Address") def City= column[String]("City") def * = (id,LastName,FirstName,Address,City); }
但当遇到表中的字段超过22个的时候就有问题了,因为scala规定构造参数不能超过22个,那这个时候怎么办呢,有两个办法,一个是使用slick提供的HList。
使用HList
import slick.collection.heterogeneous.{ HList, HCons, HNil } type hList = String :: String :: Option[String] :: Option[String] :: Option[String] :: Option[String] :: String :: String :: String :: String :: String :: Int :: Int :: String :: String :: String :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: HNil; //需要以HNil结尾 class Table(tag: Tag) extends Table[hList](tag, "myTableName") { def name1 = column[String]("name1") def name2 = column[String]("name2") def name3 = column[Option[String]]("name3") def name4 = column[Option[String]]("name4") def name5 = column[Option[String]]("name5") def name6 = column[Option[String]]("name6") def name7 = column[String]("name7") def name8 = column[String]("name8") def name9 = column[String]("name9") def name10 = column[String]("name10") def name11 = column[String]("name11") def name12 = column[Int]("name12") def name13 = column[Int]("name13") def name14 = column[String]("name14") def name15 = column[String]("name15") def name16 = column[String]("name16") def name17 = column[Int]("name17") def name18= column[Int]("name18") def name19 = column[Int]("name19") def name20 = column[Int]("name20") def name21 = column[Int]("name21") def name22 = column[Int]("name22") def name23 = column[Int]("name23") def name24 = column[Int]("name24") def name25 = column[Int]("name25") def * = name1 :: name2 :: name3 :: name4 :: name5 :: name6 :: name7 :: name8 :: name8 :: name9 :: name10 :: name11 :: name12 :: name13 :: name14 :: name15 :: name16 :: name17 :: name18 :: name19 :: name20 :: name21 :: name22 :: name23 :: name24 :: name25 :: HNil //需要以HNil结尾 }
不过这种有个缺点,那就是当表中某些属性可能为空,这时需要使用Option[String]()这种数据类型来存放属性。但HList查询出来的时候只会像一个字符串一样的Some("value")。
另一个方法是思路比较简单,既然一个case class放不下,那就拆呗
//代码出处 https://github.com/slick/slick/blob/2.1.0-RC1/slick-testkit/src/main/scala/com/typesafe/slick/testkit/tests/JdbcMapperTest.scala#L106 def testWideMappedEntity { case class Part(i1: Int, i2: Int, i3: Int, i4: Int, i5: Int, i6: Int) case class Whole(id: Int, p1: Part, p2: Part, p3: Part, p4: Part) class T(tag: Tag) extends Table[Whole](tag, "t_wide") { def id = column[Int]("id", O.PrimaryKey) def p1i1 = column[Int]("p1i1") def p1i2 = column[Int]("p1i2") def p1i3 = column[Int]("p1i3") def p1i4 = column[Int]("p1i4") def p1i5 = column[Int]("p1i5") def p1i6 = column[Int]("p1i6") def p2i1 = column[Int]("p2i1") def p2i2 = column[Int]("p2i2") def p2i3 = column[Int]("p2i3") def p2i4 = column[Int]("p2i4") def p2i5 = column[Int]("p2i5") def p2i6 = column[Int]("p2i6") def p3i1 = column[Int]("p3i1") def p3i2 = column[Int]("p3i2") def p3i3 = column[Int]("p3i3") def p3i4 = column[Int]("p3i4") def p3i5 = column[Int]("p3i5") def p3i6 = column[Int]("p3i6") def p4i1 = column[Int]("p4i1") def p4i2 = column[Int]("p4i2") def p4i3 = column[Int]("p4i3") def p4i4 = column[Int]("p4i4") def p4i5 = column[Int]("p4i5") def p4i6 = column[Int]("p4i6") def * = ( id, (p1i1, p1i2, p1i3, p1i4, p1i5, p1i6), (p2i1, p2i2, p2i3, p2i4, p2i5, p2i6), (p3i1, p3i2, p3i3, p3i4, p3i5, p3i6), (p4i1, p4i2, p4i3, p4i4, p4i5, p4i6) ).shaped <> ({ case (id, p1, p2, p3, p4) => // We could do this without .shaped but then we‘d have to write a type annotation for the parameters Whole(id, Part.tupled.apply(p1), Part.tupled.apply(p2), Part.tupled.apply(p3), Part.tupled.apply(p4)) }, { w: Whole => def f(p: Part) = Part.unapply(p).get Some((w.id, f(w.p1), f(w.p2), f(w.p3), f(w.p4))) }) } val ts = TableQuery[T] val oData = Whole(0, Part(11, 12, 13, 14, 15, 16), Part(21, 22, 23, 24, 25, 26), Part(31, 32, 33, 34, 35, 36), Part(41, 42, 43, 44, 45, 46) ) ts.ddl.create ts.insert(oData) assertEquals(oData, ts.first) }