简体   繁体   English

Scala Anorm-如何正确使用

[英]Scala Anorm - how use it properly

Scala's play framework claims that Anorm, and writing your own SQL is better that ORM's. Scala的游戏框架声称Anorm和编写自己的SQL比ORM更好。 One of the reasons is that you anyway most often want only transfer data between database and frontend as json. 原因之一是,您无论如何通常都只希望在数据库和前端之间以json格式传输数据。 However, most tutorials, and even Play documentation give examples of parsing sql's returned values into case classes, in order to parse it again into json. 但是,大多数教程甚至Play文档都提供了将sql的返回值解析为case类的示例,以便再次将其解析为json。 We still have an object relational mapping anyway, or am I missing a point? 无论如何,我们仍然有一个对象关系映射,还是我遗漏了一点?

In my database there exists a table with 33 columns. 在我的数据库中,存在一个包含33列的表。 Declaring a case class takes me 33 lines, declaring a parser with ~ operator, takes another 33. Using case statement to create an Object, another 66! 声明一个case类需要我33行,使用〜运算符声明一个解析器需要另外33行。使用case语句创建一个Object需另外66行! Seriously, what am I doing wrong? 说真的,我在做什么错? Is there any shortcut? 有捷径吗? In django the same thing takes only 33 lines. 在django中,同一件事只需要33行。

If you're using Anorm within a Play application, then the mapping into a Json object of your case class (assuming it has fairly normal apply and unapply functions defined for it, which most do) should be pretty much as simple as defining an implicit which uses the >2.10 macro based Json-inception methods...so all you actually need is a definition like this: 如果您在Play应用程序中使用Anorm,则映射到case类的Json对象(假设它具有相当正常的apply和unapply函数,大多数情况下都这样做)应该与定义隐式一样简单它使用基于> 2.10宏的Json-inception方法...因此,您真正需要的就是这样的定义:

implicit val myCaseFormats = Json.format[MyCaseClass]

where 'MyCaseClass' is the name of your case type. 其中“ MyCaseClass”是案例类型的名称。 You could even bake this into the parser combinator you use for de-serialising row-sets back from the database...that would dramatically clean up your code and cut down the amount of code you have to write. 您甚至可以将其烘焙到用于从数据库反序列化行集的解析器组合器中,这将极大地清理您的代码并减少您必须编写的代码量。

See here for details on the Json macros: https://www.playframework.com/documentation/2.1.1/ScalaJsonInception 有关Json宏的详细信息,请参见此处: https : //www.playframework.com/documentation/2.1.1/ScalaJsonInception

I use this quite extensively in a pretty large code-base and it does make things quite clean. 我在相当大的代码库中广泛使用了它,确实使事情变得很干净。

In terms of your parsers for Anorm, remember that you don't have to produce a case-class instance as a result of a parse...you can actually return anything you like, which could just be an indexed sequence of your column values (if you're using something like Shapeless to allow for mixed-type lists etc...) or some other structure. 在您的解析器ANORM而言,记住,你不必产生的情况下,类实例作为一个分析的结果......你其实可以返回任何你喜欢的,这可能只是你的列值的索引序列(如果您使用Shapeless之类的东西来允许混合类型列表等)或其他结构。

You do hav macro support in Anorm as well so the the parsers for your case classes can be one liners like this: 您也可以在Anorm中提供宏支持,因此您的case类的解析器可以是这样的一个衬里:

import norm.{Macro, Rowset}

val parser = Macro.namedParser[MyCaseClass] 

If you want to do something custom, (such as parse direct to JsValue) then you have the flexibility to just hand-craft a more crafty parser. 如果您想做一些定制的事情(例如直接解析到JsValue),那么您就可以灵活地手工制作一个更加狡猾的解析器。

HTH HTH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM