哪个SQL访问层可用于在Play-Scala中轻松阅读?

What SQL access layer to use for simple reading in Play-Scala?

我将在Play 2.1(Scala)中实现一个只读Web应用程序。由于我只会读取和整理读取到JSON的数据,因此我希望避免使用任何其他DSL和映射。

我已经使用dapper-dot-net在.NET / C#中完成了类似的项目,并且对结果的处理方式感到非常满意。没有大惊小怪,没有太多的锅炉板。

我目前正在查看:

  • anorm(anormtyped看起来也很有前途,但可能很早就采用。避免将变量手动映射到case类构造函数参数似乎很棒。)
  • 前传
  • 流畅-因为它被认为是2.1中执行SQL的主要方式,主要是简单的SQL API


光滑非常好。确保您阅读了有关这本简短的书-很好地解释了一些基础知识。与文档一起,它可以使您快速前进。另外,请注意,github中的文档更好-最新的尚未发布。

使用纯SQL选项非常受支持。但是,对于普通的sql查询,在类型检查方面您不需要太多。否则,使用Scala 2.10进行简单查询就很容易了:

1
SQL"select * from coffees where name = $name".as[Coffee]

这甚至可以保护您免受SQL注入的侵害,因为$name实际上不在查询中。有关更多信息,请参见文档。


我在GitHub上有一个受Dapper启发的项目,名为dbmapper

在Slick上的优势是:

  • 没有DSL-您已经知道一个好的数据DSL,它称为SQL
  • 完全异步
  • 很少的样板代码

这里是一个例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Scala class that maps TO the book TABLE, WITH COLUMNS TO MATCH the class members
CASE class Book(
  bookId: INT,
  title: String,
  retailPrice: BigDecimal,
  publishDate: LocalDate)

// mapping FUNCTION FROM TABLE ROW TO Book class,
// auto generated at compile TIME BY Scala Macro  
implicit def rowToBook: RowData => Book = (r) => DbCodeGenerator.rowToClass[Book](r)

// query returning future list OF books,
// safe query interpolation, the maxPrice IS converted TO a query argument  
val maxPrice = 11.99
val allBooksFuture = DbAsync.exec[Book](q"select * from book where retail_price < $maxPrice")        

val oneBook: Future[Book] = DbAsync.execOne[Book](q"select * from book where book_id = 2")

// RETURNS Future[OPTION[]]    
val maybeOneBook: Future[OPTION[Book]] = DbAsync.execOneOrNone[Book](q"select * from book where book_id = -123")

如果您从dotnet世界了解dapper,那么dbmapper会感到陌生!