How to use Anorm outside of Play?

若如初见. 提交于 2019-12-04 03:54:59
cchantep

There is no need of DB object (part of Play JDBC not Anorm). Anorm works as along as you provide it connection as implicit:

implicit val con: java.sql.Connection = ??? // whatever you want to resolve connection

SQL"SELECT * FROM Table".as(...)

You can resolve JDBC connection in many way: basic DriverManager.getConnection, JNDI, ...

As for dependency, it's easy to add it in SBT: How to declare dependency on Play's Anorm for a standalone application? .

You could also emulate the DB object as follows (i haven't tried this though)

 object DB {
    def withConnection[A](block: Connection => A): A = {
      val connection: Connection = ConnectionPool.borrow()

      try {
        block(connection)
      } finally {
        connection.close()
      }
    }
  }

Taken from https://github.com/TimothyKlim/anorm-without-play/blob/master/src/main/scala/Main.scala

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!