课程表

Slick课程

工具箱
速查手册

Slick 基本查询

当前位置:免费教程 » 数据库/运维 » Slick

基本查询

我们准备好了开发环境,下面就来看看 Slick 的基本查询方法,我们打算查询 Chinook 中的 Album 表,我们先看看之前自动生成的代码中表 Album 的定义:

  1. /** Entity class storing rows of table Album
  2. * @param albumid Database column AlbumId PrimaryKey
  3. * @param title Database column Title
  4. * @param artistid Database column ArtistId */
  5. case class AlbumRow(albumid: Int, title: String, artistid: Int)
  6. /** GetResult implicit for fetching AlbumRow objects using plain SQL queries */
  7. implicit def GetResultAlbumRow(implicit e0: GR[Int], e1: GR[String]): GR[AlbumRow] = GR{
  8. prs => import prs._
  9. AlbumRow.tupled((<<[Int], <<[String], <<[Int]))
  10. }
  11. /** Table description of table Album. Objects of this class serve as prototypes for rows in queries. */
  12. class Album(tag: Tag) extends Table[AlbumRow](tag, "Album") {
  13. ...
  14. /** Database column AlbumId PrimaryKey */
  15. val albumid: Column[Int] = column[Int]("AlbumId", O.PrimaryKey)
  16. /** Database column Title */
  17. val title: Column[String] = column[String]("Title")
  18. /** Database column ArtistId */
  19. val artistid: Column[Int] = column[Int]("ArtistId")
  20. /** Foreign key referencing Artist (database name FK_AlbumArtistId) */
  21. lazy val artistFk = foreignKey("FK_AlbumArtistId", artistid, Artist)
  22. (r => r.artistid, onUpdate=ForeignKeyAction.NoAction, onDelete=ForeignKeyAction.NoAction)
  23. }
  24. /** Collection-like TableQuery object for table Album */
  25. lazy val Album = new TableQuery(tag => new Album(tag))

可以看到对于数据库中每个表,我们定义了一个 case class 代表表的一行,如 AlbumRow,一个 Table 类,比如 Album,还定义了一个 lazy 变量 Album,可以直接使用这个变量以集合类对象的方式来查询数据表。下面我们看看查询的基本用法:

我们在和 Tables.scala 的同一个目录下(本例为 com/guidebee/slick/example)创建一个 Example.scala 文件:

  1. package com.guidebee.slick.example
  2. import scala.slick.driver.MySQLDriver.simple._
  3. import com.guidebee.slick.example.Tables._
  4. // The main application
  5. object Example extends App {
  6. Database.forURL("jdbc:mysql://127.0.0.1/Chinook",
  7. driver = "com.mysql.jdbc.Driver",
  8. user="user",
  9. password="password").withSession {
  10. implicit session =>
  11. // <- write queries here
  12. Album foreach { case AlbumRow(albumId,title,artistId) =>
  13. println(" " + albumId + ":" + title + ":" + artistId)
  14. }
  15. }
  16. }

注意:修改正确的用户名和密码。

其中代码

  1. Database.forURL("jdbc:mysql://127.0.0.1/Chinook",
  2. driver = "com.mysql.jdbc.Driver",
  3. user="user",
  4. password="password").withSession {
  5. implicit session =>
  6. // <- write queries here
  7. }
  8. }

用来连接数据库,并且创建一个 Session 对象,所有数据库相关查询都可以在这个代码块中实现,这里我们打印出 Album 中所有记录:

图片

Album 为一集合对象,因此我们可以使用 Scala 集合对象支持的方法,来过滤,比较,比如:

  1. val q1= for (a <- Album;if a.albumid<10)
  2. yield (a.albumid,a.title,a.artistid)
  3. q1 foreach println

显示前 9 条记录:(1,For Those About To Rock We Salute You,1)(2,Balls to the Wall,2)(3,Restless and Wild,2)(4,Let There Be Rock,1)(5,Big Ones,3)(6,Jagged Little Pill,4)(7,Facelift,5)(8,Warner 25 Anos,6)(9,Plays Metallica By Four Cellos,7)

  1. val q1= for (a <- Album;if a.albumid<10)
  2. yield a.albumid.asColumnOf[String] ++ LiteralColumn(":") ++ a.title
  3. q1 foreach println

1:For Those About To Rock We Salute You2:Balls to the Wall3:Restless and Wild4:Let There Be Rock5:Big Ones6:Jagged Little Pill7:Facelift8:Warner 25 Anos9:Plays Metallica By Four Cellos

我们再来看看多个表 Join 的情况:先看看直接使用 SQL 语句

  1. select album.AlbumId,album.Title,artist.Name from album
  2. INNER JOIN artist
  3. ON album.ArtistId=artist.ArtistId
  4. WHERE album.AlbumId<10

图片

那么使用 Scala 语句如何实现呢,也就是多个集合对象 Join 的情况:

  1. val q3 = for {
  2. a <- Album if a.albumid < 10
  3. t <- Artist if a.artistid===t.artistid
  4. } yield (a.albumid,a.title,t.name)
  5. q3 foreach println

注意,比较运算符为===,我们也可以直接使用外键来查询,在 Tables.scala,类 Album 中定义了一个外键 artistFk

  1. val q2 = for {
  2. a <- Album if a.albumid < 10
  3. t <- a.artistFk
  4. } yield (a.albumid,a.title,t.name)
  5. q2 foreach println

两种方法都输出如下结果:

  1. (1,For Those About To Rock We Salute You,Some(AC/DC))
  2. (2,Balls to the Wall,Some(Accept))
  3. (3,Restless and Wild,Some(Accept))
  4. (4,Let There Be Rock,Some(AC/DC))
  5. (5,Big Ones,Some(Aerosmith))
  6. (6,Jagged Little Pill,Some(Alanis Morissette))
  7. (7,Facelift,Some(Alice In Chains))
  8. (8,Warner 25 Anos,Some(Antônio Carlos Jobim))
  9. (9,Plays Metallica By Four Cellos,Some(Apocalyptica))
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号