本文来自:fair-jm.iteye.com 转截请注明出处
接着上一篇文
其实具体的都在slick官网的doc中:
http://slick.typesafe.com/doc/2.0.0/
这边就总结了一个CRUD 表结构接着使用笔记(1) 没什么变化(因为没设外键 所以就在单表操作):
Tables.Person来自于上一文 用slick的代码生成器生成
slick2有一套和scala的collection类似的操作(不同的是slick2操作的是Query Query可以通过list方法转变为scala中的List)
package slick_test import org.junit.Test import org.junit.Before import scala.slick.jdbc.JdbcBackend.Database import scala.slick.driver.MySQLDriver.simple._ class TestApp { var database: Database = null @Before def setUp: Unit = { database = Database.forURL(url = "jdbc:mysql://localhost:3306/test", user = "root", password = "", driver = "com.mysql.jdbc.Driver") } @Test def insert: Unit = { database withSession { implicit session => //Tables.Person += new Tables.PersonRow(2, Some(23), Some("[email protected]"), Some("cc"), Some("1111")) //Tables.Person insert Tables.PersonRow(0, Some(23), Some("[email protected]"), Some("cc"), Some("1111")) //使用数据库的批量插入(如果数据库支持的话) Tables.Person ++= Seq( Tables.PersonRow(0, Some(22), Some("[email protected]"), Some("cc1"), Some("1111")), Tables.PersonRow(0, Some(22), Some("[email protected]"), Some("cc2"), Some("1111")), Tables.PersonRow(0, Some(22), Some("[email protected]"), Some("cc3"), Some("1111")), Tables.PersonRow(0, Some(22), Some("[email protected]"), Some("cc4"), Some("1111")), Tables.PersonRow(0, Some(22), Some("[email protected]"), Some("cc5"), None)) } } @Test def query: Unit = { database withSession { implicit session => //使用 SELECT * FROM COFFEES 这边的*是在Person中定义的*方法反射得到的 Tables.Person foreach { case Tables.PersonRow(id, age, email, name, phone) => println(s"id:$id,age:$age,email:$email,name:$name,phone:$phone") } //使用for val q1 = for (p <- Tables.Person) yield (p.email, p.age) q1 foreach println //输出年龄大于21的人名 用了get因为name属性是Option[String] Tables.Person filter { _.age>21 } map {_.name.get} foreach println } } @Test def delete : Unit ={ database withTransaction { implicit session => //删除是先得到查询的Query再执行delete Tables.Person filter { _.id === 17 } delete } } @Test def update : Unit ={ database withSession { implicit session => //操作依旧是在Query上的 和删除的思路是一样的 val q1=Tables.Person.filter(_.id === 2) //更新所有的属性(注意id也会被更新.. q1.update(Tables.PersonRow(2, Some(13), Some("[email protected]"), Some("fairjm"), Some("2222"))) //得到查询语句 println(q1.updateStatement) //得到查询的调用者 println(q1.updateInvoker) //更新单个属性 val q2=Tables.Person.filter(_.id === 2).map(_.name) q2.update(Some("cc")) } } @Test def aggregation : Unit ={ database withSession { implicit session => val ages=Tables.Person map(_.age) //得到的sum不会被立即执行 此外还有max min avg这些聚合函数 val sum=ages.sum //输出sum Query(sum).foreach(println) //打印sum的语句: //select x2.x3 from (select sum(x4.x5) as x3 from (select x6.`age` as x5 from `person` x6) x4) x2 println(sum.selectStatement) } } }
还漏了分页 分页官网上有例子的 我就贴一下代码了:
http://slick.typesafe.com/doc/2.0.0/queries.html 写道
val q2 = coffees.drop(10).take(5)
// compiles to SQL (simplified):
// select "COF_NAME", "SUP_ID", "PRICE", "SALES", "TOTAL"
// from "COFFEES"
// limit 5 offset 10
// compiles to SQL (simplified):
// select "COF_NAME", "SUP_ID", "PRICE", "SALES", "TOTAL"
// from "COFFEES"
// limit 5 offset 10