简体   繁体   English

如何在 Play 之外使用 Anorm?

[英]How to use Anorm outside of Play?

How do you use Anorm outside of play in Scala?你如何在 Scala 的游戏之外使用 Anorm? In the Anorm document for play, it simply uses something like:在用于播放的 Anorm 文档中,它只是使用以下内容:

DB.withConnection { implicit c =>
  val result: Boolean = SQL("Select 1").execute()    
} 

The DB object is only for Play. DB对象仅用于播放。 How do you use Anorm alone without using Play?如何在不使用 Play 的情况下单独使用 Anorm?

There is no need of DB object (part of Play JDBC not Anorm).不需要DB对象(Play JDBC 的一部分而不是 Anorm)。 Anorm works as along as you provide it connection as implicit: Anorm 与您提供隐式连接一样工作:

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, ...您可以通过多种方式解析 JDBC 连接:基本的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?至于依赖,在 SBT 中很容易添加: 如何为独立应用程序声明对 Play 的 Anorm 的依赖? . .

You could also emulate the DB object as follows (i haven't tried this though)您还可以按如下方式模拟 DB 对象(不过我还没有尝试过)

 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摘自https://github.com/TimothyKlim/anorm-without-play/blob/master/src/main/scala/Main.scala

Documenting the code that works for me below:在下面记录对我有用的代码:

Entry to include in dependencies in build.sbt :包含在build.sbt依赖项中的build.sbt

// https://mvnrepository.com/artifact/org.playframework.anorm/anorm
libraryDependencies += "org.playframework.anorm" %% "anorm" % "2.6.7"

Write helper classes:编写辅助类:

    @Singleton
    class DBUtils {
    
      val schema = AppConfig.defaultSchema
    
      def withDefaultConnection(sqlQuery: SqlQuery) = {
// could replace with DBCP, not got a chance yet
        val conn = DriverManager.getConnection(AppConfig.dbUrl,AppConfig.dbUser, AppConfig.dbPassword)
        val result = Try(sqlQuery.execute()(conn))
        conn.close()
        result
      }
    }
    object DBUtils extends DBUtils

Next, any query can use the withDefaultConnection method to execute:接下来,任何查询都可以使用withDefaultConnection方法来执行:

  def saveReviews(listOfReviews: List[Review]):Try[Boolean]= {
    val query = SQL(
      s"""insert into aws.reviews
         | (                 reviewerId,
         |                 asin,
         |                 overall,
         |                 summary,
         |                 unixReviewTime,
         |                 reviewTime
         | )
         |values ${listOfReviews.mkString(",")}""".stripMargin)
    //println(query.toString())
    DBUtils.withDefaultConnection(query)
  }

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

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