I've been using iBATIS for years and have been very happy with it. iBATIS is very good about letting one write their own SQL while handling the mundane work of mapping data to/from the objects/database. I would love a Sc开发者_StackOverflow中文版ala specific library that does the same type of mappings that iBATIS does. I figure a Scala specific tool would
- not require the objects to be Java Beans (i.e. getters and setters)
- use Option instead of null values
- I think that's it, but there may be more
I've seen a bunch of stuff on the web talking about ORMs for Java and Scala, but I haven't seen anything like iBATIS for Scala.
Does anybody know of a tool like this in Scala?
Times have changed. There is now a MyBatis Scala project which is much more idiomatic to Scala.
http://mybatis.github.io/scala/
I've evaluated it, and it looked like a lot less hassle than any of the other ORM or Scala oriented persistence libraries.
The links on their project page are currently broken, but you can get to the GitHub page here: https://github.com/mybatis/scala
They have various samples under "mybatis-scala-samples". This DAO / CRUD example is a particularly nice example: ItemDAO.scala
Why not just carry on using iBatis? It's Java, after all (and hence can be used from Scala). I still use Spring JDBC as my DAO layer.
As for the scala-specifics; you could add the @BeanProperty
annotation to generate getters/setters and then declare a method to guard for null
:
@BeanProperty var injectedXyz : String
def xyz : Option[String] = Option(injectedXyz)
Admittedly this is not great (i.e. requires extra boilerplate). But I have not seen anything that looks like a widely-used scala DAO layer (for SQL)
On the Scala website (www.scala-lang.org/node/6539), nilskp recommends orbroker (http://code.google.com/p/orbroker/) because it is written natively for Scala.
If I were to choose an ORM I'd look into Squeryl (http://squeryl.org/). I've tried out the Lift Mapper and it works well with the Lift Webkit, but it's a little bit to integrated and have certain design choices I don't like.
You might prefer ScalikeJDBC. Take a look at it.
https://github.com/seratch/scalikejdbc
It also has source code generator. Especially if you access the existing legacy database, it's much convenient.
https://github.com/seratch/scalikejdbc-mapper-generator
精彩评论