我在GitHub 上有一个深受 Dapper 启发的项目,名为dbmapper
优于 Slick 的优点是:
- 没有 DSL——你已经知道一个好的数据 DSL,它叫做 SQL
- 完全异步
- 很少的样板代码
这是一个例子:
// 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 会感到奇怪的熟悉!